Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 994
shaman-apprentice Avatar asked Apr 27 '26 18:04

shaman-apprentice


1 Answers

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 :)

like image 102
shaman-apprentice Avatar answered May 01 '26 05:05

shaman-apprentice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!