Can HTML / JScontent TextDocumentContentProvider talk to extension code?

I am creating a Visual Studio Code Extension. This extension has the TextDocumentContentProvider

displayed command vscode.previewHtml

.

I want this provider to show the output of my compiled code. It seems trivial. However, since my extension also has a debugger; I want the extension context (or better yet, the debugger context) to be able to talk to this preview tab code. So the debugger can update the tab (by sending recompiled data to it) and possibly return data for the status.

Is there a way to do this without any server? I am guessing that I could have a network server from an extension context (since it is Node) and some kind of HTML preview client that connects to the server at the port specified in the uri preview schema, but it seems to be bit cumbersome.

I usually find answers to VSC extension development questions (and links / examples) by searching for extensions with similar functionality on GitHub, but I can't find any extension that does this (including Microsoft's two TextDocumentContentProvider

selections
).

So, does anyone know an easy way to do this, or is it even possible? (Or any extension that does what I can research).

(Edit) I'm leaning towards using a Node WebSocket server (at the extension level) and a WebSocket client (at the HTML preview level), which is the LaTeX preview extension allows live preview to be updated. Seems very possible and probably sufficient for me, but a bit of a workaround since I will have to create my own serialized action protocol. Having a similar JS context to interact with VSCode commands (not just link commands) would be better, if at all possible.

+3


source to share


2 answers


After a lot of research, it turns out that there is no normal way of communication between extension / debugger code and the preview context (webview).

Available functions:

  • An update to the content provider that causes the preview content to be re-rendered.
  • Calling vscode

    commands from preview content via HTML links to be clicked
  • Calling vscode

    commands from preview content using injection links
  • Some scrolling manipulations through the editor interface


None of these are suitable for linking to the preview tab.

Currently, a WebSocket-based client-server architecture is the only way to provide bi-directional communication between two contexts.

An extension to support preview / webview is distributed , so this may change in the future.

+2


source


The text content provider update is already built in. Your text content provider must have an event property onDidChange

. previewHTML

impl hooks into this to know when an update is needed. So in your extension, when you feel the need, you call your provider's update function , which in turn triggers this even . This will then cause previewHTML

impl to run your function again provideTextDocumentContent

, where you can provide updated data.



0


source







All Articles