-
Notifications
You must be signed in to change notification settings - Fork 37k
Description
This thread details the current state of the custom editor proposal delivered with VS Code 1.39.
The custom editor API will provide a framework for extensions to create fully customizable read/write editors that are used in place of VS Code's standard text editor for specific resources. A XAML custom editor, for example, could show a WYSIWYG style editor for your .xaml files. Our end goal is to give extensions the most flexibility possible while keeping VS Code fast, lean, and consistent.
This iteration, we are sharing the first pieces of the custom editor API proposal: readonly, webview-based custom editors. Here's a quick overview of the currently proposed API:
Contributing a custom editor
A webview-based custom editor binds a resource (a file) to a webview provided by an extension. This webview replaces VS Code's default text/binary editor for that resource.
Consider an extension that previews images. To create webview-based editor, our extension first uses the proposed webviewEditors contribution point to tell VS Code about the custom editors it provides:
"contributes": {
"webviewEditors": [
{
"viewType": "myExtension.imagePreview",
"displayName": "My Image Preview",
"priority": "default",
"selector": [
{
"filenamePattern": "*.{jpg,jpe,jpeg,png,bmp,gif,ico,tga,webp}",
"mime": "image/*"
}
]
}
]
},
"activationEvents": [
"onWebviewEditor:myExtension.imagePreview"
]The important properties are:
viewType- Unique identifier for the editor. This is also used in the extension code later. Note this identifier is also used in the activation event.displayName- Human readable name of the custom editor.priority- Determines if the custom editor should be enabled by default.selector- Specifies when the editor should be enabled.filenamePattern- A glob pattern to identify file names that enable the editor.mime- A glob pattern for mime types that enable the editor (this is used if you open a data URI resource, such as from Git history).
The onWebviewEditor:myExtension.imagePreview activationEvent ensures that our extension is activated whenever VS Code determines it should show our custom image preview editor.
We then use the proposed registerWebviewEditorProvider API to bind our viewType to a resolver that takes a resource and a webview editor. This resolve is invoked whenever a resource that matches the selector for the custom editor is opened:
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.window.registerWebviewEditorProvider(
'myExtension.imagePreview',
{
async resolveWebviewEditor(resource: vscode.Uri, editor: vscode.WebviewEditor): Promise<void> {
// Take ownership of webview and create its content
editor.webview.html = `...`;
}
})
);
}That's it! At this point, our extension can use the editor just like a normal webview.
Custom editor UX
We've also added proposed UX for managing custom editors.
The new Reopen With command allows you to change which editor is used for the current resource. This will close the existing editor and reopen it with the selected editor.
Additionally, the workbench.experimental.editorAssociations setting lets you control which custom editor is used for a given resource. It is a list of file selectors bound to the editor viewType that should be opened when a resource matches the selector:
"workbench.experimental.editorAssociations": [
{
"filenamePattern": "*.png",
"viewType": "myExtension.imagePreview",
}
]Note that default is also a valid viewType and can be used to force VS Code to open its normal text/binary editor instead of any default custom editor:
"workbench.experimental.editorAssociations": [
{
"filenamePattern": "*.png",
"viewType": "default",
}
]Next steps for custom editors
Custom editors are an exciting and potentially powerful new extension point. We want to be sure we that we get the API right so that extensions can build new experiences without degrading VS Code's performance or deviating from some of our core UX principles.
The current proposal is only a first step and will likely be heavily revised. We will continue to iterate on the custom editor API over the coming months.