I'm a programming newbie who wants to implement a VSCode extension and now I want to display some information while hovering over some code snippet in VSCode. There was already an LSP back-end written in Haskell providing functionalities. On the webpage of VSCode extension API, it is written that:
An alternative approach is to implement a Language Server that speaks Language Server Protocol. The way it works is:
- An extension provides a Language Client and a Language Server for JavaScript.
- The Language Client is like any other VS Code extension, running in the Node.js Extension Host context. When it gets activated, it spawns the Language Server in another process and communicates with it through Language Server Protocol.
- You hover over JavaScript code in VS Code
- VS Code informs the Language Client of the hover
- The Language Client queries the Language Server for a hover result and sends it back to VS Code
- VS Code displays the hover result in a Hover widget
The process seems more complicated, but it provides two major benefits:
- The Language Server can be written in any language
- The Language Server can be reused to provide smart editing features for multiple editors
For a more in-depth guide, head over to the Language Server extension guide.
However, Under the above text there isn't any elaboration on how to communicate between front-end and back-end. Neither did the Language Server extension guide help. From my understanding, we have to provide the method provideHover and register a hover provider. So for example, I want to do something like this:
const hoverDisposable = vscode.languages.registerHoverProvider('my-lang', {
provideHover(document, position, token) {
return {
contents: [position.line.toString(), position.character.toString()]
};
}
});
context.subscriptions.push(hoverDisposable);
Now, I'm stuck and can not figure out how to communicate between the client front-end and the server back-end. After some experiments, I found that the document uri and hover position are apparently sent to the backend automatically after hover. However, how do I get the response from the server? It is not in the arguments of provideHover.