Skip to content

Language Server Protocol for Dart

Shirish Kadam edited this page May 23, 2023 · 4 revisions

The Language Server Protocol (LSP), which is designed to integrate multiple languages into code editors and IDEs. In this case, we are using the Dart language server.

To run this server and interact with it, we'll be starting the process, listening for JSON-RPC messages on its stdout, and writing JSON-RPC messages to its stdin. Here the client 'Tonic IDE' will be interacting with the server by sending request, notifications and the server will be responding back to the client with appropriate resolution.

Starting the LSP Server

In order to start and initialize the LSP server, you need to follow these steps:

  1. Start a new process, which will be your LSP server. dart language-server --client-id my-editor.my-plugin --client-version 1.2
  2. After starting the server, client needs need to send an initialize request to it. This is done by writing a properly formed request to the stdin of the process.
  3. Client can now monitor the server by listening to its stdout and stderr streams. This allows it to process any output or errors that the server produces.

Stopping LSP Server

As for the sequence of stop operations, the typical sequence would be:

  1. Send the shutdown request to the server. This request asks the server to shut down, but it does not actually cause the server to exit.
  2. Wait for the server to respond to the shutdown request. The server should respond with an empty response ({}).
  3. Send the exit notification to the server. This notification asks the server to exit.
  4. Close the input stream of the server process. This is necessary because the server might still be waiting for input.
  5. Wait for the server process to exit. You can do this by waiting on Process.exitCode. If the server does not exit within a reasonable time, send the SIGTERM signal to the server process with Process.kill(). If the server still does not exit, send the SIGKILL signal with Process.kill(ProcessSignal.sigkill).
Clone this wiki locally