Virtual Documents
The text document content provider API allows you to create readonly documents in Visual Studio Code from arbitrary sources. You can find a sample extension with source code at: https://github.com/microsoft/vscode-extension-samples/blob/main/virtual-document-sample/README.md.
TextDocumentContentProvider
The API works by claiming an uri-scheme for which your provider then returns text contents. The scheme must be provided when registering a provider and cannot change afterwards. The same provider can be used for multiple schemes and multiple providers can be registered for a single scheme.
vscode.workspace.registerTextDocumentContentProvider(myScheme, myProvider);
Calling registerTextDocumentContentProvider
returns a disposable with which the registration can be undone. A provider must only implement the provideTextDocumentContent
-function which is called with an uri and cancellation token.
const myProvider = new class implements vscode.TextDocumentContentProvider {
provideTextDocumentContent(uri: vscode.Uri): string {
// invoke cowsay, use uri-path as text
return cowsay.say({ text: uri.path });
}
};
Note how the provider doesn't create uris for virtual documents - its role is to provide contents given such an uri. In return, content providers are wired into the open document logic so that providers are always considered.
This sample uses a 'cowsay'-command that crafts an uri which the editor should then show:
vscode.commands.registerCommand('cowsay.say', async () => {
let what = await vscode.window.showInputBox({ placeHolder: 'cow say?' });
if (what) {
let uri = vscode.Uri.parse('cowsay:' + what);
let doc = await vscode.workspace.openTextDocument(uri); // calls back into the provider
await vscode.window.showTextDocument(doc, { preview: false });
}
});
The command prompts for input, creates an uri of the cowsay
-scheme, opens a document for the uri, and finally opens an editor for that document. In step 3, opening the document, the provider is being asked to provide contents for that uri.
With this we have a fully functional text document content provider. The next sections describe how virtual documents can be updated and how UI commands can be registered for virtual documents.
Update Virtual Documents
Depending on the scenario virtual documents might change. To support that, providers can implement a onDidChange
-event.
The vscode.Event
-type defines the contract for eventing in VS Code. The easiest way to implement an event is vscode.EventEmitter
, like so:
const myProvider = new class implements vscode.TextDocumentContentProvider {
// emitter and its event
onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
onDidChange = this.onDidChangeEmitter.event;
//...
};