Skip to content

Commit b62e1c5

Browse files
author
Kapil Borle
committed
Fix in order edit application
1 parent b3236a4 commit b62e1c5

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/features/DocumentFormatter.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
5151
private languageClient: LanguageClient;
5252
private readonly ruleOrder: string[] = [
5353
"PSPlaceCloseBrace",
54-
"PSPlaceOpenBrace"];
54+
"PSPlaceOpenBrace",
55+
"PSUseConsistentIndentation"];
5556

5657
provideDocumentFormattingEdits(
5758
document: TextDocument,
@@ -62,10 +63,10 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
6263
// the edits in edit j s.t i < j (seems like a hard problem)
6364
// or
6465
// peform edits ourself and return an empty textedit array
65-
return this.applyEditsInOrder(document, options, 0);
66+
return this.executeRulesInOrder(document, options, 0);
6667
}
6768

68-
applyEditsInOrder(
69+
executeRulesInOrder(
6970
document: TextDocument,
7071
options: FormattingOptions,
7172
index: number): Thenable<TextEdit[]> | TextEdit[] {
@@ -80,26 +81,40 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
8081

8182
// TODO modify undo stops to make sure all the edits
8283
// can be undone and redone in a single step
83-
this.applyEdits(result.markers, 0);
84-
this.applyEditsInOrder(document, options, ++index);
84+
85+
// sort in decending order of the edits
86+
result.markers.sort(function(a: ScriptFileMarker, b: ScriptFileMarker): number {
87+
let leftOperand: number = a.correction.edits[0].startLineNumber,
88+
rightOperand: number = b.correction.edits[0].startLineNumber;
89+
if (leftOperand < rightOperand) {
90+
return 1;
91+
} else if (leftOperand > rightOperand) {
92+
return -1;
93+
} else {
94+
return 0;
95+
}
96+
});
97+
return this.applyEdits(result.markers, 0);
8598

8699
// we do not return a valid array because our text edits
87100
// need to be executed in a particular order and it is
88101
// easier if we perform the edits ourselves
89-
return TextEdit[0];
102+
})
103+
.then(() => {
104+
return this.executeRulesInOrder(document, options, index + 1);
90105
});
91106
} else {
92107
return TextEdit[0];
93108
}
94109
}
95110

96-
applyEdits(markers: ScriptFileMarker[], index: number): void {
111+
applyEdits(markers: ScriptFileMarker[], index: number): Thenable<void> {
97112
if (index >= markers.length) {
98113
return;
99114
}
100115

101-
let edit: ScriptRegion = markers[index++].correction.edits[0];
102-
Window.activeTextEditor.edit((editBuilder) => {
116+
let edit: ScriptRegion = markers[index].correction.edits[0];
117+
return Window.activeTextEditor.edit((editBuilder) => {
103118
editBuilder.replace(
104119
new vscode.Range(
105120
edit.startLineNumber - 1,
@@ -108,9 +123,8 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
108123
edit.endColumnNumber - 1),
109124
edit.text);
110125
}).then((isEditApplied) => {
111-
this.applyEdits(markers, index);
126+
return this.applyEdits(markers, index + 1);
112127
}); // TODO handle rejection
113-
114128
}
115129

116130
setLanguageClient(languageClient: LanguageClient): void {

0 commit comments

Comments
 (0)