Search code examples
visual-studio-codevscode-extensionslanguage-server-protocol

How to force the update of the code lenses in vscode


I am working on "run code" actions using code lenses in Metals (Scala Language Server). In our use case, we can provide those code lenses only once the compilation finishes. If the user is not modifying the source file during or after the compilation, vscode won't send a request for code lenses. This can lead to an ugly state of "stale code lenses" from before the compilation.

Digging into the internals of vscode-languageserver-node I have noticed that the CodeLensProvider can emit an onDidChangeCodeLenses event but it is not reflected in the LSP (https://github.com/microsoft/language-server-protocol/issues/192), hence I cannot send this event to vscode.

The other thing which would probably force reloading code lenses is the ICodeEditor::setModel method but I am not sure if this can be done directly from the vscode extension, since I cannot get a reference to the code editor.

How can this be done from either the Language Server or vscode extension? Or is there another, preferred way?


Solution

  • Unfortunately, the language server protocol indeed doesn't support this yet. The corresponding feature request I opened a while ago can be found here.

    I was able to work around this by implementing a dummy CodeLensProvider on the extension side for the same language ID (the VSCode API generally allows registering multiple providers for language features). It does nothing but call the onDidChangeCodeLenses event when necessary, the actual implementation is still on the language server side.