diff --git a/extensions/image-preview/src/preview.ts b/extensions/image-preview/src/preview.ts index fc43a136a01..38f6f5231b5 100644 --- a/extensions/image-preview/src/preview.ts +++ b/extensions/image-preview/src/preview.ts @@ -27,8 +27,8 @@ export class PreviewManager implements vscode.CustomEditorProvider { private readonly zoomStatusBarEntry: ZoomStatusBarEntry, ) { } - public async resolveCustomDocument(_document: vscode.CustomDocument): Promise { - return {}; + public async resolveCustomDocument(_document: vscode.CustomDocument): Promise { + // noop } public async resolveCustomEditor( diff --git a/extensions/markdown-language-features/src/features/previewManager.ts b/extensions/markdown-language-features/src/features/previewManager.ts index a6fc8ec528c..a9403313931 100644 --- a/extensions/markdown-language-features/src/features/previewManager.ts +++ b/extensions/markdown-language-features/src/features/previewManager.ts @@ -148,8 +148,8 @@ export class MarkdownPreviewManager extends Disposable implements vscode.Webview this.registerDynamicPreview(preview); } - public async resolveCustomDocument(_document: vscode.CustomDocument): Promise { - return {}; + public async resolveCustomDocument(_document: vscode.CustomDocument): Promise { + // noop } public async resolveCustomTextEditor( diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index 421a285c25c..262702d2cc4 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -1222,78 +1222,88 @@ declare module 'vscode' { // - Should we expose edits? // - More properties from `TextDocument`? - /** - * Defines the capabilities of a custom webview editor. - */ - interface CustomEditorCapabilities { - /** - * Defines the editing capability of a custom webview document. - * - * When not provided, the document is considered readonly. - */ - readonly editing?: CustomEditorEditingCapability; - } - /** * Defines the editing capability of a custom webview editor. This allows the webview editor to hook into standard * editor events such as `undo` or `save`. * * @param EditType Type of edits. */ - interface CustomEditorEditingCapability { + interface CustomEditorEditingDelegate { /** * Save the resource. * + * @param document Document to save. * @param cancellation Token that signals the save is no longer required (for example, if another save was triggered). * * @return Thenable signaling that the save has completed. */ - save(cancellation: CancellationToken): Thenable; + save(document: CustomDocument, cancellation: CancellationToken): Thenable; /** * Save the existing resource at a new path. * + * @param document Document to save. * @param targetResource Location to save to. * * @return Thenable signaling that the save has completed. */ - saveAs(targetResource: Uri): Thenable; + saveAs(document: CustomDocument, targetResource: Uri): Thenable; /** * Event triggered by extensions to signal to VS Code that an edit has occurred. */ - readonly onDidEdit: Event; + readonly onDidEdit: Event<{ + /** + * Document the edit is for. + */ + readonly document: CustomDocument; + + /** + * Object that describes the edit. + * + * Edit objects are passed back to your extension in `undoEdits`, `applyEdits`, and `revert`. + */ + readonly edit: EditType; + + /** + * Display name describing the edit. + */ + readonly label?: string; + }>; /** * Apply a set of edits. * * Note that is not invoked when `onDidEdit` is called because `onDidEdit` implies also updating the view to reflect the edit. * + * @param document Document to apply edits to. * @param edit Array of edits. Sorted from oldest to most recent. * * @return Thenable signaling that the change has completed. */ - applyEdits(edits: readonly EditType[]): Thenable; + applyEdits(document: CustomDocument, edits: readonly EditType[]): Thenable; /** * Undo a set of edits. * * This is triggered when a user undoes an edit. * + * @param document Document to undo edits from. * @param edit Array of edits. Sorted from most recent to oldest. * * @return Thenable signaling that the change has completed. */ - undoEdits(edits: readonly EditType[]): Thenable; + undoEdits(document: CustomDocument, edits: readonly EditType[]): Thenable; /** * Revert the file to its last saved state. * + * @param document Document to revert. * @param change Added or applied edits. * * @return Thenable signaling that the change has completed. */ - revert(change: { + revert(document: CustomDocument, change: { readonly undoneEdits: readonly EditType[]; readonly appliedEdits: readonly EditType[]; }): Thenable; @@ -1311,12 +1321,13 @@ declare module 'vscode' { * made in quick succession, `backup` is only triggered after the last one. `backup` is not invoked when * `auto save` is enabled (since auto save already persists resource ). * + * @param document Document to revert. * @param cancellation Token that signals the current backup since a new backup is coming in. It is up to your * extension to decided how to respond to cancellation. If for example your extension is backing up a large file * in an operation that takes time to complete, your extension may decide to finish the ongoing backup rather * than cancelling it to ensure that VS Code has some valid backup. */ - backup(cancellation: CancellationToken): Thenable; + backup(document: CustomDocument, cancellation: CancellationToken): Thenable; } /** @@ -1375,7 +1386,7 @@ declare module 'vscode' { * * @return The capabilities of the resolved document. */ - resolveCustomDocument(document: CustomDocument): Thenable; + resolveCustomDocument(document: CustomDocument): Thenable; /** * Resolve a webview editor for a given resource. @@ -1393,6 +1404,13 @@ declare module 'vscode' { * @return Thenable indicating that the webview editor has been resolved. */ resolveCustomEditor(document: CustomDocument, webviewPanel: WebviewPanel): Thenable; + + /** + * Defines the editing capability of a custom webview document. + * + * When not provided, the document is considered readonly. + */ + readonly editingDelegate?: CustomEditorEditingDelegate; } /** diff --git a/src/vs/workbench/api/browser/mainThreadWebview.ts b/src/vs/workbench/api/browser/mainThreadWebview.ts index 52ea58cb549..77193cf90b1 100644 --- a/src/vs/workbench/api/browser/mainThreadWebview.ts +++ b/src/vs/workbench/api/browser/mainThreadWebview.ts @@ -366,14 +366,14 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma return this._customEditorService.models.add(resource, viewType, model); } - public async $onDidEdit(resourceComponents: UriComponents, viewType: string, editId: number): Promise { + public async $onDidEdit(resourceComponents: UriComponents, viewType: string, editId: number, label: string | undefined): Promise { const resource = URI.revive(resourceComponents); const model = await this._customEditorService.models.get(resource, viewType); if (!model || !(model instanceof MainThreadCustomEditorModel)) { throw new Error('Could not find model for webview editor'); } - model.pushEdit(editId); + model.pushEdit(editId, label); } private hookupWebviewEventDelegate(handle: extHostProtocol.WebviewPanelHandle, input: WebviewInput) { @@ -604,7 +604,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod return this._viewType; } - public pushEdit(editId: number) { + public pushEdit(editId: number, label: string | undefined) { if (!this._editable) { throw new Error('Document is not editable'); } @@ -617,7 +617,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod this._undoService.pushElement({ type: UndoRedoElementType.Resource, resource: this.resource, - label: 'Edit', // TODO: get this from extensions? + label: label ?? 'Edit', undo: async () => { if (!this._editable) { return; diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index 2e93f3aa9b8..cbbba0f9265 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -592,7 +592,7 @@ export interface MainThreadWebviewsShape extends IDisposable { $registerCustomEditorProvider(extension: WebviewExtensionDescription, viewType: string, options: modes.IWebviewPanelOptions): void; $unregisterEditorProvider(viewType: string): void; - $onDidEdit(resource: UriComponents, viewType: string, editId: number): void; + $onDidEdit(resource: UriComponents, viewType: string, editId: number, label: string | undefined): void; } export interface WebviewPanelViewStateData { diff --git a/src/vs/workbench/api/common/extHostWebview.ts b/src/vs/workbench/api/common/extHostWebview.ts index 3324e32c8ae..f4dc637a2fe 100644 --- a/src/vs/workbench/api/common/extHostWebview.ts +++ b/src/vs/workbench/api/common/extHostWebview.ts @@ -5,7 +5,7 @@ import { CancellationToken } from 'vs/base/common/cancellation'; import { Emitter, Event } from 'vs/base/common/event'; -import { Disposable } from 'vs/base/common/lifecycle'; +import { Disposable, DisposableStore } from 'vs/base/common/lifecycle'; import { URI, UriComponents } from 'vs/base/common/uri'; import { generateUuid } from 'vs/base/common/uuid'; import * as modes from 'vs/editor/common/modes'; @@ -248,25 +248,31 @@ export class ExtHostWebviewEditor extends Disposable implements vscode.WebviewPa class CustomDocument extends Disposable implements vscode.CustomDocument { - public static create(proxy: MainThreadWebviewsShape, viewType: string, uri: vscode.Uri) { - return Object.seal(new CustomDocument(proxy, viewType, uri)); + public static create( + viewType: string, + uri: vscode.Uri, + editingDelegate: vscode.CustomEditorEditingDelegate | undefined + ) { + return Object.seal(new CustomDocument(viewType, uri, editingDelegate)); } // Explicitly initialize all properties as we seal the object after creation! readonly #_edits = new Cache('edits'); - readonly #proxy: MainThreadWebviewsShape; readonly #viewType: string; readonly #uri: vscode.Uri; + readonly #editingDelegate: vscode.CustomEditorEditingDelegate | undefined; - #capabilities: vscode.CustomEditorCapabilities | undefined = undefined; - - private constructor(proxy: MainThreadWebviewsShape, viewType: string, uri: vscode.Uri) { + private constructor( + viewType: string, + uri: vscode.Uri, + editingDelegate: vscode.CustomEditorEditingDelegate | undefined, + ) { super(); - this.#proxy = proxy; this.#viewType = viewType; this.#uri = uri; + this.#editingDelegate = editingDelegate; } dispose() { @@ -289,47 +295,35 @@ class CustomDocument extends Disposable implements vscode.CustomDocument { //#region Internal - /** @internal*/ _setCapabilities(capabilities: vscode.CustomEditorCapabilities) { - if (this.#capabilities) { - throw new Error('Capabilities already provided'); - } - - this.#capabilities = capabilities; - capabilities.editing?.onDidEdit(edit => { - const id = this.#_edits.add([edit]); - this.#proxy.$onDidEdit(this.uri, this.viewType, id); - }); - } - /** @internal*/ async _revert(changes: { undoneEdits: number[], redoneEdits: number[] }) { - const editing = this.getEditingCapability(); + const editing = this.getEditingDelegate(); const undoneEdits = changes.undoneEdits.map(id => this.#_edits.get(id, 0)); const appliedEdits = changes.redoneEdits.map(id => this.#_edits.get(id, 0)); - return editing.revert({ undoneEdits, appliedEdits }); + return editing.revert(this, { undoneEdits, appliedEdits }); } /** @internal*/ _undo(editId: number) { - const editing = this.getEditingCapability(); + const editing = this.getEditingDelegate(); const edit = this.#_edits.get(editId, 0); - return editing.undoEdits([edit]); + return editing.undoEdits(this, [edit]); } /** @internal*/ _redo(editId: number) { - const editing = this.getEditingCapability(); + const editing = this.getEditingDelegate(); const edit = this.#_edits.get(editId, 0); - return editing.applyEdits([edit]); + return editing.applyEdits(this, [edit]); } /** @internal*/ _save(cancellation: CancellationToken) { - return this.getEditingCapability().save(cancellation); + return this.getEditingDelegate().save(this, cancellation); } /** @internal*/ _saveAs(target: vscode.Uri) { - return this.getEditingCapability().saveAs(target); + return this.getEditingDelegate().saveAs(this, target); } /** @internal*/ _backup(cancellation: CancellationToken) { - return this.getEditingCapability().backup(cancellation); + return this.getEditingDelegate().backup(this, cancellation); } /** @internal*/ _disposeEdits(editIds: number[]) { @@ -338,13 +332,17 @@ class CustomDocument extends Disposable implements vscode.CustomDocument { } } + /** @internal*/ _pushEdit(edit: unknown): number { + return this.#_edits.add([edit]); + } + //#endregion - private getEditingCapability(): vscode.CustomEditorEditingCapability { - if (!this.#capabilities?.editing) { + private getEditingDelegate(): vscode.CustomEditorEditingDelegate { + if (!this.#editingDelegate) { throw new Error('Document is not editable'); } - return this.#capabilities.editing; + return this.#editingDelegate; } } @@ -487,17 +485,24 @@ export class ExtHostWebviews implements ExtHostWebviewsShape { provider: vscode.CustomEditorProvider | vscode.CustomTextEditorProvider, options: vscode.WebviewPanelOptions | undefined = {} ): vscode.Disposable { - let disposable: vscode.Disposable; + const disposables = new DisposableStore(); if ('resolveCustomTextEditor' in provider) { - disposable = this._editorProviders.addTextProvider(viewType, extension, provider); + disposables.add(this._editorProviders.addTextProvider(viewType, extension, provider)); this._proxy.$registerTextEditorProvider(toExtensionData(extension), viewType, options); } else { - disposable = this._editorProviders.addCustomProvider(viewType, extension, provider); + disposables.add(this._editorProviders.addCustomProvider(viewType, extension, provider)); this._proxy.$registerCustomEditorProvider(toExtensionData(extension), viewType, options); + if (provider.editingDelegate) { + disposables.add(provider.editingDelegate.onDidEdit(e => { + const document = e.document; + const editId = (document as CustomDocument)._pushEdit(e.edit); + this._proxy.$onDidEdit(document.uri, document.viewType, editId, e.label); + })); + } } return VSCodeDisposable.from( - disposable, + disposables, new VSCodeDisposable(() => { this._proxy.$unregisterEditorProvider(viewType); })); @@ -592,12 +597,11 @@ export class ExtHostWebviews implements ExtHostWebviewsShape { } const revivedResource = URI.revive(resource); - const document = CustomDocument.create(this._proxy, viewType, revivedResource); - const capabilities = await entry.provider.resolveCustomDocument(document); - document._setCapabilities(capabilities); + const document = CustomDocument.create(viewType, revivedResource, entry.provider.editingDelegate); + await entry.provider.resolveCustomDocument(document); this._documents.add(document); return { - editable: !!capabilities.editing + editable: !!entry.provider.editingDelegate, }; }