fix(P4): LSP 三处修复——①clangd 需真实文件编译分析(虚拟 uri 不工作) ②method 全名 textDocument/publishDiagnostics(旧匹配 publishDiagnostics 导致 clangd 回退失效) ③回退时先查已收缓存(clangd 推送早于 ev 注册)
这个提交包含在:
@@ -9,6 +9,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import queue
|
import queue
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
@@ -109,7 +110,7 @@ class LspProcess:
|
|||||||
q = self._pending.pop(msg.get("id"), None)
|
q = self._pending.pop(msg.get("id"), None)
|
||||||
if q:
|
if q:
|
||||||
q.put(msg)
|
q.put(msg)
|
||||||
elif msg.get("method") == "publishDiagnostics":
|
elif msg.get("method") == "textDocument/publishDiagnostics":
|
||||||
uri = msg["params"].get("uri", "")
|
uri = msg["params"].get("uri", "")
|
||||||
logger.debug("LSP %s publishDiagnostics uri=%s (期望在 _diags=%s)", self.lang, uri, bool(self._diag_event.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", [])
|
self._diags[uri] = msg["params"].get("diagnostics", [])
|
||||||
@@ -161,9 +162,25 @@ class LspProcess:
|
|||||||
|
|
||||||
# ---------- 文档操作 ----------
|
# ---------- 文档操作 ----------
|
||||||
def _uri(self) -> str:
|
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", {
|
self.notify("textDocument/didOpen", {
|
||||||
"textDocument": {"uri": uri, "languageId": LANG_IDS[self.lang],
|
"textDocument": {"uri": uri, "languageId": LANG_IDS[self.lang],
|
||||||
"version": 1, "text": code},
|
"version": 1, "text": code},
|
||||||
@@ -176,14 +193,18 @@ class LspProcess:
|
|||||||
if not self._warmed:
|
if not self._warmed:
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
self._warmed = True
|
self._warmed = True
|
||||||
uri = self._uri()
|
path = self._uri()
|
||||||
self.open_doc(uri, code)
|
uri = self._to_uri(path)
|
||||||
|
self.open_doc(path, code)
|
||||||
t0 = time.time()
|
t0 = time.time()
|
||||||
while time.time() - t0 < timeout:
|
while time.time() - t0 < timeout:
|
||||||
try:
|
try:
|
||||||
result = self.request("textDocument/diagnostic", {"textDocument": {"uri": uri}}, timeout=min(10, timeout))
|
result = self.request("textDocument/diagnostic", {"textDocument": {"uri": uri}}, timeout=min(10, timeout))
|
||||||
except LspError as e:
|
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)
|
logger.warning("LSP %s 拉取诊断失败,回退异步推送: %s", self.lang, e)
|
||||||
ev = threading.Event()
|
ev = threading.Event()
|
||||||
self._diag_event[uri] = ev
|
self._diag_event[uri] = ev
|
||||||
@@ -199,8 +220,9 @@ class LspProcess:
|
|||||||
raise LspError(f"{self.lang} 诊断超时(服务器无响应)")
|
raise LspError(f"{self.lang} 诊断超时(服务器无响应)")
|
||||||
|
|
||||||
def completion(self, code: str, line: int, character: int) -> list[dict]:
|
def completion(self, code: str, line: int, character: int) -> list[dict]:
|
||||||
uri = self._uri()
|
path = self._uri()
|
||||||
self.open_doc(uri, code)
|
uri = self._to_uri(path)
|
||||||
|
self.open_doc(path, code)
|
||||||
result = self.request("textDocument/completion", {
|
result = self.request("textDocument/completion", {
|
||||||
"textDocument": {"uri": uri}, "position": {"line": line, "character": character},
|
"textDocument": {"uri": uri}, "position": {"line": line, "character": character},
|
||||||
"context": {"triggerKind": 1},
|
"context": {"triggerKind": 1},
|
||||||
@@ -215,8 +237,9 @@ class LspProcess:
|
|||||||
} for it in items]
|
} for it in items]
|
||||||
|
|
||||||
def hover(self, code: str, line: int, character: int) -> str:
|
def hover(self, code: str, line: int, character: int) -> str:
|
||||||
uri = self._uri()
|
path = self._uri()
|
||||||
self.open_doc(uri, code)
|
uri = self._to_uri(path)
|
||||||
|
self.open_doc(path, code)
|
||||||
result = self.request("textDocument/hover", {
|
result = self.request("textDocument/hover", {
|
||||||
"textDocument": {"uri": uri}, "position": {"line": line, "character": character},
|
"textDocument": {"uri": uri}, "position": {"line": line, "character": character},
|
||||||
})
|
})
|
||||||
|
|||||||
在新工单中引用
屏蔽一个用户