|
11 | 11 | - textDocument/publishDiagnostics (Fehler/Warnungen) |
12 | 12 | """ |
13 | 13 |
|
| 14 | +import importlib.util |
14 | 15 | import json |
15 | 16 | import subprocess |
| 17 | +import sys |
16 | 18 | import threading |
17 | 19 | import shutil |
18 | 20 | from typing import Optional, Dict, List, Callable |
|
21 | 23 |
|
22 | 24 | # Bekannte LSP-Server pro Sprache |
23 | 25 | LSP_SERVERS = { |
24 | | - "Python": {"cmd": ["pylsp"], "check": "pylsp"}, |
| 26 | + "Python": {"cmd": ["pylsp"], "check": "pylsp", "module": "pylsp"}, |
25 | 27 | "JavaScript": {"cmd": ["typescript-language-server", "--stdio"], "check": "typescript-language-server"}, |
26 | 28 | "TypeScript": {"cmd": ["typescript-language-server", "--stdio"], "check": "typescript-language-server"}, |
27 | 29 | "Rust": {"cmd": ["rust-analyzer"], "check": "rust-analyzer"}, |
@@ -83,22 +85,37 @@ def __init__(self, language: str, root_path: str = "."): |
83 | 85 | def server_config(self) -> Optional[dict]: |
84 | 86 | return LSP_SERVERS.get(self.language) |
85 | 87 |
|
86 | | - def is_available(self) -> bool: |
87 | | - """Prüft, ob der LSP-Server installiert ist.""" |
| 88 | + def _resolve_command(self) -> Optional[List[str]]: |
| 89 | + """Ermittelt das startbare Server-Kommando. |
| 90 | +
|
| 91 | + Bei Python ist `pylsp.exe` nach einer pip-Installation nicht immer auf |
| 92 | + PATH. Wenn das Modul im aktuellen Interpreter vorhanden ist, startet |
| 93 | + CodeBox den Server deshalb über `python -m pylsp`. |
| 94 | + """ |
88 | 95 | config = self.server_config |
89 | 96 | if not config: |
90 | | - return False |
91 | | - return shutil.which(config["check"]) is not None |
| 97 | + return None |
| 98 | + check = config.get("check") |
| 99 | + if check and shutil.which(check): |
| 100 | + return list(config["cmd"]) |
| 101 | + module = config.get("module") |
| 102 | + if module and importlib.util.find_spec(module): |
| 103 | + return [sys.executable, "-m", module] |
| 104 | + return None |
| 105 | + |
| 106 | + def is_available(self) -> bool: |
| 107 | + """Prüft, ob der LSP-Server installiert ist.""" |
| 108 | + return self._resolve_command() is not None |
92 | 109 |
|
93 | 110 | def start(self) -> bool: |
94 | 111 | """Startet den LSP-Server-Prozess.""" |
95 | | - if not self.is_available(): |
| 112 | + command = self._resolve_command() |
| 113 | + if not command: |
96 | 114 | return False |
97 | 115 |
|
98 | | - config = self.server_config |
99 | 116 | try: |
100 | 117 | self.process = subprocess.Popen( |
101 | | - config["cmd"], |
| 118 | + command, |
102 | 119 | stdin=subprocess.PIPE, |
103 | 120 | stdout=subprocess.PIPE, |
104 | 121 | stderr=subprocess.PIPE, |
@@ -131,18 +148,34 @@ def start(self) -> bool: |
131 | 148 | def stop(self): |
132 | 149 | """Stoppt den LSP-Server.""" |
133 | 150 | self._running = False |
134 | | - if self.process: |
| 151 | + process = self.process |
| 152 | + if process: |
135 | 153 | try: |
136 | 154 | self._send_request("shutdown", {}) |
137 | 155 | self._send_notification("exit", {}) |
138 | 156 | except (BrokenPipeError, OSError): |
139 | 157 | pass |
140 | 158 | try: |
141 | | - self.process.terminate() |
142 | | - self.process.wait(timeout=3) |
| 159 | + process.terminate() |
| 160 | + process.wait(timeout=3) |
143 | 161 | except subprocess.TimeoutExpired: |
144 | | - self.process.kill() |
145 | | - self.process = None |
| 162 | + process.kill() |
| 163 | + process.wait(timeout=3) |
| 164 | + finally: |
| 165 | + for stream in (process.stdin, process.stdout, process.stderr): |
| 166 | + if stream: |
| 167 | + try: |
| 168 | + stream.close() |
| 169 | + except OSError: |
| 170 | + pass |
| 171 | + self.process = None |
| 172 | + if ( |
| 173 | + self._reader_thread |
| 174 | + and self._reader_thread.is_alive() |
| 175 | + and threading.current_thread() is not self._reader_thread |
| 176 | + ): |
| 177 | + self._reader_thread.join(timeout=1) |
| 178 | + self._reader_thread = None |
146 | 179 |
|
147 | 180 | def did_open(self, uri: str, language_id: str, text: str, version: int = 1): |
148 | 181 | """Benachrichtigt den Server über eine geöffnete Datei.""" |
|
0 commit comments