From 71dfc8902555f21a47a1255b4336436a5bbb6983 Mon Sep 17 00:00:00 2001 From: langxm Date: Wed, 14 Oct 2026 03:38:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(P4):=20LSP=20=E4=B8=89=E5=A4=84=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E2=80=94=E2=80=94=E2=91=A0clangd=20=E9=9C=80=E7=9C=9F?= =?UTF-8?q?=E5=AE=9E=E6=96=87=E4=BB=B6=E7=BC=96=E8=AF=91=E5=88=86=E6=9E=90?= =?UTF-8?q?(=E8=99=9A=E6=8B=9F=20uri=20=E4=B8=8D=E5=B7=A5=E4=BD=9C)=20?= =?UTF-8?q?=E2=91=A1method=20=E5=85=A8=E5=90=8D=20textDocument/publishDiag?= =?UTF-8?q?nostics(=E6=97=A7=E5=8C=B9=E9=85=8D=20publishDiagnostics=20?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=20clangd=20=E5=9B=9E=E9=80=80=E5=A4=B1?= =?UTF-8?q?=E6=95=88)=20=E2=91=A2=E5=9B=9E=E9=80=80=E6=97=B6=E5=85=88?= =?UTF-8?q?=E6=9F=A5=E5=B7=B2=E6=94=B6=E7=BC=93=E5=AD=98(clangd=20?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E6=97=A9=E4=BA=8E=20ev=20=E6=B3=A8=E5=86=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/services/lsp_service.py | 43 ++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 10 deletions(-) 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}, })