@@ -31,10 +31,6 @@ import * as Settings from '../settings';
31
31
import * as Utils from '../utils' ;
32
32
import * as AnimatedStatusBar from '../controls/animatedStatusBar' ;
33
33
34
- export namespace ScriptFileMarkersRequest {
35
- export const type = new RequestType < any , any , void , void > ( "powerShell/getScriptFileMarkers" ) ;
36
- }
37
-
38
34
export namespace ScriptRegionRequest {
39
35
export const type = new RequestType < any , any , void , void > ( "powerShell/getScriptRegion" ) ;
40
36
}
@@ -50,29 +46,6 @@ interface ScriptRegionRequestResult {
50
46
scriptRegion : ScriptRegion ;
51
47
}
52
48
53
- // TODO move some of the common interface to a separate file?
54
- interface ScriptFileMarkersRequestParams {
55
- fileUri : string ;
56
- settings : any ;
57
- }
58
-
59
- interface ScriptFileMarkersRequestResultParams {
60
- markers : ScriptFileMarker [ ] ;
61
- }
62
-
63
- interface ScriptFileMarker {
64
- message : string ;
65
- level : ScriptFileMarkerLevel ;
66
- scriptRegion : ScriptRegion ;
67
- correction : MarkerCorrection ;
68
- }
69
-
70
- enum ScriptFileMarkerLevel {
71
- Information = 0 ,
72
- Warning ,
73
- Error
74
- }
75
-
76
49
interface ScriptRegion {
77
50
file : string ;
78
51
text : string ;
@@ -84,11 +57,6 @@ interface ScriptRegion {
84
57
endOffset : number ;
85
58
}
86
59
87
- interface MarkerCorrection {
88
- name : string ;
89
- edits : ScriptRegion [ ] ;
90
- }
91
-
92
60
function toRange ( scriptRegion : ScriptRegion ) : vscode . Range {
93
61
return new vscode . Range (
94
62
scriptRegion . startLineNumber - 1 ,
@@ -101,24 +69,6 @@ function toOneBasedPosition(position: Position): Position {
101
69
return position . translate ( { lineDelta : 1 , characterDelta : 1 } ) ;
102
70
}
103
71
104
- function editComparer ( leftOperand : ScriptRegion , rightOperand : ScriptRegion ) : number {
105
- if ( leftOperand . startLineNumber < rightOperand . startLineNumber ) {
106
- return - 1 ;
107
- } else if ( leftOperand . startLineNumber > rightOperand . startLineNumber ) {
108
- return 1 ;
109
- } else {
110
- if ( leftOperand . startColumnNumber < rightOperand . startColumnNumber ) {
111
- return - 1 ;
112
- }
113
- else if ( leftOperand . startColumnNumber > rightOperand . startColumnNumber ) {
114
- return 1 ;
115
- }
116
- else {
117
- return 0 ;
118
- }
119
- }
120
- }
121
-
122
72
class DocumentLocker {
123
73
private lockedDocuments : Object ;
124
74
@@ -171,31 +121,16 @@ class PSDocumentFormattingEditProvider implements
171
121
DocumentFormattingEditProvider ,
172
122
DocumentRangeFormattingEditProvider ,
173
123
OnTypeFormattingEditProvider {
124
+
174
125
private static documentLocker = new DocumentLocker ( ) ;
175
126
private static statusBarTracker = new Object ( ) ;
176
127
private languageClient : LanguageClient ;
177
- private lineDiff : number ;
178
-
179
- // The order in which the rules will be executed starting from the first element.
180
- private readonly ruleOrder : string [ ] = [
181
- "PSPlaceCloseBrace" ,
182
- "PSPlaceOpenBrace" ,
183
- "PSUseConsistentWhitespace" ,
184
- "PSUseConsistentIndentation" ,
185
- "PSAlignAssignmentStatement" ]
186
-
187
- // Allows edits to be undone and redone is a single step.
188
- // It is usefuld to have undo stops after every edit while debugging
189
- // hence we keep this as an option but set it true by default.
190
- private aggregateUndoStop : boolean ;
191
128
192
129
private get emptyPromise ( ) : Promise < TextEdit [ ] > {
193
130
return Promise . resolve ( TextEdit [ 0 ] ) ;
194
131
}
195
132
196
- constructor ( aggregateUndoStop = true ) {
197
- this . aggregateUndoStop = aggregateUndoStop ;
198
- this . lineDiff = 0 ;
133
+ constructor ( ) {
199
134
}
200
135
201
136
provideDocumentFormattingEdits (
@@ -300,13 +235,6 @@ class PSDocumentFormattingEditProvider implements
300
235
} ) ;
301
236
}
302
237
303
- private snapRangeToEdges ( range : Range , document : TextDocument ) : Range {
304
- return range . with ( {
305
- start : range . start . with ( { character : 0 } ) ,
306
- end : document . lineAt ( range . end . line ) . range . end
307
- } ) ;
308
- }
309
-
310
238
private getEditor ( document : TextDocument ) : TextEditor {
311
239
return Window . visibleTextEditors . find ( ( e , n , obj ) => { return e . document === document ; } ) ;
312
240
}
@@ -319,160 +247,6 @@ class PSDocumentFormattingEditProvider implements
319
247
PSDocumentFormattingEditProvider . documentLocker . lock ( document , unlockWhenDone ) ;
320
248
}
321
249
322
- private executeRulesInOrder (
323
- editor : TextEditor ,
324
- range : Range ,
325
- options : FormattingOptions ,
326
- index : number ) : Thenable < TextEdit [ ] > {
327
- if ( this . languageClient !== null && index < this . ruleOrder . length ) {
328
- let rule : string = this . ruleOrder [ index ] ;
329
- let uniqueEdits : ScriptRegion [ ] = [ ] ;
330
- let document : TextDocument = editor . document ;
331
- let edits : ScriptRegion [ ] ;
332
-
333
- return this . languageClient . sendRequest (
334
- ScriptFileMarkersRequest . type ,
335
- {
336
- fileUri : document . uri . toString ( ) ,
337
- settings : this . getSettings ( rule )
338
- } )
339
- . then ( ( result : ScriptFileMarkersRequestResultParams ) => {
340
- edits = result . markers . map ( marker => { return marker . correction . edits [ 0 ] ; } ) ;
341
-
342
- // sort in decending order of the edits
343
- edits . sort ( ( left : ScriptRegion , right : ScriptRegion ) => {
344
- return - 1 * editComparer ( left , right ) ;
345
- } ) ;
346
-
347
-
348
- // we need to update the range as the edits might
349
- // have changed the original layout
350
- if ( range !== null ) {
351
- if ( this . lineDiff !== 0 ) {
352
- range = range . with ( { end : range . end . translate ( { lineDelta : this . lineDiff } ) } ) ;
353
- }
354
-
355
- // extend the range such that it starts at the first character of the
356
- // start line of the range.
357
- range = this . snapRangeToEdges ( range , document ) ;
358
-
359
- // filter edits that are contained in the input range
360
- edits = edits . filter ( edit => range . contains ( toRange ( edit ) . start ) ) ;
361
- }
362
-
363
- // We cannot handle multiple edits at the same point hence we
364
- // filter the markers so that there is only one edit per region
365
- if ( edits . length > 0 ) {
366
- uniqueEdits . push ( edits [ 0 ] ) ;
367
- for ( let edit of edits . slice ( 1 ) ) {
368
- let lastEdit : ScriptRegion = uniqueEdits [ uniqueEdits . length - 1 ] ;
369
- if ( lastEdit . startLineNumber !== edit . startLineNumber
370
- || ( edit . startColumnNumber + edit . text . length ) < lastEdit . startColumnNumber ) {
371
- uniqueEdits . push ( edit ) ;
372
- }
373
- }
374
- }
375
-
376
- // reset line difference to 0
377
- this . lineDiff = 0 ;
378
-
379
- // we do not return a valid array because our text edits
380
- // need to be executed in a particular order and it is
381
- // easier if we perform the edits ourselves
382
- return this . applyEdit ( editor , uniqueEdits , 0 , index ) ;
383
- } )
384
- . then ( ( ) => {
385
- // execute the same rule again if we left out violations
386
- // on the same line
387
- let newIndex : number = index + 1 ;
388
- if ( uniqueEdits . length !== edits . length ) {
389
- newIndex = index ;
390
- }
391
-
392
- return this . executeRulesInOrder ( editor , range , options , newIndex ) ;
393
- } ) ;
394
- } else {
395
- return this . emptyPromise ;
396
- }
397
- }
398
-
399
- private applyEdit (
400
- editor : TextEditor ,
401
- edits : ScriptRegion [ ] ,
402
- markerIndex : number ,
403
- ruleIndex : number ) : Thenable < void > {
404
- if ( markerIndex >= edits . length ) {
405
- return ;
406
- }
407
-
408
- let undoStopAfter = ! this . aggregateUndoStop || ( ruleIndex === this . ruleOrder . length - 1 && markerIndex === edits . length - 1 ) ;
409
- let undoStopBefore = ! this . aggregateUndoStop || ( ruleIndex === 0 && markerIndex === 0 ) ;
410
- let edit : ScriptRegion = edits [ markerIndex ] ;
411
- let editRange : Range = toRange ( edit ) ;
412
-
413
-
414
- // accumulate the changes in number of lines
415
- // get the difference between the number of lines in the replacement text and
416
- // that of the original text
417
- this . lineDiff += this . getNumLines ( edit . text ) - ( editRange . end . line - editRange . start . line + 1 ) ;
418
- return editor . edit ( ( editBuilder ) => {
419
- editBuilder . replace (
420
- editRange ,
421
- edit . text ) ;
422
- } ,
423
- {
424
- undoStopAfter : undoStopAfter ,
425
- undoStopBefore : undoStopBefore
426
- } ) . then ( ( isEditApplied ) => {
427
- return this . applyEdit ( editor , edits , markerIndex + 1 , ruleIndex ) ;
428
- } ) ; // TODO handle rejection
429
- }
430
-
431
- private getNumLines ( text : string ) : number {
432
- return text . split ( / \r ? \n / ) . length ;
433
- }
434
-
435
- private getSettings ( rule : string ) : any {
436
- let psSettings : Settings . ISettings = Settings . load ( Utils . PowerShellLanguageId ) ;
437
- let ruleSettings = new Object ( ) ;
438
- ruleSettings [ "Enable" ] = true ;
439
-
440
- switch ( rule ) {
441
- case "PSPlaceOpenBrace" :
442
- ruleSettings [ "OnSameLine" ] = psSettings . codeFormatting . openBraceOnSameLine ;
443
- ruleSettings [ "NewLineAfter" ] = psSettings . codeFormatting . newLineAfterOpenBrace ;
444
- ruleSettings [ "IgnoreOneLineBlock" ] = psSettings . codeFormatting . ignoreOneLineBlock ;
445
- break ;
446
-
447
- case "PSPlaceCloseBrace" :
448
- ruleSettings [ "IgnoreOneLineBlock" ] = psSettings . codeFormatting . ignoreOneLineBlock ;
449
- ruleSettings [ "NewLineAfter" ] = psSettings . codeFormatting . newLineAfterCloseBrace ;
450
- break ;
451
-
452
- case "PSUseConsistentIndentation" :
453
- ruleSettings [ "IndentationSize" ] = vscode . workspace . getConfiguration ( "editor" ) . get < number > ( "tabSize" ) ;
454
- break ;
455
-
456
- case "PSUseConsistentWhitespace" :
457
- ruleSettings [ "CheckOpenBrace" ] = psSettings . codeFormatting . whitespaceBeforeOpenBrace ;
458
- ruleSettings [ "CheckOpenParen" ] = psSettings . codeFormatting . whitespaceBeforeOpenParen ;
459
- ruleSettings [ "CheckOperator" ] = psSettings . codeFormatting . whitespaceAroundOperator ;
460
- ruleSettings [ "CheckSeparator" ] = psSettings . codeFormatting . whitespaceAfterSeparator ;
461
- break ;
462
-
463
- case "PSAlignAssignmentStatement" :
464
- ruleSettings [ "CheckHashtable" ] = psSettings . codeFormatting . alignPropertyValuePairs ;
465
- break ;
466
-
467
- default :
468
- break ;
469
- }
470
-
471
- let settings : Object = new Object ( ) ;
472
- settings [ rule ] = ruleSettings ;
473
- return settings ;
474
- }
475
-
476
250
private getEditorSettings ( ) : { insertSpaces : boolean , tabSize : number } {
477
251
let editorConfiguration = vscode . workspace . getConfiguration ( "editor" ) ;
478
252
return {
0 commit comments