@@ -12,7 +12,8 @@ import {
12
12
DocumentFormattingEditProvider ,
13
13
DocumentRangeFormattingEditProvider ,
14
14
Range ,
15
- TextEditor
15
+ TextEditor ,
16
+ TextLine
16
17
} from 'vscode' ;
17
18
import { LanguageClient , RequestType } from 'vscode-languageclient' ;
18
19
import Window = vscode . window ;
@@ -134,6 +135,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
134
135
private static documentLocker = new DocumentLocker ( ) ;
135
136
private static statusBarTracker = new Object ( ) ;
136
137
private languageClient : LanguageClient ;
138
+ private lineDiff : number ;
137
139
138
140
// The order in which the rules will be executed starting from the first element.
139
141
private readonly ruleOrder : string [ ] = [
@@ -153,6 +155,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
153
155
154
156
constructor ( aggregateUndoStop = true ) {
155
157
this . aggregateUndoStop = aggregateUndoStop ;
158
+ this . lineDiff = 0 ;
156
159
}
157
160
158
161
provideDocumentFormattingEdits (
@@ -195,6 +198,12 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
195
198
PSDocumentFormattingEditProvider . disposeAllStatusBars ( ) ;
196
199
}
197
200
201
+ private snapRangeToEdges ( range : Range , document : TextDocument ) : Range {
202
+ return range . with ( {
203
+ start : range . start . with ( { character : 0 } ) ,
204
+ end : document . lineAt ( range . end . line ) . range . end } ) ;
205
+ }
206
+
198
207
private getEditor ( document : TextDocument ) : TextEditor {
199
208
return Window . visibleTextEditors . find ( ( e , n , obj ) => { return e . document === document ; } ) ;
200
209
}
@@ -248,12 +257,18 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
248
257
// we need to update the range as the edits might
249
258
// have changed the original layout
250
259
if ( range !== null ) {
251
- let tempRange : Range = this . getSelectionRange ( document ) ;
252
- if ( tempRange !== null ) {
253
- range = tempRange ;
260
+ if ( this . lineDiff !== 0 ) {
261
+ range = range . with ( { end : range . end . translate ( { lineDelta : this . lineDiff } ) } ) ;
254
262
}
263
+
264
+ // extend the range such that it starts at the first character of the
265
+ // start line of the range.
266
+ range = this . snapRangeToEdges ( range , document ) ;
255
267
}
256
268
269
+ // reset line difference to 0
270
+ this . lineDiff = 0 ;
271
+
257
272
// we do not return a valid array because our text edits
258
273
// need to be executed in a particular order and it is
259
274
// easier if we perform the edits ourselves
@@ -292,7 +307,13 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
292
307
edit . startColumnNumber - 1 ,
293
308
edit . endLineNumber - 1 ,
294
309
edit . endColumnNumber - 1 ) ;
295
- if ( range === null || range . contains ( editRange ) ) {
310
+
311
+ if ( range === null || range . contains ( editRange . start ) ) {
312
+
313
+ // accumulate the changes in number of lines
314
+ // get the difference between the number of lines in the replacement text and
315
+ // that of the original text
316
+ this . lineDiff += this . getNumLines ( edit . text ) - ( editRange . end . line - editRange . start . line + 1 ) ;
296
317
return editor . edit ( ( editBuilder ) => {
297
318
editBuilder . replace (
298
319
editRange ,
@@ -310,13 +331,8 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
310
331
}
311
332
}
312
333
313
- private getSelectionRange ( document : TextDocument ) : Range {
314
- let editor = vscode . window . visibleTextEditors . find ( editor => editor . document === document ) ;
315
- if ( editor !== undefined ) {
316
- return editor . selection as Range ;
317
- }
318
-
319
- return null ;
334
+ private getNumLines ( text : string ) : number {
335
+ return text . split ( / \r ? \n / ) . length ;
320
336
}
321
337
322
338
private getSettings ( rule : string ) : any {
0 commit comments