Skip to content

Commit 755cc65

Browse files
author
Kapil Borle
committed
Add one stop undo/redo option to code formatting
1 parent 9c61aa4 commit 755cc65

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/features/DocumentFormatter.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,22 @@ interface MarkerCorrection {
5151

5252
class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider {
5353
private languageClient: LanguageClient;
54+
55+
// The order in which the rules will be executed starting from the first element.
5456
private readonly ruleOrder: string[] = [
5557
"PSPlaceCloseBrace",
5658
"PSPlaceOpenBrace",
5759
"PSUseConsistentIndentation"];
5860

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+
5970
provideDocumentFormattingEdits(
6071
document: TextDocument,
6172
options: FormattingOptions,
@@ -111,12 +122,14 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
111122
}
112123
}
113124

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) {
116127
return;
117128
}
118129

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];
120133
return Window.activeTextEditor.edit((editBuilder) => {
121134
editBuilder.replace(
122135
new vscode.Range(
@@ -125,8 +138,12 @@ class PSDocumentFormattingEditProvider implements vscode.DocumentFormattingEditP
125138
edit.endLineNumber - 1,
126139
edit.endColumnNumber - 1),
127140
edit.text);
141+
},
142+
{
143+
undoStopAfter: undoStopAfter,
144+
undoStopBefore: undoStopBefore
128145
}).then((isEditApplied) => {
129-
return this.applyEdits(markers, index + 1);
146+
return this.applyEdit(markers, markerIndex + 1, ruleIndex);
130147
}); // TODO handle rejection
131148
}
132149

@@ -175,7 +192,7 @@ export class DocumentFormatterFeature implements IFeature {
175192
private documentFormattingEditProvider: PSDocumentFormattingEditProvider;
176193

177194
constructor() {
178-
this.documentFormattingEditProvider = new PSDocumentFormattingEditProvider();
195+
this.documentFormattingEditProvider = new PSDocumentFormattingEditProvider(true);
179196
this.disposable = vscode.languages.registerDocumentFormattingEditProvider(
180197
"powershell",
181198
this.documentFormattingEditProvider);

0 commit comments

Comments
 (0)