LSP: Handle shutdown commands

This fixes the issue with terminal-based editor Helix which asks
language servers to shut down when trying to close the editor. Since
blueprint-compiler's server implementation didn't handle this request,
Helix ended up waiting for a response until timing out after a few
seconds and forcefully terminating the language server process.

Besides fixing Helix, this patch should also make user-initiated server
restarts more robust.
This commit is contained in:
Alexey Yerin 2025-01-03 22:31:49 +03:00
parent d6f4b88d35
commit f3faf4b993

View file

@ -118,6 +118,7 @@ class LanguageServer:
self.client_capabilities = {}
self.client_supports_completion_choice = False
self._open_files: T.Dict[str, OpenFile] = {}
self._exited = False
def run(self):
# Read <doc> tags from gir files. During normal compilation these are
@ -125,7 +126,7 @@ class LanguageServer:
xml_reader.PARSE_GIR.add("doc")
try:
while True:
while not self._exited:
line = ""
content_len = -1
while content_len == -1 or (line != "\n" and line != "\r\n"):
@ -221,6 +222,14 @@ class LanguageServer:
},
)
@command("shutdown")
def shutdown(self, id, params):
self._send_response(id, None)
@command("exit")
def exit(self, id, params):
self._exited = True
@command("textDocument/didOpen")
def didOpen(self, id, params):
doc = params.get("textDocument")