@@ -51,7 +51,8 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
51
51
private languageClient : LanguageClient ;
52
52
private readonly ruleOrder : string [ ] = [
53
53
"PSPlaceCloseBrace" ,
54
- "PSPlaceOpenBrace" ] ;
54
+ "PSPlaceOpenBrace" ,
55
+ "PSUseConsistentIndentation" ] ;
55
56
56
57
provideDocumentFormattingEdits (
57
58
document : TextDocument ,
@@ -62,10 +63,10 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
62
63
// the edits in edit j s.t i < j (seems like a hard problem)
63
64
// or
64
65
// peform edits ourself and return an empty textedit array
65
- return this . applyEditsInOrder ( document , options , 0 ) ;
66
+ return this . executeRulesInOrder ( document , options , 0 ) ;
66
67
}
67
68
68
- applyEditsInOrder (
69
+ executeRulesInOrder (
69
70
document : TextDocument ,
70
71
options : FormattingOptions ,
71
72
index : number ) : Thenable < TextEdit [ ] > | TextEdit [ ] {
@@ -80,26 +81,40 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
80
81
81
82
// TODO modify undo stops to make sure all the edits
82
83
// 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 ) ;
85
98
86
99
// we do not return a valid array because our text edits
87
100
// need to be executed in a particular order and it is
88
101
// easier if we perform the edits ourselves
89
- return TextEdit [ 0 ] ;
102
+ } )
103
+ . then ( ( ) => {
104
+ return this . executeRulesInOrder ( document , options , index + 1 ) ;
90
105
} ) ;
91
106
} else {
92
107
return TextEdit [ 0 ] ;
93
108
}
94
109
}
95
110
96
- applyEdits ( markers : ScriptFileMarker [ ] , index : number ) : void {
111
+ applyEdits ( markers : ScriptFileMarker [ ] , index : number ) : Thenable < void > {
97
112
if ( index >= markers . length ) {
98
113
return ;
99
114
}
100
115
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 ) => {
103
118
editBuilder . replace (
104
119
new vscode . Range (
105
120
edit . startLineNumber - 1 ,
@@ -108,9 +123,8 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
108
123
edit . endColumnNumber - 1 ) ,
109
124
edit . text ) ;
110
125
} ) . then ( ( isEditApplied ) => {
111
- this . applyEdits ( markers , index ) ;
126
+ return this . applyEdits ( markers , index + 1 ) ;
112
127
} ) ; // TODO handle rejection
113
-
114
128
}
115
129
116
130
setLanguageClient ( languageClient : LanguageClient ) : void {
0 commit comments