diff --git a/src/features/settings/__tests__/staging.test.ts b/src/features/settings/__tests__/staging.test.ts index ec0c05326..ee2b6f500 100644 --- a/src/features/settings/__tests__/staging.test.ts +++ b/src/features/settings/__tests__/staging.test.ts @@ -4,10 +4,13 @@ import { DEFAULT_SSH_CONFIG } from "@/platform/remoteSettings"; import { SUPPORTED_LOCALES } from "@/platform/i18n"; import { useProject } from "@/platform/project"; import { DEFAULT_SETTINGS, getAt, setAt, validateSettings } from "../model/schema"; +import { DEFAULT_PAGE } from "../model/staging"; +import { lastSettingsPage, rememberSettingsPage } from "../model/lastPage"; import { applySettings, closeSettings, openSettings, + selectPage, pendingChanges, persistChanges, resetPage, @@ -339,3 +342,49 @@ describe("literal map entry staging", () => { expect(useSettingsDialog.getState().staged).toEqual({}); }); }); + +describe("reopening Settings", () => { + beforeEach(() => { + localStorage.clear(); + useProject.setState({ settings: structuredClone(DEFAULT_SETTINGS) as Settings, project: null }); + useSettingsDialog.setState({ pageId: DEFAULT_PAGE, newProjects: false }); + }); + + it("reopens on the page the last session left", () => { + openSettings(); + selectPage("editor.general"); + closeSettings(); + // A new session starts from the default page; the remembered one has to win. + useSettingsDialog.setState({ pageId: DEFAULT_PAGE }); + openSettings(); + expect(useSettingsDialog.getState().pageId).toBe("editor.general"); + }); + + it("remembers the page of a deep link that opens Settings directly", () => { + openSettings("tools.sshConfigurations#add"); + expect(lastSettingsPage()).toBe("tools.sshConfigurations"); + expect(useSettingsDialog.getState().focus).toBe("add"); + }); + + it("keeps Settings for New Projects out of the remembered page", () => { + selectPage("keymap"); + openSettings("languages.java", { newProjects: true }); + selectPage("languages.python"); + expect(lastSettingsPage()).toBe("keymap"); + openSettings(); + expect(useSettingsDialog.getState().pageId).toBe("keymap"); + }); + + it("falls back to the first page when nothing was recorded", () => { + openSettings(); + expect(useSettingsDialog.getState().pageId).toBe(DEFAULT_PAGE); + }); + + it("ignores a recorded value that is not a page id", () => { + rememberSettingsPage("editor.general"); + localStorage.setItem("openidea.settings.lastPage.v1", "not a page!"); + expect(lastSettingsPage()).toBeNull(); + openSettings(); + expect(useSettingsDialog.getState().pageId).toBe(DEFAULT_PAGE); + }); +}); diff --git a/src/features/settings/model/lastPage.ts b/src/features/settings/model/lastPage.ts new file mode 100644 index 000000000..f2fb24bc5 --- /dev/null +++ b/src/features/settings/model/lastPage.ts @@ -0,0 +1,29 @@ +/** + * The page Settings was last shown on (IDEA reopens Settings there). This is UI state, not a + * setting: it stays out of the settings JSON and lives in localStorage, which the separate + * Settings window shares with the workbench through the same origin. + */ +const KEY = "openidea.settings.lastPage.v1"; + +/** Page id characters of every built-in and plugin page (`editor.general`, `tools.sshConfigurations`). */ +const PAGE_ID = /^[\w.-]{1,200}$/; + +/** The page to reopen Settings on, or `null` when nothing usable was recorded. */ +export function lastSettingsPage(): string | null { + try { + const value = localStorage.getItem(KEY); + return value && PAGE_ID.test(value) ? value : null; + } catch { + return null; + } +} + +/** Records the page Settings shows; `null` forgets it. */ +export function rememberSettingsPage(pageId: string | null): void { + try { + if (pageId) localStorage.setItem(KEY, pageId); + else localStorage.removeItem(KEY); + } catch { + // Without storage the dialog opens on the first page; nothing else depends on this. + } +} diff --git a/src/features/settings/model/staging.ts b/src/features/settings/model/staging.ts index 3ff041535..9394cc54c 100644 --- a/src/features/settings/model/staging.ts +++ b/src/features/settings/model/staging.ts @@ -6,6 +6,7 @@ import { create } from "zustand"; import { call, commands, isTauri, type Settings } from "@/platform/ipc"; import { loadSettings, useProject } from "@/platform/project"; +import { lastSettingsPage, rememberSettingsPage } from "./lastPage"; import { deepEqual, getAt, putAt, setAt, validateSettings, type ValidSettings } from "./schema"; export type Scope = "global" | "project"; @@ -112,7 +113,9 @@ export function splitPageTarget(target: string): { pageId: string; focus: string export function openSettings(pageId?: string, opts: { newProjects?: boolean; globalBase?: ValidSettings } = {}) { const s = useSettingsDialog.getState(); const base = validateSettings(useProject.getState().settings); - const target = pageId ? splitPageTarget(pageId) : { pageId: s.pageId, focus: null }; + const newProjects = opts.newProjects ?? false; + // IDEA reopens Settings on the page it was left on; Settings for New Projects has its own pages. + const target = splitPageTarget(pageId ?? (newProjects ? s.pageId : (lastSettingsPage() ?? s.pageId))); useSettingsDialog.setState({ open: true, projectStructureOpen: false, @@ -121,7 +124,7 @@ export function openSettings(pageId?: string, opts: { newProjects?: boolean; glo query: "", base, globalBase: opts.globalBase ?? base, - newProjects: opts.newProjects ?? false, + newProjects, staged: {}, stagedSecrets: {}, stagedExternal: {}, @@ -129,6 +132,7 @@ export function openSettings(pageId?: string, opts: { newProjects?: boolean; glo saving: false, error: null, }); + if (!newProjects) rememberSettingsPage(target.pageId); if (!opts.globalBase) void loadGlobalBase(); } @@ -214,7 +218,9 @@ export function closeSettings() { /** Shows `pageId` (`page#focus` also selects `focus` on it). */ export function selectPage(pageId: string) { - useSettingsDialog.setState(splitPageTarget(pageId)); + const target = splitPageTarget(pageId); + useSettingsDialog.setState(target); + if (!useSettingsDialog.getState().newProjects) rememberSettingsPage(target.pageId); } export function setQuery(query: string) {