Файли
openidea/src/features/code-insight/servers.ts
T
l0ng-ai 5e3e1f939d fix(lsp): Run jdtls on a JDK 21+ and offer Install in the server popup (#2)
* fix(lsp): Run jdtls on a JDK 21+ and offer Install in the server popup

jdtls refuses to start on Java < 21, and its launcher took whatever
JAVA_HOME / PATH pointed at, so it crash-looped with a Python traceback
when that was an older JDK (#1). Pick the first detected JDK 21+ and pass
it via JAVA_HOME; projects still compile against their own runtimes.
When none is found, report a clear message instead of launching, and
rescan on restart so a newly installed JDK is picked up. JDK detection
now also covers the java on PATH and Windows vendor directories.

The Language Servers status popup showed the manual install hint for
missing servers; it now has an Install button that runs tools.install,
which opens the Language Servers settings page for tools the IDE cannot
install itself.

Fixes #1

Claude-Session: https://claude.ai/code/session_01B65qLEKnkeYTGdbwhLpjCu

* fix(lsp): Address review of the jdtls runtime check

- Cache the JDK scan and rescan at most every 10s while no JDK 21+ is
  found, refreshing the shared list instead of discarding it; skip
  duplicate Windows Program Files roots.
- Check `launch_error` in the manager (inside the blocking launch task)
  rather than in ProcessLauncher, and skip such servers in batch
  inspection.
- Keep the install hint visible next to the Install button; when the tool
  list failed to load, try the install so the error is reported.
- Don't let a server snapshot requested before a status event overwrite
  that newer status (left jdtls showing "Starting…" while missing).
2026-10-23 11:38:51 +08:00

141 рядки
4.5 KiB
TypeScript

/** Language server status: `lsp_servers` snapshot + `languageServer` events. */
import { getAction } from "@/platform/actions";
import { create } from "zustand";
import type { ServerId, ServerInfo, ServerStatus } from "@/bindings";
import { call, commands, isTauri, onIdeEvent } from "@/platform/ipc";
import { useProject } from "@/platform/project";
import { notifications } from "@/workbench/contributions";
export interface ServerEntry {
id: ServerId;
name: string;
status: ServerStatus;
message: string | null;
languages: string[];
root: string | null;
}
interface ServersState {
servers: ReadonlyMap<ServerId, ServerEntry>;
}
export const useServers = create<ServersState>(() => ({ servers: new Map() }));
const notifiedMissing = new Set<string>();
/** Event count at each server's latest event, so a snapshot taken earlier doesn't undo it. */
let eventSeq = 0;
const lastEvent = new Map<ServerId, number>();
function notifyMissing(e: ServerEntry) {
if (notifiedMissing.has(e.name)) return;
notifiedMissing.add(e.name);
// features/tools shows the same notification with an Install button.
if (getAction("tools.install")) return;
notifications().notify({
group: "lsp",
level: "warning",
title: `Language server '${e.name}' is not installed`,
body: e.message ?? "Code insight features are unavailable for these files until it is installed.",
key: `lsp-missing-${e.name}`,
});
}
export function upsertServer(entry: Partial<ServerEntry> & { id: ServerId; name: string; status: ServerStatus }) {
const prev = useServers.getState().servers.get(entry.id);
const next: ServerEntry = {
languages: prev?.languages ?? [],
root: prev?.root ?? null,
message: null,
...prev,
...entry,
};
useServers.setState((s) => ({ servers: new Map(s.servers).set(entry.id, next) }));
if (next.status === "missing") notifyMissing(next);
}
export async function refreshServers() {
if (!useProject.getState().project) {
useServers.setState({ servers: new Map() });
return;
}
if (!isTauri()) return;
try {
const since = eventSeq;
const list: ServerInfo[] = await call(commands.lspServers());
const prev = useServers.getState().servers;
const map = new Map<ServerId, ServerEntry>();
for (const s of list) {
const p = prev.get(s.id);
const newer = p && (lastEvent.get(s.id) ?? 0) > since;
map.set(s.id, {
id: s.id,
name: s.name,
status: newer ? p.status : s.status,
languages: s.languages,
root: s.root,
message: p?.message ?? null,
});
}
useServers.setState({ servers: map });
for (const e of map.values()) if (e.status === "missing") notifyMissing(e);
} catch {
// No project / backend not ready; events will fill in.
}
}
export async function restartServer(id: ServerId) {
try {
await call(commands.lspRestart(id));
} catch (e) {
notifications().notify({
group: "lsp",
level: "error",
title: "Failed to restart language server",
body: e instanceof Error ? e.message : String(e),
});
}
}
export function installServerEvents(): () => void {
const off = onIdeEvent("languageServer", (e) => {
const known = useServers.getState().servers.has(e.server);
lastEvent.set(e.server, ++eventSeq);
upsertServer({ id: e.server, name: e.name, status: e.status, message: e.message });
if (!known) void refreshServers();
});
let root = useProject.getState().project?.root ?? null;
void refreshServers();
const unsub = useProject.subscribe((s) => {
const r = s.project?.root ?? null;
if (r === root) return;
root = r;
notifiedMissing.clear();
void refreshServers();
});
return () => {
off();
unsub();
};
}
export const STATUS_LABEL: Record<ServerStatus, string> = {
starting: "Starting…",
ready: "Running",
busy: "Busy",
crashed: "Crashed",
stopped: "Stopped",
missing: "Not installed",
};
/** One-line summary for the status bar. */
export function serversSummary(servers: readonly ServerEntry[]): { text: string; tone: "ok" | "busy" | "error" | "idle" } | null {
if (!servers.length) return null;
const bad = servers.filter((s) => s.status === "crashed" || s.status === "missing");
const busy = servers.filter((s) => s.status === "starting" || s.status === "busy");
const tone = bad.length ? "error" : busy.length ? "busy" : servers.some((s) => s.status === "ready") ? "ok" : "idle";
const names = [...new Set(servers.map((s) => s.name))];
const text = names.length === 1 ? names[0]! : `${names[0]} +${names.length - 1}`;
return { text, tone };
}