visual-studio-codevscode-extensionslanguageservice

How does a server-side command in vscode-languageserver-node know which is the current document?


I'm developing an language extension for vscode for a DSL. The architecture uses the vscode-languageserver-node infrastructure from microsoft's github, with a REST back-end that can evaluate the DSL.

I have a command called Evaluate, which should send the current opened file text to the back-end server for evaluation.

I tried modeling it as a server-side command that gets executed with onExecuteCommand, however in the server handler for this, there isn't really a way to find out which is the current focused source file in the client. At least not that I can tell.

I suppose I could just model it as a client command and then manually call sendRequest(ExecuteCommandRequest) with parameters that contain the info I need, to the server in that client handler, but I wonder if I'm missing something.


Solution

  • Assuming that only text documents that the user updated need to be (re-)validated, I find it more convenient (at least for the kind of DSL, where multiple source files need to be validated together) to do this by implementing languageserver, which upon initialization does two things:

    1. loops through open documents workspace.textDocuments in the workspace (those text documents that were open in the workspace in the previous VS Code session) and adds them to the collection of files to be validated
    2. subscribe to workspace events:
      • workspace.onDidOpenTextDocument --> file should be validated and diagnostic info should be shown
      • workspace.onDidChangeTextDocument --> file should be re-validated, diagnostic info shown
      • workspace.onDidCloseTextDocument --> diagnostic info should be deleted

    This is a good place to start: https://code.visualstudio.com/docs/extensions/example-language-server

    ... but instead of handling the evaluation in the languageserver, you can delegate that to your RESTful back-end server. That communication can just be handled in any way you want and the interface does not need to adhere to the languageserver protocol.

    It may be a good idea to introduce a queue of such change requests and throttling of the validation requests. I would only trigger the actual DSL validation if a file was not updated in 3 seconds. That delay can be configurable.