@@ -51,11 +51,22 @@ interface MarkerCorrection {
51
51
52
52
class PSDocumentFormattingEditProvider implements vscode . DocumentFormattingEditProvider {
53
53
private languageClient : LanguageClient ;
54
+
55
+ // The order in which the rules will be executed starting from the first element.
54
56
private readonly ruleOrder : string [ ] = [
55
57
"PSPlaceCloseBrace" ,
56
58
"PSPlaceOpenBrace" ,
57
59
"PSUseConsistentIndentation" ] ;
58
60
61
+ // Allows edits to be undone and redone is a single step.
62
+ // Should we expose this through settings?
63
+ private aggregateUndoStop : boolean ;
64
+
65
+ constructor ( aggregateUndoStop : boolean )
66
+ {
67
+ this . aggregateUndoStop = aggregateUndoStop ;
68
+ }
69
+
59
70
provideDocumentFormattingEdits (
60
71
document : TextDocument ,
61
72
options : FormattingOptions ,
@@ -111,12 +122,14 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
111
122
}
112
123
}
113
124
114
- applyEdits ( markers : ScriptFileMarker [ ] , index : number ) : Thenable < void > {
115
- if ( index >= markers . length ) {
125
+ applyEdit ( markers : ScriptFileMarker [ ] , markerIndex : number , ruleIndex : number ) : Thenable < void > {
126
+ if ( markerIndex >= markers . length ) {
116
127
return ;
117
128
}
118
129
119
- let edit : ScriptRegion = markers [ index ] . correction . edits [ 0 ] ;
130
+ let undoStopAfter = ! this . aggregateUndoStop || ( ruleIndex === this . ruleOrder . length - 1 && markerIndex === markers . length - 1 ) ;
131
+ let undoStopBefore = ! this . aggregateUndoStop || ( ruleIndex === 0 && markerIndex === 0 ) ;
132
+ let edit : ScriptRegion = markers [ markerIndex ] . correction . edits [ 0 ] ;
120
133
return Window . activeTextEditor . edit ( ( editBuilder ) => {
121
134
editBuilder . replace (
122
135
new vscode . Range (
@@ -125,8 +138,12 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
125
138
edit . endLineNumber - 1 ,
126
139
edit . endColumnNumber - 1 ) ,
127
140
edit . text ) ;
141
+ } ,
142
+ {
143
+ undoStopAfter : undoStopAfter ,
144
+ undoStopBefore : undoStopBefore
128
145
} ) . then ( ( isEditApplied ) => {
129
- return this . applyEdits ( markers , index + 1 ) ;
146
+ return this . applyEdit ( markers , markerIndex + 1 , ruleIndex ) ;
130
147
} ) ; // TODO handle rejection
131
148
}
132
149
@@ -175,7 +192,7 @@ export class DocumentFormatterFeature implements IFeature {
175
192
private documentFormattingEditProvider : PSDocumentFormattingEditProvider ;
176
193
177
194
constructor ( ) {
178
- this . documentFormattingEditProvider = new PSDocumentFormattingEditProvider ( ) ;
195
+ this . documentFormattingEditProvider = new PSDocumentFormattingEditProvider ( true ) ;
179
196
this . disposable = vscode . languages . registerDocumentFormattingEditProvider (
180
197
"powershell" ,
181
198
this . documentFormattingEditProvider ) ;
0 commit comments