From 62872d88ad5fe2b15d5ac96f701609a2c14294dc Mon Sep 17 00:00:00 2001 From: langxm Date: Wed, 14 Oct 2026 04:00:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(P4.5):=20Java=20LSP(jdt.ls)=20=E5=85=A8?= =?UTF-8?q?=E9=80=9A=E2=80=94=E2=80=94Temurin=20JDK25(/opt/jdk25)=20?= =?UTF-8?q?=E6=BB=A1=E8=B6=B3=E6=96=B0=E7=89=88=E5=BF=AB=E7=85=A7=20osgi.e?= =?UTF-8?q?e=3DJavaSE25=20=E8=A6=81=E6=B1=82=E3=80=81=E5=AE=98=E6=96=B9?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=8F=82=E6=95=B0(add-modules/add-opens/conf?= =?UTF-8?q?ig=5Flinux/data)=E3=80=81=E7=B1=BB=E5=90=8D=E5=81=9A=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8D=E6=B6=88=E9=99=A4=20file/class=20=E8=AF=AF?= =?UTF-8?q?=E6=8A=A5=EF=BC=9Binitialize=20=E8=B6=85=E6=97=B6=E6=94=BE?= =?UTF-8?q?=E5=AE=BD=2090s=EF=BC=9Bjava=20warmup=208s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/services/lsp_service.py | 49 +++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/backend/app/services/lsp_service.py b/backend/app/services/lsp_service.py index 922e097..9dec874 100644 --- a/backend/app/services/lsp_service.py +++ b/backend/app/services/lsp_service.py @@ -38,10 +38,11 @@ class LspError(Exception): class LspProcess: """单个语言服务器进程: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.cmd = cmd self.uri_prefix = uri_prefix + self.warmup = warmup self.proc: subprocess.Popen | None = None self._lock = threading.Lock() self._pending: dict[int, queue.Queue] = {} @@ -149,7 +150,7 @@ class LspProcess: "hover": {"contentFormat": ["markdown", "plaintext"]}}, }, "workspaceFolders": None, - }, timeout=30) + }, timeout=90) self.notify("initialized", {}) self._ready.set() @@ -161,14 +162,20 @@ class LspProcess: pass # ---------- 文档操作 ---------- - def _uri(self) -> str: - """返回真实文件路径(clangd 等需真实文件做编译分析,虚拟 uri 不工作)。""" + def _uri(self, code: str = "") -> str: + """返回真实文件路径(clangd/jdtls 需真实文件编译分析)。Java 用类名做文件名, + 避免 jdt.ls 的 file/class 不匹配误报。""" 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]}" + 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 def _to_uri(path: str) -> str: @@ -191,9 +198,9 @@ class LspProcess: 首帧前留初始化间隔,拉取为空时小间隔重试(服务器异步分析)。 """ if not self._warmed: - time.sleep(2) + time.sleep(self.warmup) self._warmed = True - path = self._uri() + path = self._uri(code) uri = self._to_uri(path) self.open_doc(path, code) t0 = time.time() @@ -220,7 +227,7 @@ class LspProcess: raise LspError(f"{self.lang} 诊断超时(服务器无响应)") def completion(self, code: str, line: int, character: int) -> list[dict]: - path = self._uri() + path = self._uri(code) uri = self._to_uri(path) self.open_doc(path, code) result = self.request("textDocument/completion", { @@ -237,7 +244,7 @@ class LspProcess: } for it in items] def hover(self, code: str, line: int, character: int) -> str: - path = self._uri() + path = self._uri(code) uri = self._to_uri(path) self.open_doc(path, code) result = self.request("textDocument/hover", { @@ -272,10 +279,26 @@ class LspManager: with self._lock: proc = self._procs.get(lang) if proc is None or proc.proc is None or proc.proc.poll() is not None: - 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) + 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] + proc = LspProcess(lang, cmd) proc.start() self._procs[lang] = proc logger.info("LSP %s 已启动: %s", lang, cmd[0])