feat(P4.5): Java LSP(jdt.ls) 全通——Temurin JDK25(/opt/jdk25) 满足新版快照 osgi.ee=JavaSE25 要求、官方启动参数(add-modules/add-opens/config_linux/data)、类名做文件名消除 file/class 误报;initialize 超时放宽 90s;java warmup 8s

这个提交包含在:
langxm
2026-10-14 04:00:11 +08:00
父节点 56e8f56d40
当前提交 62872d88ad
+34 -11
查看文件
@@ -38,10 +38,11 @@ class LspError(Exception):
class LspProcess: class LspProcess:
"""单个语言服务器进程:JSON-RPC 帧封装 + 请求/响应 + 诊断收集。""" """单个语言服务器进程:JSON-RPC 帧封装 + 请求/响应 + 诊断收集。"""
def __init__(self, lang: str, cmd: list[str], uri_prefix: str = "file:///tmp/fl"): def __init__(self, lang: str, cmd: list[str], uri_prefix: str = "file:///tmp/fl", warmup: float = 2.0):
self.lang = lang self.lang = lang
self.cmd = cmd self.cmd = cmd
self.uri_prefix = uri_prefix self.uri_prefix = uri_prefix
self.warmup = warmup
self.proc: subprocess.Popen | None = None self.proc: subprocess.Popen | None = None
self._lock = threading.Lock() self._lock = threading.Lock()
self._pending: dict[int, queue.Queue] = {} self._pending: dict[int, queue.Queue] = {}
@@ -149,7 +150,7 @@ class LspProcess:
"hover": {"contentFormat": ["markdown", "plaintext"]}}, "hover": {"contentFormat": ["markdown", "plaintext"]}},
}, },
"workspaceFolders": None, "workspaceFolders": None,
}, timeout=30) }, timeout=90)
self.notify("initialized", {}) self.notify("initialized", {})
self._ready.set() self._ready.set()
@@ -161,14 +162,20 @@ class LspProcess:
pass pass
# ---------- 文档操作 ---------- # ---------- 文档操作 ----------
def _uri(self) -> str: def _uri(self, code: str = "") -> str:
"""返回真实文件路径(clangd 等需真实文件做编译分析,虚拟 uri 不工作)。""" """返回真实文件路径(clangd/jdtls 需真实文件编译分析)。Java 用类名做文件名,
避免 jdt.ls 的 file/class 不匹配误报。"""
d = f"/tmp/fl/{self.lang}" d = f"/tmp/fl/{self.lang}"
try: try:
os.makedirs(d, exist_ok=True) os.makedirs(d, exist_ok=True)
except Exception: # noqa: BLE001 except Exception: # noqa: BLE001
pass pass
return f"{d}/{uuid.uuid4().hex}{EXT[self.lang]}" name = uuid.uuid4().hex
if self.lang == "java" and code:
m = re.search(r"public\s+(?:class|interface|enum)\s+(\w+)", code)
if m:
name = m.group(1)
return f"{d}/{name}{EXT[self.lang]}"
@staticmethod @staticmethod
def _to_uri(path: str) -> str: def _to_uri(path: str) -> str:
@@ -191,9 +198,9 @@ class LspProcess:
首帧前留初始化间隔,拉取为空时小间隔重试(服务器异步分析)。 首帧前留初始化间隔,拉取为空时小间隔重试(服务器异步分析)。
""" """
if not self._warmed: if not self._warmed:
time.sleep(2) time.sleep(self.warmup)
self._warmed = True self._warmed = True
path = self._uri() path = self._uri(code)
uri = self._to_uri(path) uri = self._to_uri(path)
self.open_doc(path, code) self.open_doc(path, code)
t0 = time.time() t0 = time.time()
@@ -220,7 +227,7 @@ 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]:
path = self._uri() path = self._uri(code)
uri = self._to_uri(path) uri = self._to_uri(path)
self.open_doc(path, code) self.open_doc(path, code)
result = self.request("textDocument/completion", { result = self.request("textDocument/completion", {
@@ -237,7 +244,7 @@ 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:
path = self._uri() path = self._uri(code)
uri = self._to_uri(path) uri = self._to_uri(path)
self.open_doc(path, code) self.open_doc(path, code)
result = self.request("textDocument/hover", { result = self.request("textDocument/hover", {
@@ -272,9 +279,25 @@ class LspManager:
with self._lock: with self._lock:
proc = self._procs.get(lang) proc = self._procs.get(lang)
if proc is None or proc.proc is None or proc.proc.poll() is not None: if proc is None or proc.proc is None or proc.proc.poll() is not None:
if lang == "java":
jars = __import__("glob").glob("/opt/jdtls/plugins/org.eclipse.equinox.launcher_*.jar")
if not jars:
raise LspError("Java LSP(jdt.ls)未安装:需部署 eclipse.jdt.ls 到 /opt/jdtls(约 50MB 精简版),安装后自动启用")
try:
os.makedirs("/tmp/jdtls_ws", exist_ok=True)
except Exception: # noqa: BLE001
pass
cmd = ["/opt/jdk25/bin/java", "-Xmx1G",
"--add-modules=ALL-SYSTEM",
"--add-opens", "java.base/java.util=ALL-UNNAMED",
"--add-opens", "java.base/java.lang=ALL-UNNAMED",
"-jar", jars[0],
"-configuration", "/opt/jdtls/config_linux",
"-data", "/tmp/jdtls_ws"]
# jdt.ls 冷启动慢(JDK 索引),首帧 warmup 加长
proc = LspProcess(lang, cmd, warmup=8.0)
else:
cmd = LSP_COMMANDS[lang] cmd = LSP_COMMANDS[lang]
if lang == "java" and not __import__("glob").glob("/opt/jdtls/**/org.eclipse.equinox.launcher_*.jar", recursive=True):
raise LspError("Java LSP(jdt.ls)未安装:需部署 eclipse.jdt.ls 到 /opt/jdtls(约 200MB),安装后自动启用")
proc = LspProcess(lang, cmd) proc = LspProcess(lang, cmd)
proc.start() proc.start()
self._procs[lang] = proc self._procs[lang] = proc