Skip to content

Commit d4809fc

Browse files
author
Kapil Borle
committed
Handle multiple violations on one line
1 parent 755cc65 commit d4809fc

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/features/DocumentFormatter.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
8585
index: number): Thenable<TextEdit[]> | TextEdit[] {
8686
if (this.languageClient !== null && index < this.ruleOrder.length) {
8787
let rule = this.ruleOrder[index];
88+
let uniqueMarkers: ScriptFileMarker[] = [];
89+
let markers: ScriptFileMarker[];
8890
return this.languageClient.sendRequest(
8991
ScriptFileMarkersRequest.type,
9092
{
9193
filePath: document.fileName,
9294
settings: this.getSettings(rule)
9395
})
9496
.then((result: ScriptFileMarkersRequestResultParams) => {
95-
96-
// TODO modify undo stops to make sure all the edits
97-
// can be undone and redone in a single step
97+
markers = result.markers;
9898

9999
// sort in decending order of the edits
100-
result.markers.sort(function (a: ScriptFileMarker, b: ScriptFileMarker): number {
100+
markers.sort(function (a: ScriptFileMarker, b: ScriptFileMarker): number {
101101
let leftOperand: number = a.correction.edits[0].startLineNumber,
102102
rightOperand: number = b.correction.edits[0].startLineNumber;
103103
if (leftOperand < rightOperand) {
@@ -108,13 +108,31 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
108108
return 0;
109109
}
110110
});
111-
return this.applyEdits(result.markers, 0);
111+
112+
// We cannot handle multiple edits on the same line hence we
113+
// filter the markers so that there is only one edit per line
114+
if (markers.length > 0) {
115+
uniqueMarkers.push(markers[0]);
116+
for (let marker of markers.slice(1)) {
117+
if (marker.correction.edits[0].startLineNumber
118+
!== uniqueMarkers[uniqueMarkers.length - 1].correction.edits[0].startLineNumber) {
119+
uniqueMarkers.push(marker);
120+
}
121+
}
122+
}
112123

113124
// we do not return a valid array because our text edits
114125
// need to be executed in a particular order and it is
115126
// easier if we perform the edits ourselves
127+
return this.applyEdit(uniqueMarkers, 0, index);
116128
})
117129
.then(() => {
130+
131+
// execute the same rule again if we left out violations
132+
// on the same line
133+
if (uniqueMarkers.length !== markers.length) {
134+
return this.executeRulesInOrder(document, options, index);
135+
}
118136
return this.executeRulesInOrder(document, options, index + 1);
119137
});
120138
} else {

0 commit comments

Comments
 (0)