@@ -12,7 +12,8 @@ import {
1212 DocumentFormattingEditProvider ,
1313 DocumentRangeFormattingEditProvider ,
1414 Range ,
15- TextEditor
15+ TextEditor ,
16+ TextLine
1617} from 'vscode' ;
1718import { LanguageClient , RequestType } from 'vscode-languageclient' ;
1819import Window = vscode . window ;
@@ -134,6 +135,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
134135 private static documentLocker = new DocumentLocker ( ) ;
135136 private static statusBarTracker = new Object ( ) ;
136137 private languageClient : LanguageClient ;
138+ private lineDiff : number ;
137139
138140 // The order in which the rules will be executed starting from the first element.
139141 private readonly ruleOrder : string [ ] = [
@@ -153,6 +155,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
153155
154156 constructor ( aggregateUndoStop = true ) {
155157 this . aggregateUndoStop = aggregateUndoStop ;
158+ this . lineDiff = 0 ;
156159 }
157160
158161 provideDocumentFormattingEdits (
@@ -195,6 +198,12 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
195198 PSDocumentFormattingEditProvider . disposeAllStatusBars ( ) ;
196199 }
197200
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+
198207 private getEditor ( document : TextDocument ) : TextEditor {
199208 return Window . visibleTextEditors . find ( ( e , n , obj ) => { return e . document === document ; } ) ;
200209 }
@@ -248,12 +257,18 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
248257 // we need to update the range as the edits might
249258 // have changed the original layout
250259 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 } ) } ) ;
254262 }
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 ) ;
255267 }
256268
269+ // reset line difference to 0
270+ this . lineDiff = 0 ;
271+
257272 // we do not return a valid array because our text edits
258273 // need to be executed in a particular order and it is
259274 // easier if we perform the edits ourselves
@@ -292,7 +307,13 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
292307 edit . startColumnNumber - 1 ,
293308 edit . endLineNumber - 1 ,
294309 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 ) ;
296317 return editor . edit ( ( editBuilder ) => {
297318 editBuilder . replace (
298319 editRange ,
@@ -310,13 +331,8 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
310331 }
311332 }
312333
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 ;
320336 }
321337
322338 private getSettings ( rule : string ) : any {
0 commit comments