Skip to content

Commit 7291539

Browse files
author
Kapil Borle
committed
Include startcolumnnumber in edit comparison
1 parent 88551de commit 7291539

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

src/features/DocumentFormatter.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ interface MarkerCorrection {
4949
edits: ScriptRegion[]
5050
}
5151

52+
function editComparer(leftOperand: ScriptRegion, rightOperand: ScriptRegion): number {
53+
if (leftOperand.startLineNumber < rightOperand.startLineNumber) {
54+
return -1;
55+
} else if (leftOperand.startLineNumber > rightOperand.startLineNumber) {
56+
return 1;
57+
} else {
58+
if (leftOperand.startColumnNumber < rightOperand.startColumnNumber) {
59+
return -1;
60+
}
61+
else if (leftOperand.startColumnNumber > rightOperand.startColumnNumber) {
62+
return 1;
63+
}
64+
else {
65+
return 0;
66+
}
67+
}
68+
}
69+
5270
class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider {
5371
private languageClient: LanguageClient;
5472

@@ -62,8 +80,7 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
6280
// Should we expose this through settings?
6381
private aggregateUndoStop: boolean;
6482

65-
constructor(aggregateUndoStop: boolean)
66-
{
83+
constructor(aggregateUndoStop: boolean) {
6784
this.aggregateUndoStop = aggregateUndoStop;
6885
}
6986

@@ -94,28 +111,20 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
94111
settings: this.getSettings(rule)
95112
})
96113
.then((result: ScriptFileMarkersRequestResultParams) => {
97-
edits = result.markers.map(m => { return m.correction.edits[0]; });
114+
edits = result.markers.map(marker => { return marker.correction.edits[0]; });
98115

99116
// sort in decending order of the edits
100-
edits.sort(function (a: ScriptRegion, b: ScriptRegion): number {
101-
let leftOperand: number = a.startLineNumber,
102-
rightOperand: number = b.startLineNumber;
103-
if (leftOperand < rightOperand) {
104-
return 1;
105-
} else if (leftOperand > rightOperand) {
106-
return -1;
107-
} else {
108-
return 0;
109-
}
117+
edits.sort((left: ScriptRegion, right: ScriptRegion) => {
118+
return -1 * editComparer(left, right);
110119
});
111120

112-
// We cannot handle multiple edits on the same line hence we
121+
// We cannot handle multiple edits at the same point hence we
113122
// filter the markers so that there is only one edit per line
123+
// This ideally should not happen but it is good to have some additional safeguard
114124
if (edits.length > 0) {
115125
uniqueEdits.push(edits[0]);
116126
for (let edit of edits.slice(1)) {
117-
if (edit.startLineNumber
118-
!== uniqueEdits[uniqueEdits.length - 1].startLineNumber) {
127+
if (editComparer(uniqueEdits[uniqueEdits.length - 1], edit) !== 0) {
119128
uniqueEdits.push(edit);
120129
}
121130
}
@@ -157,12 +166,12 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
157166
edit.endColumnNumber - 1),
158167
edit.text);
159168
},
160-
{
161-
undoStopAfter: undoStopAfter,
162-
undoStopBefore: undoStopBefore
163-
}).then((isEditApplied) => {
164-
return this.applyEdit(edits, markerIndex + 1, ruleIndex);
165-
}); // TODO handle rejection
169+
{
170+
undoStopAfter: undoStopAfter,
171+
undoStopBefore: undoStopBefore
172+
}).then((isEditApplied) => {
173+
return this.applyEdit(edits, markerIndex + 1, ruleIndex);
174+
}); // TODO handle rejection
166175
}
167176

168177
setLanguageClient(languageClient: LanguageClient): void {

0 commit comments

Comments
 (0)