Can TextDocumentContentProvider HTML/JScontent talk to an the extension's code?

Viewed 1009

I'm building a Visual Studio Code extension. This extension has a TextDocumentContentProvider that is displayed with the vscode.previewHtml command.

I want this provider to show the result of my compiled code. That seems trivial. However, because my extension also has a debugger; I want the extension context (or better yet, the debugger context) to be able to talk to that preview tab's code. That way, the debugger can update the tab (sending recompiled data to it) and maybe get some data back for status.

Is there any way to do it without a server of some sort? I suppose I could have a net server running from the extension context (since it's Node) and a client of some kind in the preview HTML that connects to the server in a port specified by the preview tab schema uri, but it seems a bit cumbersome.

I normally find answers to VSC extension development questions (and references/examples) by searching for extensions with similar features on GitHub, but I can't find any extension that quite does that (including Microsoft's two TextDocumentContentProvider samples).

So, does anyone know of an easy way to do that, or if it's possible at all? (Or any extension that does that that I can investigate).

(Edit) I'm leaning towards using a Node WebSocket server (at the extension level) and WebSocket client (at the preview HTML level), which is what the LaTeX preview extension does for real time preview updates. Seems very possible and likely enough for me, but a bit of a workaround since I'll have to establish my own serialized protocol for actions. Having a similar JS context of the ability to communicate with VSCode commands (not just commands-as-links) would be better, if at all possible.

3 Answers

This is now easy to do using the webview api. Extensions can post messages to the webview content and webview can post messages back to the extension.

This sample extension demonstrates how to use the webview API and webview communication. The webview docs have much more detail on the api and webview communication as well.

Related