@@ -49,6 +49,24 @@ interface MarkerCorrection {
49
49
edits : ScriptRegion [ ]
50
50
}
51
51
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
+
52
70
class PSDocumentFormattingEditProvider implements vscode . DocumentFormattingEditProvider {
53
71
private languageClient : LanguageClient ;
54
72
@@ -62,8 +80,7 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
62
80
// Should we expose this through settings?
63
81
private aggregateUndoStop : boolean ;
64
82
65
- constructor ( aggregateUndoStop : boolean )
66
- {
83
+ constructor ( aggregateUndoStop : boolean ) {
67
84
this . aggregateUndoStop = aggregateUndoStop ;
68
85
}
69
86
@@ -94,28 +111,20 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
94
111
settings : this . getSettings ( rule )
95
112
} )
96
113
. 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 ] ; } ) ;
98
115
99
116
// 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 ) ;
110
119
} ) ;
111
120
112
- // We cannot handle multiple edits on the same line hence we
121
+ // We cannot handle multiple edits at the same point hence we
113
122
// 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
114
124
if ( edits . length > 0 ) {
115
125
uniqueEdits . push ( edits [ 0 ] ) ;
116
126
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 ) {
119
128
uniqueEdits . push ( edit ) ;
120
129
}
121
130
}
@@ -157,12 +166,12 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
157
166
edit . endColumnNumber - 1 ) ,
158
167
edit . text ) ;
159
168
} ,
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
166
175
}
167
176
168
177
setLanguageClient ( languageClient : LanguageClient ) : void {
0 commit comments