дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-28 05:22:23 +02:00
On Python files, Extract Method, Inline, Introduce Parameter and module-level Introduce Constant come from pylsp-rope, a companion server the missing-server prompt skipped, so without it these refactorings only said "Cannot perform refactoring". Now, when one of them finds nothing and pylsp-rope isn't installed, the usual missing-tool notification offers to install it (Install restarts the server, so the refactoring works right away). `promptInstall` takes a purpose (code insight, formatter or a refactoring) instead of a formatter flag; a refactoring purpose lets a companion server through, even when another server already serves the files. Opening a project stays quiet. Closes #5149 Co-authored-by: wildcatDownstairs <[email protected]>
127 рядки
5.1 KiB
TypeScript
127 рядки
5.1 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}`;
|
|
|
|
/** What the tool is offered for: code insight, Reformat Code, or the refactoring with this (translated) label. */
|
|
export type InstallPurpose = "codeInsight" | "formatter" | { refactoring: string };
|
|
|
|
/**
|
|
* `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),
|
|
* until one of its refactorings is invoked.
|
|
*/
|
|
export async function promptInstall(
|
|
name: string,
|
|
sticky = true,
|
|
offered: ReadonlySet<string> = new Set(),
|
|
purpose: InstallPurpose = "codeInsight",
|
|
): Promise<boolean> {
|
|
if (!useTools.getState().loaded) await refreshTools();
|
|
const tool = toolByName(name);
|
|
const refactoring = typeof purpose === "object" ? purpose.refactoring : null;
|
|
if (!tool || !tool.canInstall || tool.status !== "missing" || (tool.companion && !refactoring)) return false;
|
|
const position = tool.alternatives.indexOf(name);
|
|
if (position > 0 && tool.alternatives.slice(0, position).some((n) => offered.has(n))) return false;
|
|
const formatter = purpose === "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 && !refactoring && 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: refactoring
|
|
? tx`${refactoring} needs ${tool.displayName}. Install it via ${tool.installMethod}?`
|
|
: 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) },
|
|
],
|
|
});
|
|
})();
|
|
});
|
|
}
|