shaman-apprentice
shaman-apprentice

Reputation: 190

vscode-extension update virtual document

I try to figure out, how to update a virtual document with my own custom Provider, which I have opened in an editor. I neither understand the docu at https://code.visualstudio.com/api/extension-guides/virtual-documents#update-virtual-documents nor the example at https://github.com/microsoft/vscode-extension-samples/tree/master/contentprovider-sample

I tried to hack something like the following:

class MyProvider implements vscode.TextDocumentContentProvider {
  public onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
  onDidChange = this.onDidChangeEmitter.event;

  public static instance: LocalChangesProvider | undefined;
  public constructor() {
    LocalChangesProvider.instance = this;
  }

  provideTextDocumentContent(uri: vscode.Uri) {
    return someGlobal.text;
  }
}

someGlobal.text = 'other text';
MyProvider.instance.onDidChangeEmitter.fire(myUriSchema);

But it seems I am missing something. I am kind of frustrated, that I am too stupid to simply notify vscode to update my own virtual document :/ Any help is appreciated :)

Upvotes: 3

Views: 1040

Answers (1)

shaman-apprentice
shaman-apprentice

Reputation: 190

While working for a minimal full example I noticed, that the myUriSchema must match the title / path of the opened document, meaning:

const documentUriToUpdate = vscode.Uri.parse(myUriSchema + ':' + myTitle);
MyProvider.instance.onDidChangeEmitter.fire(documentUriToUpdate )

P.S.: From a design perspective this instance-hack of mine definitely wins no price - just for PoC how it works :)

Upvotes: 1

Related Questions