Adds a backup method (#88948)

Adds a backup method to the custom editor API proposal. This method allows custom editors to hook in to VS Code's hot exit behavior

If `backup` is not implemented, VS Code will assume that the custom editor cannot be hot exited.

When `backup` is implemented, VS Code will invoke the method after every edit (this is debounced). At this point, this extension should back up the current resource.  The result is a promise indicating if the backup was successful or not

VS Code will only hot exit if all backups were successful.
This commit is contained in:
Matt Bierner
2020-01-23 16:14:27 -08:00
committed by GitHub
parent b60f43d098
commit f3dbcea32a
6 changed files with 118 additions and 6 deletions

View File

@@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createCancelablePromise } from 'vs/base/common/async';
import { onUnexpectedError } from 'vs/base/common/errors';
import { Disposable, DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
@@ -355,7 +356,10 @@ export class MainThreadWebviews extends Disposable implements extHostProtocol.Ma
});
if (capabilitiesSet.has(extHostProtocol.WebviewEditorCapabilities.SupportsHotExit)) {
// TODO: Hook up hot exit / backup logic
model.onBackup(() => {
return createCancelablePromise(token =>
this._proxy.$backup(model.resource.toJSON(), viewType, token));
});
}
return model;

View File

@@ -612,6 +612,8 @@ export interface ExtHostWebviewsShape {
$onSave(resource: UriComponents, viewType: string): Promise<void>;
$onSaveAs(resource: UriComponents, viewType: string, targetResource: UriComponents): Promise<void>;
$backup(resource: UriComponents, viewType: string, cancellation: CancellationToken): Promise<boolean>;
}
export interface MainThreadUrlsShape extends IDisposable {

View File

@@ -18,6 +18,7 @@ import type * as vscode from 'vscode';
import { Cache } from './cache';
import { ExtHostWebviewsShape, IMainContext, MainContext, MainThreadWebviewsShape, WebviewEditorCapabilities, WebviewPanelHandle, WebviewPanelViewStateData } from './extHost.protocol';
import { Disposable as VSCodeDisposable } from './extHostTypes';
import { CancellationToken } from 'vs/base/common/cancellation';
type IconPath = URI | { light: URI, dark: URI };
@@ -478,6 +479,14 @@ export class ExtHostWebviews implements ExtHostWebviewsShape {
return provider?.editingDelegate?.saveAs(URI.revive(resource), URI.revive(targetResource));
}
async $backup(resource: UriComponents, viewType: string, cancellation: CancellationToken): Promise<boolean> {
const provider = this.getEditorProvider(viewType);
if (!provider?.editingDelegate?.backup) {
return false;
}
return provider.editingDelegate.backup(URI.revive(resource), cancellation);
}
private getWebviewPanel(handle: WebviewPanelHandle): ExtHostWebviewEditor | undefined {
return this._webviewPanels.get(handle);
}
@@ -491,6 +500,9 @@ export class ExtHostWebviews implements ExtHostWebviewsShape {
if (capabilities.editingDelegate) {
declaredCapabilites.push(WebviewEditorCapabilities.Editable);
}
if (capabilities.editingDelegate?.backup) {
declaredCapabilites.push(WebviewEditorCapabilities.SupportsHotExit);
}
return declaredCapabilites;
}
}