From a4866b032391578beb970eddcc15f308e8eb8d11 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:32:49 +0800 Subject: [PATCH] feat(tools): show language server updates in the status bar The Language Servers status bar widget marks that an installed server has a newer version, and its row in the popup gets an Update button. Updates are looked up a minute after startup, at most once a day; the result is shared by the windows and drops what was installed. --- .../code-insight/ServerStatus.module.css | 6 ++ .../code-insight/ServerStatus.test.tsx | 29 +++++++++- src/features/code-insight/ServerStatus.tsx | 28 ++++++++- src/features/tools/__tests__/tools.test.tsx | 45 +++++++++++++- src/features/tools/index.ts | 20 ++++++- src/features/tools/store.ts | 58 ++++++++++++++++++- src/platform/locales/text/zh.json | 3 +- src/workbench/contributions.ts | 6 ++ 8 files changed, 188 insertions(+), 7 deletions(-) diff --git a/src/features/code-insight/ServerStatus.module.css b/src/features/code-insight/ServerStatus.module.css index d11c212c1..3082d9a11 100644 --- a/src/features/code-insight/ServerStatus.module.css +++ b/src/features/code-insight/ServerStatus.module.css @@ -83,3 +83,9 @@ -webkit-user-select: text; user-select: text; } + +/* Some server has a newer version (Update in its row). */ +.updateBadge { + margin-left: 4px; + color: var(--accent); +} diff --git a/src/features/code-insight/ServerStatus.test.tsx b/src/features/code-insight/ServerStatus.test.tsx index d32ee6b2c..ce3dfb64a 100644 --- a/src/features/code-insight/ServerStatus.test.tsx +++ b/src/features/code-insight/ServerStatus.test.tsx @@ -3,6 +3,7 @@ import { fireEvent, render, screen } from "@testing-library/react"; import { registerAction } from "@/platform/actions"; import { ServerStatus } from "./ServerStatus"; import { useServers, type ServerEntry } from "./servers"; +import { useToolUpdates } from "@/workbench/contributions"; const ISSUE_49 = "jdtls needs JDK 25 or newer to run (found JDK 8, 17, 21). Download JDK 25, or pick an installed one in Settings › Languages & Frameworks › Java › Language Server › JDK (it restarts automatically); the project can still target an older Java version."; @@ -10,9 +11,35 @@ const ISSUE_49 = const server = (status: ServerEntry["status"], message: string | null): ServerEntry => ({ id: 1, name: "jdtls", status, message, languages: ["Java"], root: null }) as unknown as ServerEntry; -afterEach(() => useServers.setState({ servers: new Map() })); +afterEach(() => { + useServers.setState({ servers: new Map() }); + useToolUpdates.setState({ latest: {} }); +}); describe("Language server status popup", () => { + it("marks a server with a newer version and updates it from its row (#7365)", () => { + const install = vi.fn(); + const off = registerAction({ id: "tools.install", title: "Install", run: ({ arg }) => install(arg) }); + useServers.setState({ servers: new Map([[1 as never, server("ready", null)]]) }); + useToolUpdates.setState({ latest: { jdtls: "1.64.0" } }); + render(); + expect(screen.getByLabelText("Language server updates are available")).toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: /Language servers/ })); + fireEvent.click(screen.getByRole("button", { name: "Update jdtls" })); + expect(install).toHaveBeenCalledWith("jdtls"); + off(); + }); + + it("shows no update mark without one", () => { + const off = registerAction({ id: "tools.install", title: "Install", run: () => {} }); + useServers.setState({ servers: new Map([[1 as never, server("ready", null)]]) }); + render(); + expect(screen.queryByLabelText("Language server updates are available")).toBeNull(); + fireEvent.click(screen.getByRole("button", { name: /Language servers/ })); + expect(screen.queryByRole("button", { name: "Update jdtls" })).toBeNull(); + off(); + }); + it("offers Download JDK N when jdtls can't start for want of a JDK", () => { const run = vi.fn(); const off = registerAction({ id: "java.downloadJdk", title: "Download JDK…", run: ({ arg }) => run(arg) }); diff --git a/src/features/code-insight/ServerStatus.tsx b/src/features/code-insight/ServerStatus.tsx index be5bea54e..f90ff13f2 100644 --- a/src/features/code-insight/ServerStatus.tsx +++ b/src/features/code-insight/ServerStatus.tsx @@ -2,7 +2,7 @@ import { useMemo, useRef, useState } from "react"; import clsx from "clsx"; import { getAction, executeAction } from "@/platform/actions"; -import { BarButton, Button, IconButton, Popup, T } from "@/ui"; +import { BarButton, Button, Icon, IconButton, Popup, T } from "@/ui"; import { downloadJdkActions } from "@/platform/jdk"; import { relativePath, useProject } from "@/platform/project"; import { @@ -17,6 +17,7 @@ import { } from "./servers"; import styles from "./ServerStatus.module.css"; import { tx } from "@/platform/i18n"; +import { useToolUpdates } from "@/workbench/contributions"; export function ServerStatus() { const map = useServers((s) => s.servers); @@ -28,6 +29,9 @@ export function ServerStatus() { // 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 (!summary) return null; return ( <> @@ -40,6 +44,14 @@ export function ServerStatus() { > {summary.text} + {updatable.length > 0 && ( + + )} { // 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 ( @@ -81,6 +94,19 @@ export function ServerStatus() { ) : ( <> + {latest && updatable.includes(s) && ( + { + setOpen(false); + void executeAction("tools.install", s.name); + }} + > + Update + + )} {s.status === "missing" && canInstall && ( { afterEach(() => setToolsBackend(null)); +describe("tool updates (#7365)", () => { + afterEach(() => localStorage.removeItem("openidea.tools.updates.v1")); + + it("looks once a day, shows the last lookup meanwhile, and publishes it to the workbench", async () => { + let stop = publishToolUpdates(); + backend.available = [{ name: "rust-analyzer", current: "1.0", latest: "1.1" }]; + const updates = vi.spyOn(backend, "updates"); + await loadToolUpdates(1_000); + expect(updates).toHaveBeenCalledTimes(1); + expect(useToolUpdates.getState().latest).toEqual({ "rust-analyzer": "1.1" }); + + // Another window starting within the day: the stored result, no lookup. + stop(); + useTools.setState({ updates: {} }); + useToolUpdates.setState({ latest: {} }); + stop = publishToolUpdates(); + const at = JSON.parse(localStorage.getItem("openidea.tools.updates.v1")!).at as number; + await loadToolUpdates(at + UPDATES_EVERY_MS - 1); + expect(updates).toHaveBeenCalledTimes(1); + expect(useToolUpdates.getState().latest).toEqual({ "rust-analyzer": "1.1" }); + + // Installing the update drops it, from the stored copy too. + await refreshTools(); + await installTool("rust-analyzer"); + expect(useToolUpdates.getState().latest).toEqual({}); + expect(JSON.parse(localStorage.getItem("openidea.tools.updates.v1")!).updates).toEqual([]); + + await loadToolUpdates(at + UPDATES_EVERY_MS); + expect(updates).toHaveBeenCalledTimes(2); + stop(); + }); +}); + describe("tools store", () => { it("installs, streams progress, restarts servers and notifies", async () => { await refreshTools(); diff --git a/src/features/tools/index.ts b/src/features/tools/index.ts index 6a2c2b57e..984a07636 100644 --- a/src/features/tools/index.ts +++ b/src/features/tools/index.ts @@ -19,7 +19,23 @@ import { installCssModulesHint } from "./cssModules"; import { installXcodeBspHint } from "./xcodeBsp"; import { installStyledTemplatesHint } from "./styledTemplates"; import { activateSettingsPages } from "./settingsPages"; -import { installTool, refreshTools, SETTINGS_PAGE_ID, toolByName, useTools } from "./store"; +import { + installTool, + loadToolUpdates, + publishToolUpdates, + refreshTools, + SETTINGS_PAGE_ID, + toolByName, + useTools, +} from "./store"; + +/** After startup has settled: a lookup goes to every tool's registry. */ +const UPDATES_DELAY_MS = 60_000; + +function lookForToolUpdatesLater(): () => void { + const timer = setTimeout(() => void loadToolUpdates(), UPDATES_DELAY_MS); + return () => clearTimeout(timer); +} export function activate(): () => void { const disposers = [ @@ -80,6 +96,8 @@ export function activate(): () => void { }, }, ]), + publishToolUpdates(), + lookForToolUpdatesLater(), installMissingServerPrompts(), installServerReadyExpiry(), installServerWarningPrompts(), diff --git a/src/features/tools/store.ts b/src/features/tools/store.ts index be924cb34..811661a69 100644 --- a/src/features/tools/store.ts +++ b/src/features/tools/store.ts @@ -3,7 +3,7 @@ import { create } from "zustand"; import type { RequestId, ToolInfo, ToolUpdate } from "@/bindings"; import { executeAction } from "@/platform/actions"; import { isCancelled, nextRequestId } from "@/platform/ipc"; -import { notifications } from "@/workbench/contributions"; +import { notifications, useToolUpdates } from "@/workbench/contributions"; import { cancelTask, queueTask, upsertTask, useTasks } from "@/workbench/tasks"; import { toolsBackend } from "./backend"; import { tx } from "@/platform/i18n"; @@ -69,13 +69,67 @@ export function refreshTools(): Promise { let checkingUpdates: Promise | null = null; +/** The last lookup's result, shared by the windows: `{ at, updates }`. */ +const UPDATES_KEY = "openidea.tools.updates.v1"; +/** How long a lookup stands before the next window start looks again (#7365). */ +export const UPDATES_EVERY_MS = 24 * 60 * 60 * 1000; + +interface CachedUpdates { + at: number; + updates: ToolUpdate[]; +} + +function cachedUpdates(): CachedUpdates | null { + try { + const cached = JSON.parse(localStorage.getItem(UPDATES_KEY) ?? "null") as CachedUpdates | null; + return cached && typeof cached.at === "number" && Array.isArray(cached.updates) ? cached : null; + } catch { + return null; + } +} + +function saveUpdates(at: number, updates: Readonly>) { + try { + localStorage.setItem(UPDATES_KEY, JSON.stringify({ at, updates: Object.values(updates) })); + } catch { + // Storage full or unavailable: the next start looks again. + } +} + +/** + * Shows the updates the last lookup found, and looks again when it is older than a day: the + * status bar's Language Servers says which servers have one without opening Settings. + */ +export function loadToolUpdates(now = Date.now()): Promise { + const cached = cachedUpdates(); + if (cached) useTools.setState({ updates: Object.fromEntries(cached.updates.map((u) => [u.name, u])) }); + return cached && now - cached.at < UPDATES_EVERY_MS ? Promise.resolve() : checkToolUpdates(); +} + +/** Publishes the found updates to the workbench, and keeps the shared copy without installed ones. */ +export function publishToolUpdates(): () => void { + const publish = (updates: Readonly>) => + useToolUpdates.setState({ + latest: Object.fromEntries(Object.values(updates).map((u) => [u.name, u.latest])), + }); + publish(useTools.getState().updates); + return useTools.subscribe((s, prev) => { + if (s.updates === prev.updates) return; + publish(s.updates); + const cached = cachedUpdates(); + if (cached) saveUpdates(cached.at, s.updates); + }); +} + /** Looks for newer versions of the tools the IDE installed; offline, none are shown. */ export function checkToolUpdates(): Promise { checkingUpdates ??= (async () => { useTools.setState({ checkingUpdates: true }); try { const found = await toolsBackend().updates(nextRequestId()); - useTools.setState({ updates: Object.fromEntries(found.map((u) => [u.name, u])) }); + const updates = Object.fromEntries(found.map((u) => [u.name, u])); + saveUpdates(Date.now(), updates); + useTools.setState({ updates }); } catch { // A failed lookup leaves the last result. } finally { diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 258458a12..101acfb3d 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -11985,5 +11985,6 @@ "Product Name:": "产品名称:", "Use letters, digits, spaces, hyphens and underscores, starting with a letter, digit or underscore": "只能使用字母、数字、空格、连字符和下划线,并以字母、数字或下划线开头", "iOS": "iOS", - "macOS": "macOS" + "macOS": "macOS", + "Language server updates are available": "语言服务器有可用更新" } diff --git a/src/workbench/contributions.ts b/src/workbench/contributions.ts index e0acb0bab..29f968aff 100644 --- a/src/workbench/contributions.ts +++ b/src/workbench/contributions.ts @@ -242,6 +242,12 @@ export interface EditorDocumentLifecycle { /** Bumped by `notifyTabTitlesChanged`; tab labels whose `title(input)` changed re-render. */ export const useEditorTabTitles = create<{ revision: number }>(() => ({ revision: 0 })); +/** + * The newer version of each installed language server or debugger that has one, by tool name: + * published by features/tools, shown where servers are listed (the status bar's Language Servers). + */ +export const useToolUpdates = create<{ latest: Readonly> }>(() => ({ latest: {} })); + /** An editor kind's tab title may have changed outside the tab state (e.g. a renamed agent task). */ export function notifyTabTitlesChanged() { useEditorTabTitles.setState((s) => ({ revision: s.revision + 1 }));