From 96e49d47226fa589c90d9575b2e8d5c78e870904 Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Thu, 30 Mar 2017 14:46:09 -0700 Subject: [PATCH 1/2] Filter edits contained in formatting range --- src/features/DocumentFormatter.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/features/DocumentFormatter.ts b/src/features/DocumentFormatter.ts index 4662484618..6efe5caa88 100644 --- a/src/features/DocumentFormatter.ts +++ b/src/features/DocumentFormatter.ts @@ -310,18 +310,6 @@ class PSDocumentFormattingEditProvider implements return -1 * editComparer(left, right); }); - // We cannot handle multiple edits at the same point hence we - // filter the markers so that there is only one edit per region - if (edits.length > 0) { - uniqueEdits.push(edits[0]); - for (let edit of edits.slice(1)) { - let lastEdit: ScriptRegion = uniqueEdits[uniqueEdits.length - 1]; - if (lastEdit.startLineNumber !== edit.startLineNumber - || (edit.startColumnNumber + edit.text.length) < lastEdit.startColumnNumber) { - uniqueEdits.push(edit); - } - } - } // we need to update the range as the edits might // have changed the original layout @@ -333,6 +321,22 @@ class PSDocumentFormattingEditProvider implements // extend the range such that it starts at the first character of the // start line of the range. range = this.snapRangeToEdges(range, document); + + // filter edits that are contained in the input range + edits = edits.filter(edit => range.contains(toRange(edit).start)); + } + + // We cannot handle multiple edits at the same point hence we + // filter the markers so that there is only one edit per region + if (edits.length > 0) { + uniqueEdits.push(edits[0]); + for (let edit of edits.slice(1)) { + let lastEdit: ScriptRegion = uniqueEdits[uniqueEdits.length - 1]; + if (lastEdit.startLineNumber !== edit.startLineNumber + || (edit.startColumnNumber + edit.text.length) < lastEdit.startColumnNumber) { + uniqueEdits.push(edit); + } + } } // reset line difference to 0 From 03475392d23622d47f561cb20008a3799d373b49 Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Thu, 30 Mar 2017 14:50:43 -0700 Subject: [PATCH 2/2] Remove edit filtering from applyedit method --- src/features/DocumentFormatter.ts | 40 +++++++++++++------------------ 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/features/DocumentFormatter.ts b/src/features/DocumentFormatter.ts index 6efe5caa88..17e4628796 100644 --- a/src/features/DocumentFormatter.ts +++ b/src/features/DocumentFormatter.ts @@ -345,7 +345,7 @@ class PSDocumentFormattingEditProvider implements // we do not return a valid array because our text edits // need to be executed in a particular order and it is // easier if we perform the edits ourselves - return this.applyEdit(editor, uniqueEdits, range, 0, index); + return this.applyEdit(editor, uniqueEdits, 0, index); }) .then(() => { // execute the same rule again if we left out violations @@ -365,7 +365,6 @@ class PSDocumentFormattingEditProvider implements private applyEdit( editor: TextEditor, edits: ScriptRegion[], - range: Range, markerIndex: number, ruleIndex: number): Thenable { if (markerIndex >= edits.length) { @@ -377,27 +376,22 @@ class PSDocumentFormattingEditProvider implements let edit: ScriptRegion = edits[markerIndex]; let editRange: Range = toRange(edit); - if (range === null || range.contains(editRange.start)) { - - // accumulate the changes in number of lines - // get the difference between the number of lines in the replacement text and - // that of the original text - this.lineDiff += this.getNumLines(edit.text) - (editRange.end.line - editRange.start.line + 1); - return editor.edit((editBuilder) => { - editBuilder.replace( - editRange, - edit.text); - }, - { - undoStopAfter: undoStopAfter, - undoStopBefore: undoStopBefore - }).then((isEditApplied) => { - return this.applyEdit(editor, edits, range, markerIndex + 1, ruleIndex); - }); // TODO handle rejection - } - else { - return this.applyEdit(editor, edits, range, markerIndex + 1, ruleIndex); - } + + // accumulate the changes in number of lines + // get the difference between the number of lines in the replacement text and + // that of the original text + this.lineDiff += this.getNumLines(edit.text) - (editRange.end.line - editRange.start.line + 1); + return editor.edit((editBuilder) => { + editBuilder.replace( + editRange, + edit.text); + }, + { + undoStopAfter: undoStopAfter, + undoStopBefore: undoStopBefore + }).then((isEditApplied) => { + return this.applyEdit(editor, edits, markerIndex + 1, ruleIndex); + }); // TODO handle rejection } private getNumLines(text: string): number {