From f3faf4b993c73de623eff17abf7e28cfa9664f6a Mon Sep 17 00:00:00 2001 From: Alexey Yerin Date: Fri, 3 Jan 2025 22:31:49 +0300 Subject: [PATCH] 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. --- blueprintcompiler/lsp.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/blueprintcompiler/lsp.py b/blueprintcompiler/lsp.py index 0659154..c4076b4 100644 --- a/blueprintcompiler/lsp.py +++ b/blueprintcompiler/lsp.py @@ -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 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")