diff --git a/backend/app/services/lsp_service.py b/backend/app/services/lsp_service.py index 6ebee8a..922e097 100644 --- a/backend/app/services/lsp_service.py +++ b/backend/app/services/lsp_service.py @@ -9,6 +9,7 @@ from __future__ import annotations import json import logging +import os import queue import re import subprocess @@ -109,7 +110,7 @@ class LspProcess: q = self._pending.pop(msg.get("id"), None) if q: q.put(msg) - elif msg.get("method") == "publishDiagnostics": + elif msg.get("method") == "textDocument/publishDiagnostics": uri = msg["params"].get("uri", "") logger.debug("LSP %s publishDiagnostics uri=%s (期望在 _diags=%s)", self.lang, uri, bool(self._diag_event.get(uri))) self._diags[uri] = msg["params"].get("diagnostics", []) @@ -161,9 +162,25 @@ class LspProcess: # ---------- 文档操作 ---------- def _uri(self) -> str: - return f"{self.uri_prefix}/{self.lang}/{uuid.uuid4().hex}{EXT[self.lang]}" + """返回真实文件路径(clangd 等需真实文件做编译分析,虚拟 uri 不工作)。""" + d = f"/tmp/fl/{self.lang}" + try: + os.makedirs(d, exist_ok=True) + except Exception: # noqa: BLE001 + pass + return f"{d}/{uuid.uuid4().hex}{EXT[self.lang]}" - def open_doc(self, uri: str, code: str) -> None: + @staticmethod + def _to_uri(path: str) -> str: + return "file://" + path + + def open_doc(self, path: str, code: str) -> None: + try: + with open(path, "w", encoding="utf-8") as f: + f.write(code) + except Exception: # noqa: BLE001 + logger.warning("LSP %s 写临时文件失败: %s", self.lang, path) + uri = self._to_uri(path) self.notify("textDocument/didOpen", { "textDocument": {"uri": uri, "languageId": LANG_IDS[self.lang], "version": 1, "text": code}, @@ -176,14 +193,18 @@ class LspProcess: if not self._warmed: time.sleep(2) self._warmed = True - uri = self._uri() - self.open_doc(uri, code) + path = self._uri() + uri = self._to_uri(path) + self.open_doc(path, code) t0 = time.time() while time.time() - t0 < timeout: try: result = self.request("textDocument/diagnostic", {"textDocument": {"uri": uri}}, timeout=min(10, timeout)) except LspError as e: - # 服务器不支持拉取诊断 → 回退等 publishDiagnostics + # 服务器不支持拉取诊断(如 clangd 14)→ 回退等 publishDiagnostics。 + # 推送可能早于回退注册(didOpen 后立即到达),先查已收缓存。 + if uri in self._diags: + return self._diags.pop(uri, []) logger.warning("LSP %s 拉取诊断失败,回退异步推送: %s", self.lang, e) ev = threading.Event() self._diag_event[uri] = ev @@ -199,8 +220,9 @@ class LspProcess: raise LspError(f"{self.lang} 诊断超时(服务器无响应)") def completion(self, code: str, line: int, character: int) -> list[dict]: - uri = self._uri() - self.open_doc(uri, code) + path = self._uri() + uri = self._to_uri(path) + self.open_doc(path, code) result = self.request("textDocument/completion", { "textDocument": {"uri": uri}, "position": {"line": line, "character": character}, "context": {"triggerKind": 1}, @@ -215,8 +237,9 @@ class LspProcess: } for it in items] def hover(self, code: str, line: int, character: int) -> str: - uri = self._uri() - self.open_doc(uri, code) + path = self._uri() + uri = self._to_uri(path) + self.open_doc(path, code) result = self.request("textDocument/hover", { "textDocument": {"uri": uri}, "position": {"line": line, "character": character}, })