Файли
openidea/src/features/code-insight/ServerStatus.tsx
T
l0ng-ai 18f28cd804 feat(code-insight): compact Language Services status widget for the current file (#7562)
The status item is a compact icon whose badge reflects the active file's servers; the popup lists the current file's servers first, then the project's others, keeping restart, log and update actions.

Fixes #7489
2026-12-02 23:23:37 +08:00

228 рядки
9.4 KiB
TypeScript

/** Status bar widget listing language servers, with a popup to stop, restart or configure them. */
import { useEffect, useMemo, useRef, useState } from "react";
import clsx from "clsx";
import { getAction, executeAction } from "@/platform/actions";
import { BarButton, Button, Icon, IconButton, Popup, T, fileIconName } from "@/ui";
import { useEditorRegistry } from "@/editor/api";
import { call, commands, isTauri, type ServerId } from "@/platform/ipc";
import { downloadJdkActions } from "@/platform/jdk";
import { relativePath, useProject } from "@/platform/project";
import {
STATUS_LABEL,
canStop,
restartServer,
servedByOther,
serverLabel,
serversSummary,
stopServer,
useServers,
} from "./servers";
import styles from "./ServerStatus.module.css";
import { tx } from "@/platform/i18n";
import { useToolUpdates } from "@/workbench/contributions";
const MAX_WIDGET_ICONS = 4;
export function ServerStatus() {
const map = useServers((s) => s.servers);
const servers = useMemo(() => [...map.values()].sort((a, b) => serverLabel(a).localeCompare(serverLabel(b))), [map]);
const root = useProject((s) => s.project?.root ?? "");
const [open, setOpen] = useState(false);
const ref = useRef<HTMLButtonElement>(null);
const active = useEditorRegistry((s) => s.active?.ctx);
const [association, setAssociation] = useState<{ file: typeof active; root: string; ids: Set<ServerId> }>();
useEffect(() => {
if (!root || !active || !isTauri()) return;
let current = true;
void call(commands.lspDocumentServers(active.doc)).then(
(ids) => {
if (current) setAssociation({ file: active, root, ids: new Set(ids) });
},
() => {
if (current) setAssociation(undefined);
},
);
return () => {
current = false;
};
}, [active, root, map]);
const ids = association?.file === active && association?.root === root ? association.ids : undefined;
const current = servers.filter((s) => ids?.has(s.id));
const others = servers.filter((s) => !ids?.has(s.id));
const summary = serversSummary(current.length ? current : servers);
const currentSummary = current.length ? summary : null;
const shown = current.length > MAX_WIDGET_ICONS ? MAX_WIDGET_ICONS - 1 : current.length;
// features/tools owns installing; without it, fall back to the manual hint text.
const canInstall = !!getAction("tools.install");
const canConfigure = !!getAction("tools.languageServers");
const latestVersions = useToolUpdates((s) => s.latest);
// Installed servers with a newer version; the same tool can back several servers.
const updatable = canInstall ? servers.filter((s) => latestVersions[s.name]) : [];
if (!root && !servers.length) return null;
return (
<>
<BarButton
ref={ref}
aria-expanded={open}
aria-label={currentSummary ? tx`Language servers: ${currentSummary.text}` : tx("Language servers")}
tooltip="Language servers"
onClick={() => setOpen((v) => !v)}
>
<span className={styles.widgetIcons} aria-hidden>
{current.length ? (
current
.slice(0, shown)
.map((server, index) => (
<Icon
key={server.id}
name={index === 0 && active ? fileIconName(active.path) : "file-json"}
size={14}
/>
))
) : (
<Icon name="file-json" size={14} />
)}
{summary && <span className={clsx(styles.dot, styles.indicator, styles[summary.tone])} />}
</span>
{current.length > shown && <span aria-hidden>+{current.length - shown}</span>}
{updatable.length > 0 && (
<Icon
name="arrow-up"
size={12}
className={styles.updateBadge}
label={tx("Language server updates are available")}
/>
)}
</BarButton>
<Popup
open={open}
anchor={ref}
placement="top-end"
offset={6}
onClose={() => setOpen(false)}
aria-label="Language servers"
>
<div className={styles.panel}>
{[
{ label: tx("Running on Current File"), entries: current },
{ label: tx("Running on Other Files"), entries: others },
].map(({ label, entries }) => (
<section key={label} aria-label={label} className={styles.section}>
<div className={styles.header}>{label}</div>
{entries.length === 0 && (
<div className={styles.empty}>
<T>No language servers</T>
</div>
)}
{entries.map((s) => {
// jdtls without a JDK new enough to run it: "Download JDK 25".
const [jdkFix] = s.status === "misconfigured" ? downloadJdkActions(s.message) : [];
const latest = latestVersions[s.name];
return (
<div key={s.id} className={styles.server}>
<div className={styles.line}>
<span
className={clsx(
styles.dot,
styles[s.status === "missing" && servedByOther(s, servers) ? "idle" : toneOf(s.status)],
)}
aria-hidden
/>
<span className={styles.name}>{serverLabel(s)}</span>
<span className={styles.status}>{STATUS_LABEL[s.status]}</span>
<span className={styles.spacer} />
{jdkFix ? (
<Button
size="small"
onClick={() => {
setOpen(false);
jdkFix.run();
}}
>
{jdkFix.label}
</Button>
) : (
<>
{latest && updatable.includes(s) && (
<Button
size="small"
aria-label={tx`Update ${s.name}`}
title={tx`Version ${latest} is available.`}
onClick={() => {
setOpen(false);
void executeAction("tools.install", s.name);
}}
>
<T>Update</T>
</Button>
)}
{s.status === "missing" && canInstall && (
<Button
size="small"
aria-label={tx`Install ${s.name}`}
onClick={() => {
setOpen(false);
void executeAction("tools.install", s.name);
}}
>
<T>Install</T>
</Button>
)}
{canStop(s.status) && (
<IconButton
icon="stop"
size="small"
title={tx`Stop ${serverLabel(s)}`}
onClick={() => void stopServer(s.id)}
/>
)}
{/* A missing server may have been installed outside the IDE since; restarting looks for it again. */}
<IconButton
icon="refresh"
size="small"
title={
s.status === "missing" ? tx`Check for ${s.name} again` : tx`Restart ${serverLabel(s)}`
}
onClick={() => void restartServer(s.id)}
/>
</>
)}
{canConfigure && (
<IconButton
icon="settings"
size="small"
title={tx`Configure ${serverLabel(s)}…`}
onClick={() => {
setOpen(false);
void executeAction("tools.languageServers", s.name);
}}
/>
)}
</div>
{(s.languages.length > 0 || s.root) && (
<div className={styles.meta}>
{s.languages.join(", ")}
{s.root && s.root !== root ? ` · ${relativePath(root, s.root) || s.root}` : ""}
</div>
)}
{s.message && (
<div className={clsx(styles.message, s.status === "missing" && styles.install)}>{s.message}</div>
)}
</div>
);
})}
</section>
))}
</div>
</Popup>
</>
);
}
function toneOf(status: string): "ok" | "busy" | "error" | "idle" {
if (status === "ready") return "ok";
if (status === "starting" || status === "busy") return "busy";
if (status === "crashed" || status === "missing" || status === "misconfigured") return "error";
return "idle";
}