2
2
* Copyright (C) Microsoft Corporation. All rights reserved.
3
3
*--------------------------------------------------------*/
4
4
5
+ import * as path from "path" ;
5
6
import vscode = require( 'vscode' ) ;
6
7
import {
7
8
TextDocument ,
@@ -11,6 +12,7 @@ import {
11
12
DocumentFormattingEditProvider ,
12
13
DocumentRangeFormattingEditProvider ,
13
14
Range ,
15
+ TextEditor
14
16
} from 'vscode' ;
15
17
import { LanguageClient , RequestType } from 'vscode-languageclient' ;
16
18
import Window = vscode . window ;
@@ -95,6 +97,10 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
95
97
// hence we keep this as an option but set it true by default.
96
98
private aggregateUndoStop : boolean ;
97
99
100
+ private get emptyPromise ( ) : Promise < TextEdit [ ] > {
101
+ return Promise . resolve ( TextEdit [ 0 ] ) ;
102
+ }
103
+
98
104
constructor ( aggregateUndoStop = true ) {
99
105
this . aggregateUndoStop = aggregateUndoStop ;
100
106
}
@@ -112,17 +118,28 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
112
118
options : FormattingOptions ,
113
119
token : CancellationToken ) : TextEdit [ ] | Thenable < TextEdit [ ] > {
114
120
121
+ let editor : TextEditor = this . getEditor ( document ) ;
122
+ if ( editor === undefined ) {
123
+ Window . showWarningMessage ( `Something went wrong. Cannot format ${ path . basename ( document . fileName ) } ` ) ;
124
+ return this . emptyPromise ;
125
+ }
126
+
115
127
if ( this . isDocumentLocked ( document ) ) {
116
- return ;
128
+ Window . showWarningMessage ( `Formatting ${ path . basename ( document . fileName ) } . Please wait.` ) ;
129
+ return this . emptyPromise ;
117
130
}
118
131
119
132
this . lockDocument ( document ) ;
120
- let textEdits : Thenable < TextEdit [ ] > = this . executeRulesInOrder ( document , range , options , 0 ) ;
133
+ let textEdits : Thenable < TextEdit [ ] > = this . executeRulesInOrder ( editor , range , options , 0 ) ;
121
134
this . releaseDocument ( document , textEdits ) ;
122
135
AnimatedStatusBar . showAnimatedStatusBarMessage ( "Formatting PowerShell document" , textEdits ) ;
123
136
return textEdits ;
124
137
}
125
138
139
+ getEditor ( document : TextDocument ) : TextEditor {
140
+ return Window . visibleTextEditors . find ( ( e , n , obj ) => { return e . document === document ; } ) ;
141
+ }
142
+
126
143
isDocumentLocked ( document : TextDocument ) : boolean {
127
144
if ( PSDocumentFormattingEditProvider . filesBeingFormatted . hasOwnProperty ( this . getDocumentKey ( document ) ) ) {
128
145
return true ;
@@ -150,13 +167,14 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
150
167
}
151
168
152
169
executeRulesInOrder (
153
- document : TextDocument ,
170
+ editor : TextEditor ,
154
171
range : Range ,
155
172
options : FormattingOptions ,
156
173
index : number ) : Thenable < TextEdit [ ] > {
157
174
if ( this . languageClient !== null && index < this . ruleOrder . length ) {
158
- let rule = this . ruleOrder [ index ] ;
175
+ let rule : string = this . ruleOrder [ index ] ;
159
176
let uniqueEdits : ScriptRegion [ ] = [ ] ;
177
+ let document : TextDocument = editor . document ;
160
178
let edits : ScriptRegion [ ] ;
161
179
162
180
return this . languageClient . sendRequest (
@@ -197,22 +215,29 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
197
215
// we do not return a valid array because our text edits
198
216
// need to be executed in a particular order and it is
199
217
// easier if we perform the edits ourselves
200
- return this . applyEdit ( uniqueEdits , range , 0 , index ) ;
218
+ return this . applyEdit ( editor , uniqueEdits , range , 0 , index ) ;
201
219
} )
202
220
. then ( ( ) => {
203
221
// execute the same rule again if we left out violations
204
222
// on the same line
223
+ let newIndex : number = index + 1 ;
205
224
if ( uniqueEdits . length !== edits . length ) {
206
- return this . executeRulesInOrder ( document , range , options , index ) ;
225
+ newIndex = index ;
207
226
}
208
- return this . executeRulesInOrder ( document , range , options , index + 1 ) ;
227
+
228
+ return this . executeRulesInOrder ( editor , range , options , newIndex ) ;
209
229
} ) ;
210
230
} else {
211
- return Promise . resolve ( TextEdit [ 0 ] ) ;
231
+ return this . emptyPromise ;
212
232
}
213
233
}
214
234
215
- applyEdit ( edits : ScriptRegion [ ] , range : Range , markerIndex : number , ruleIndex : number ) : Thenable < void > {
235
+ applyEdit (
236
+ editor : TextEditor ,
237
+ edits : ScriptRegion [ ] ,
238
+ range : Range ,
239
+ markerIndex : number ,
240
+ ruleIndex : number ) : Thenable < void > {
216
241
if ( markerIndex >= edits . length ) {
217
242
return ;
218
243
}
@@ -226,7 +251,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
226
251
edit . endLineNumber - 1 ,
227
252
edit . endColumnNumber - 1 ) ;
228
253
if ( range === null || range . contains ( editRange ) ) {
229
- return Window . activeTextEditor . edit ( ( editBuilder ) => {
254
+ return editor . edit ( ( editBuilder ) => {
230
255
editBuilder . replace (
231
256
editRange ,
232
257
edit . text ) ;
@@ -235,11 +260,11 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
235
260
undoStopAfter : undoStopAfter ,
236
261
undoStopBefore : undoStopBefore
237
262
} ) . then ( ( isEditApplied ) => {
238
- return this . applyEdit ( edits , range , markerIndex + 1 , ruleIndex ) ;
263
+ return this . applyEdit ( editor , edits , range , markerIndex + 1 , ruleIndex ) ;
239
264
} ) ; // TODO handle rejection
240
265
}
241
266
else {
242
- return this . applyEdit ( edits , range , markerIndex + 1 , ruleIndex ) ;
267
+ return this . applyEdit ( editor , edits , range , markerIndex + 1 , ruleIndex ) ;
243
268
}
244
269
}
245
270
0 commit comments