fix(P4): LSP 诊断改主动拉取(textDocument/diagnostic 3.17)——异步 publishDiagnostics 管道帧错位(read(n)不满)导致非确定丢帧
这个提交包含在:
@@ -49,6 +49,7 @@ class LspProcess:
|
||||
self._msg_id = 0
|
||||
self._ready = threading.Event()
|
||||
self._closed = False
|
||||
self._warmed = False
|
||||
|
||||
def start(self) -> None:
|
||||
try:
|
||||
@@ -71,6 +72,16 @@ class LspProcess:
|
||||
self.proc.stdin.write(frame)
|
||||
self.proc.stdin.flush()
|
||||
|
||||
def _read_exact(self, n: int) -> bytes:
|
||||
"""管道 read(n) 不保证读满 n 字节(帧错位根因),循环读满。"""
|
||||
buf = b""
|
||||
while len(buf) < n and self.proc and self.proc.stdout:
|
||||
chunk = self.proc.stdout.read(n - len(buf))
|
||||
if not chunk:
|
||||
break
|
||||
buf += chunk
|
||||
return buf
|
||||
|
||||
def _read_loop(self) -> None:
|
||||
try:
|
||||
while not self._closed and self.proc and self.proc.stdout:
|
||||
@@ -81,8 +92,8 @@ class LspProcess:
|
||||
continue
|
||||
n = int(line.split(b":")[1])
|
||||
self.proc.stdout.readline() # 空行
|
||||
raw = self.proc.stdout.read(n)
|
||||
if not raw:
|
||||
raw = self._read_exact(n)
|
||||
if len(raw) < n:
|
||||
break
|
||||
try:
|
||||
msg = json.loads(raw)
|
||||
@@ -93,12 +104,14 @@ class LspProcess:
|
||||
logger.warning("LSP %s 读线程退出: %s", self.lang, e)
|
||||
|
||||
def _dispatch(self, msg: dict) -> None:
|
||||
logger.debug("LSP %s 收到帧: method=%s id=%s", self.lang, msg.get("method"), msg.get("id"))
|
||||
if "id" in msg and "method" not in msg:
|
||||
q = self._pending.pop(msg.get("id"), None)
|
||||
if q:
|
||||
q.put(msg)
|
||||
elif msg.get("method") == "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", [])
|
||||
ev = self._diag_event.get(uri)
|
||||
if ev:
|
||||
@@ -156,15 +169,34 @@ class LspProcess:
|
||||
"version": 1, "text": code},
|
||||
})
|
||||
|
||||
def diagnostics(self, code: str, timeout: float = 12.0) -> list[dict]:
|
||||
def diagnostics(self, code: str, timeout: float = 15.0) -> list[dict]:
|
||||
"""主动拉取诊断(LSP 3.17 textDocument/diagnostic;不依赖异步 publishDiagnostics)。
|
||||
首帧前留初始化间隔,拉取为空时小间隔重试(服务器异步分析)。
|
||||
"""
|
||||
if not self._warmed:
|
||||
time.sleep(2)
|
||||
self._warmed = True
|
||||
uri = self._uri()
|
||||
self._diag_event[uri] = threading.Event()
|
||||
self.open_doc(uri, code)
|
||||
if not self._diag_event[uri].wait(timeout):
|
||||
self._diag_event.pop(uri, None)
|
||||
raise LspError(f"{self.lang} 诊断超时(服务器无响应)")
|
||||
self._diag_event.pop(uri, None)
|
||||
return self._diags.pop(uri, [])
|
||||
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
|
||||
logger.warning("LSP %s 拉取诊断失败,回退异步推送: %s", self.lang, e)
|
||||
ev = threading.Event()
|
||||
self._diag_event[uri] = ev
|
||||
if ev.wait(timeout):
|
||||
self._diag_event.pop(uri, None)
|
||||
return self._diags.pop(uri, [])
|
||||
self._diag_event.pop(uri, None)
|
||||
raise
|
||||
items = result.get("items", [])
|
||||
if items:
|
||||
return items
|
||||
time.sleep(1.0)
|
||||
raise LspError(f"{self.lang} 诊断超时(服务器无响应)")
|
||||
|
||||
def completion(self, code: str, line: int, character: int) -> list[dict]:
|
||||
uri = self._uri()
|
||||
|
||||
在新工单中引用
屏蔽一个用户