Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use webview with monaco-languageclient #882

Open
MangoIV opened this issue Feb 25, 2025 · 7 comments
Open

how to use webview with monaco-languageclient #882

MangoIV opened this issue Feb 25, 2025 · 7 comments

Comments

@MangoIV
Copy link

MangoIV commented Feb 25, 2025

Hi - I would like to ask whether or not it is possible to use the vscode webview with monaco-languageclient or if there's an example for that around? I see that there's some code for it but I don't understand how to use it.

I'm also wondering if I'm missing some place for documentation of this project? All I could find were the examples and they are all kind of incomplete if one is trying to implement all of an extension without actually loading up the bundled extension.

Thank you so much in advance, an answer is much appreciated!

@CGNonofr
Copy link
Collaborator

Hello,

monaco-languageclient is based on monaco-vscode-api, which supports everything that VSCode support in web mode, including webviews.

The proper service-overrides need to be loaded, then you'll be able to load your extension using webviews. Either by calling the registerExtension function by hands, or loading your vsix using the rollup-vsix-loader, or even installing the extension from the marketplace

@MangoIV
Copy link
Author

MangoIV commented Feb 25, 2025

Thank you for your answer @CGNonofr, if I may ask, is there any way to find out which one is the correct override?

Additionally it would be nice if I could just mount the webview onto a container already reserved on the webpage that monaco is running on to, instead of loading the complete vscode extension - is there support for that?

@MangoIV
Copy link
Author

MangoIV commented Feb 25, 2025

More specifically, there’s this function acquireVsCodeApi which is supplied by some magic to webviews. My question basically amounts to where I can find that magic and how to use it to pass that function to the component that would be running in the webview

@CGNonofr
Copy link
Collaborator

Thank you for your answer @CGNonofr, if I may ask, is there any way to find out which one is the correct override?

Not really, you can start by adding the obvious ones then add the missing ones when you notice an issue or a missing feature

Additionally it would be nice if I could just mount the webview onto a container already reserved on the webpage that monaco is running on to, instead of loading the complete vscode extension - is there support for that?

It doesn't work that way, the point of webview is to let the user manipulate them, move them or close them. If you just want to render an iframe in your container, why aren't you doing it yourself?

More specifically, there’s this function acquireVsCodeApi which is supplied by some magic to webviews. My question basically amounts to where I can find that magic and how to use it to pass that function to the component that would be running in the webview

acquireVsCodeApi just provides 3 methods (getState, setState and postMessage), all relying on postmessages, so it can probably easily be implemented by hands though, depending on what you're trying to do

@MangoIV
Copy link
Author

MangoIV commented Feb 25, 2025

why aren't you doing it yourself

I try to, the issue is that the webview loses the ability to talk to vscode, i.e. the webview tries to use vscode-messenger to talk to the editor and that needs the acquireVsCodeApi function to supply the necessary context.

I would be happy to implement that part myself, however I don't know enough about vscode to know what should go there. vscode-messenger basically says nothing about what is supposed to go there and the types aren't helpful either, it just says "this is passed in by vscode"

@CGNonofr
Copy link
Collaborator

I'm sorry, that's too advanced for me to be able to help you. The best documentation is the code itself though :)

@MangoIV
Copy link
Author

MangoIV commented Feb 25, 2025

The best documentation is the code itself though

if you're speaking in terms of this code, then I'm willing to believe you, if you're speaking in absolute terms, perhaps you can tell that the knowledge of an interface having three methods isn't quite enough to implement it ;)

Thank you for your help though. I will try to use an actual webview for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants