Skip to content

Commit 9b89cca

Browse files
committed
Working on hooking up revert properly for custom editors
1 parent f515fdc commit 9b89cca

5 files changed

Lines changed: 29 additions & 18 deletions

File tree

src/vs/vscode.proposed.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ declare module 'vscode' {
12651265
*
12661266
* @param edit Array of edits. Sorted from oldest to most recent.
12671267
*/
1268-
applyEdits(edits: any[]): Thenable<void>;
1268+
applyEdits(edits: readonly any[]): Thenable<void>;
12691269

12701270
/**
12711271
* Undo a set of edits.
@@ -1274,7 +1274,7 @@ declare module 'vscode' {
12741274
*
12751275
* @param edit Array of edits. Sorted from most recent to oldest.
12761276
*/
1277-
undoEdits(edits: any[]): Thenable<void>;
1277+
undoEdits(edits: readonly any[]): Thenable<void>;
12781278
}
12791279

12801280
export interface WebviewEditorProvider {

src/vs/workbench/api/browser/mainThreadWebview.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma
273273

274274
const model = await this._customEditorService.models.loadOrCreate(webviewInput.getResource(), webviewInput.viewType);
275275

276-
model.onUndo(edit => { this._proxy.$undoEdits(handle, [edit]); });
277-
model.onRedo(edit => { this._proxy.$redoEdits(handle, [edit]); });
276+
model.onUndo(edits => { this._proxy.$undoEdits(handle, edits); });
277+
model.onRedo(edits => { this._proxy.$redoEdits(handle, edits); });
278278
model.onWillSave(e => { e.waitUntil(this._proxy.$onSave(handle)); });
279279

280280
webviewInput.onDispose(() => {

src/vs/workbench/api/common/extHost.protocol.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ export interface ExtHostWebviewsShape {
592592
$onDidDisposeWebviewPanel(handle: WebviewPanelHandle): Promise<void>;
593593
$deserializeWebviewPanel(newWebviewHandle: WebviewPanelHandle, viewType: string, title: string, state: any, position: EditorViewColumn, options: modes.IWebviewOptions & modes.IWebviewPanelOptions): Promise<void>;
594594
$resolveWebviewEditor(resource: UriComponents, newWebviewHandle: WebviewPanelHandle, viewType: string, title: string, position: EditorViewColumn, options: modes.IWebviewOptions & modes.IWebviewPanelOptions): Promise<void>;
595-
$undoEdits(handle: WebviewPanelHandle, edits: string[]): void;
596-
$redoEdits(handle: WebviewPanelHandle, edits: string[]): void;
595+
$undoEdits(handle: WebviewPanelHandle, edits: readonly any[]): void;
596+
$redoEdits(handle: WebviewPanelHandle, edits: readonly any[]): void;
597597
$onSave(handle: WebviewPanelHandle): Promise<void>;
598598
}
599599

src/vs/workbench/contrib/customEditor/common/customEditor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface ICustomEditorService {
3838
promptOpenWith(resource: URI, options?: ITextEditorOptions, group?: IEditorGroup): Promise<IEditor | undefined>;
3939
}
4040

41-
export type CustomEditorEdit = string;
41+
export type CustomEditorEdit = unknown;
4242

4343
export interface ICustomEditorModelManager {
4444
get(resource: URI, viewType: string): ICustomEditorModel | undefined;
@@ -49,8 +49,8 @@ export interface ICustomEditorModelManager {
4949
}
5050

5151
export interface ICustomEditorModel extends IWorkingCopy {
52-
readonly onUndo: Event<CustomEditorEdit>;
53-
readonly onRedo: Event<CustomEditorEdit>;
52+
readonly onUndo: Event<readonly CustomEditorEdit[]>;
53+
readonly onRedo: Event<readonly CustomEditorEdit[]>;
5454
readonly onWillSave: Event<{ waitUntil: (until: Promise<any>) => void }>;
5555

5656
undo(): void;

src/vs/workbench/contrib/customEditor/common/customEditorModel.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ export class CustomEditorModel extends Disposable implements ICustomEditorModel
4141

4242
//#endregion
4343

44-
protected readonly _onUndo = this._register(new Emitter<CustomEditorEdit>());
45-
readonly onUndo: Event<CustomEditorEdit> = this._onUndo.event;
44+
protected readonly _onUndo = this._register(new Emitter<readonly CustomEditorEdit[]>());
45+
readonly onUndo = this._onUndo.event;
4646

47-
protected readonly _onRedo = this._register(new Emitter<CustomEditorEdit>());
48-
readonly onRedo: Event<CustomEditorEdit> = this._onRedo.event;
47+
protected readonly _onRedo = this._register(new Emitter<readonly CustomEditorEdit[]>());
48+
readonly onRedo = this._onRedo.event;
4949

5050
protected readonly _onWillSave = this._register(new Emitter<{ waitUntil: (until: Promise<any>) => void }>());
5151
readonly onWillSave = this._onWillSave.event;
@@ -72,11 +72,22 @@ export class CustomEditorModel extends Disposable implements ICustomEditorModel
7272
return true;
7373
}
7474

75-
public async revert(options?: IRevertOptions) {
76-
while (this._currentEditIndex > 0) {
77-
this.undo();
75+
public async revert(_options?: IRevertOptions) {
76+
if (this._currentEditIndex === this._savePoint) {
77+
return true;
7878
}
7979

80+
if (this._currentEditIndex >= this._savePoint) {
81+
const editsToUndo = this._edits.slice(this._savePoint, this._currentEditIndex);
82+
this._onUndo.fire(editsToUndo.reverse());
83+
} else if (this._currentEditIndex < this._savePoint) {
84+
const editsToRedo = this._edits.slice(this._currentEditIndex, this._savePoint);
85+
this._onRedo.fire(editsToRedo);
86+
}
87+
88+
this._currentEditIndex = this._savePoint;
89+
this._edits.splice(this._currentEditIndex + 1, this._edits.length - this._currentEditIndex);
90+
this.updateDirty();
8091
return true;
8192
}
8293

@@ -88,7 +99,7 @@ export class CustomEditorModel extends Disposable implements ICustomEditorModel
8899

89100
const undoneEdit = this._edits[this._currentEditIndex];
90101
--this._currentEditIndex;
91-
this._onUndo.fire(undoneEdit);
102+
this._onUndo.fire([undoneEdit]);
92103

93104
this.updateDirty();
94105
}
@@ -101,7 +112,7 @@ export class CustomEditorModel extends Disposable implements ICustomEditorModel
101112

102113
++this._currentEditIndex;
103114
const redoneEdit = this._edits[this._currentEditIndex];
104-
this._onRedo.fire(redoneEdit);
115+
this._onRedo.fire([redoneEdit]);
105116

106117
this.updateDirty();
107118
}

0 commit comments

Comments
 (0)