Files
openidea/src/features/tools/missing.ts
T
LU XIANGRUIandwildcatDownstairs 7a3f004235 fix(notifications): take down a failure balloon once a reload or server start succeeds (#4101)
A failed Maven/Gradle reload left its error balloon on screen after a later reload succeeded: syncTool notified on failure under build-sync-<tool> but never expired that key on success. A clean sync now expires it (the Notifications tool window keeps the entry). The same holds for the language-server prompts (not installed, JDK required, outdated build): a server turning ready expires them, for when the fix came from a settings page rather than the prompt's button. expireNotification leaves the state untouched when no balloon is shown.

Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
2026-11-18 10:54:51 +08:00

120 lines
4.6 KiB
TypeScript

/**
* "rust-analyzer is not installed — Install": when a language server reports
* `missing` and we know how to install it, offer a one-click install that
* restarts the server when done. Uses the same notification key as the
* generic code-insight notice so it replaces it.
*/
import { executeAction } from "@/platform/actions";
import { onIdeEvent } from "@/platform/ipc";
import { notifications } from "@/workbench/contributions";
import { expiredKey } from "./expired";
import { jdkRequiredKey } from "./jdk";
import { installTool, refreshTools, SETTINGS_PAGE_ID, toolByName, useTools } from "./store";
import { tx } from "@/platform/i18n";
export const missingKey = (name: string) => `lsp-missing-${name}`;
/**
* `offered`: servers already offered for install. A fallback of one of them (pyright
* after basedpyright) stays quiet: installing the first is enough, and the Language
* Servers… page offers the others. So does a companion server (pylsp-rope's refactorings).
*/
export async function promptInstall(
name: string,
sticky = true,
offered: ReadonlySet<string> = new Set(),
formatter = false,
): Promise<boolean> {
if (!useTools.getState().loaded) await refreshTools();
const tool = toolByName(name);
if (!tool || !tool.canInstall || tool.status !== "missing" || tool.companion) return false;
const position = tool.alternatives.indexOf(name);
if (position > 0 && tool.alternatives.slice(0, position).some((n) => offered.has(n))) return false;
formatter ||= tool.kind === "formatter";
// An installed alternative (typescript-language-server for vtsls) already serves these files.
const served = useTools
.getState()
.tools.some(
(t) =>
t.name !== name &&
t.kind === tool.kind &&
t.status !== "missing" &&
t.languages.some((l) => tool.languages.includes(l)),
);
if (!formatter && served) return false;
const key = missingKey(name);
const langs = tool.languages.length ? tool.languages.join(", ") : "these files";
notifications().notify({
group: "tools",
level: "warning",
title: tx`${tool.displayName} is not installed`,
body: formatter
? tx`Reformat Code needs ${tool.displayName}. Install it via ${tool.installMethod}?`
: tx`Code insight for ${langs} needs ${tool.command}. Install it via ${tool.installMethod}?`,
key,
sticky,
actions: [
{ label: "Install", run: () => void installTool(name, { notificationKey: key }) },
{ label: "Language Servers…", run: () => void executeAction("file.settings", SETTINGS_PAGE_ID) },
],
});
return true;
}
export function installMissingServerPrompts(): () => void {
const prompted = new Set<string>();
const offered = new Set<string>();
// One at a time: a missing server's fallback reports missing right after it, and
// must see whether the server's own notice went up.
let queue = Promise.resolve();
return onIdeEvent("languageServer", (e) => {
if (e.status !== "missing" || prompted.has(e.name)) return;
prompted.add(e.name);
// Async on purpose: lands after the synchronous generic notice and replaces it.
queue = queue
.then(async () => {
if (await promptInstall(e.name, true, offered)) offered.add(e.name);
})
.catch(() => {});
});
}
/**
* A server that starts after all (installed from the Language Servers page, given a JDK,
* updated) takes down the balloons that said it could not.
*/
export function installServerReadyExpiry(): () => void {
return onIdeEvent("languageServer", (e) => {
if (e.status !== "ready") return;
for (const key of [missingKey(e.name), jdkRequiredKey(e.name), expiredKey(e.name)]) notifications().expire?.(key);
});
}
/** A system server that lacks a capability can be replaced by the managed tool. */
export function installServerWarningPrompts(): () => void {
const prompted = new Set<string>();
return onIdeEvent("languageServerWarning", (e) => {
const key = `lsp-warning-${e.name}-${e.installTool}`;
if (prompted.has(key)) return;
prompted.add(key);
void (async () => {
if (!useTools.getState().loaded) await refreshTools();
const tool = toolByName(e.installTool);
notifications().notify({
group: "lsp",
level: "warning",
title: e.name,
body: tx(e.message),
key,
sticky: true,
actions: [
...(tool?.canInstall
? [{ label: "Install", run: () => void installTool(e.installTool, { notificationKey: key }) }]
: []),
{ label: "Language Servers…", run: () => void executeAction("file.settings", SETTINGS_PAGE_ID) },
],
});
})();
});
}