monaco-languageclient icon indicating copy to clipboard operation
monaco-languageclient copied to clipboard

Get LanguageClient of LSP running in WebExtension

Open P422L opened this issue 1 year ago • 6 comments

Description

I have created a MonacoEditorReactComp with the following userConfig:

import getEditorServiceOverride from '@codingame/monaco-vscode-editor-service-override';
import './custom-lsp-1.0.0.vsix';
import { useOpenEditorStub } from 'monaco-editor-wrapper/vscode/services';
import { UserConfig } from 'monaco-editor-wrapper';

export const setupExtended = async (test?: string): Promise<UserConfig> => {
    let main = {
        text: text || "",
        fileExt: 'test'
    };
    let original = {
        text: text || "",
        fileExt: 'test'
    };

    return {
        wrapperConfig: {
            serviceConfig: {
                userServices: {
                    ...getEditorServiceOverride(useOpenEditorStub),
                },
                enableExtHostWorker: true,
                debugLogging: true
            },
            editorAppConfig: {
                $type: 'extended',
                codeResources: {
                    main,
                    original
                },
                useDiffEditor: false,
                userConfiguration: {
                    json: JSON.stringify({
                        'workbench.colorTheme': 'Custom Dark',
                    })
                }
            }
        }
    }
}

The LSP functionalities work as expected, meaning my Monaco editor is definitely connected to the LanguageClient from the .vsix file, and this LanguageClient is also connected to the LanguageServer running in the extension.

Use Case

I need to react to a custom state of the LanguageClient, meaning I need a way to communicate between the LanguageClient and my editor. Currently, I only see options to access the LanguageClient and the connection when I create it in the config, but not when I want to access a LanguageClient in an extension.

Question

How can I obtain the connection details for a LanguageClient that is running in an extension, so I can communicate with it from my editor? Is there no way I can access it via the id?

P422L avatar Sep 24 '24 07:09 P422L

In addition, I execute custom commands with:

monacoEditorRef.current?.executeCommand(...)

...which throw intial error messages because the LanguageServer has not yet started. So it would also be nice for this use case to be able to communicate explicitly with the LanguageClient to be informed when the LanguageServer is fully ready

P422L avatar Sep 24 '24 07:09 P422L

@P422L is this web extension created by you? You need to communicate with that extension somehow, because it runs isolated from your editor code above. A MessageChannel would be a way to achieve that or something like this: https://github.com/TypeFox/vscode-messenger

kaisalmen avatar Sep 24 '24 16:09 kaisalmen

@kaisalmen Thank you for your quick reply. Yes it is my extension and I have tried both the MessageChannel and vscode-messenger approach but to no avail.

I lack a bit of understanding of how to connect my React component with my Monaco Editor (running in a worker) to the LanguageClient (running in a worker in a WebExtension). The approach with the MessageChannel sounds good, but as far as I understand it I have to pass it, otherwise I can't communicate on the same channel.

Do you have a short example?

P422L avatar Sep 25 '24 09:09 P422L

@kaisalmen I would still appreciate a small code snippet that shows me how to set up the communication to an extension in a webworker. 😅

P422L avatar Oct 07 '24 06:10 P422L

@P422L sorry, I don't have a ready example for your problem. I am working on PR #734 where the languageclient / server communication and the file exchange is routed via message channels. This is still WIP. This is not exactly what you need, but may help.

You need to pass the message port via a transferables to a worker. You can pass the two ports of a channel to different workers and then use that channel for inter-worker communication.

kaisalmen avatar Oct 07 '24 19:10 kaisalmen

@kaisalmen unfortunately, that doesn't really help me. My problem is that I don't understand how I can continuously pass something to a worker in a web extension.

pbrostean avatar Oct 18 '24 11:10 pbrostean

@pbrostean / @P422L I have to see if I can find an example.

kaisalmen avatar Oct 21 '24 08:10 kaisalmen

As this is not an issue but rather a question for help i think this can be closed and reopened for discussion if needed.

pbrostean avatar Nov 19 '24 09:11 pbrostean