From c3034b25e8ef5c5c8de2d342f06c8eb59e857dbc Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 18 Nov 2026 21:32:25 +0800 Subject: [PATCH] Stage TODO and Vim preferences until Settings applies them (#4283) --- .../__tests__/todoVimStaging.test.tsx | 109 ++++++++++++++++++ src/features/todo/TodoSettingsPage.tsx | 37 +++--- src/features/vim/VimSettingsPage.tsx | 34 +++--- 3 files changed, 148 insertions(+), 32 deletions(-) create mode 100644 src/features/settings/__tests__/todoVimStaging.test.tsx diff --git a/src/features/settings/__tests__/todoVimStaging.test.tsx b/src/features/settings/__tests__/todoVimStaging.test.tsx new file mode 100644 index 000000000..d8ad056c3 --- /dev/null +++ b/src/features/settings/__tests__/todoVimStaging.test.tsx @@ -0,0 +1,109 @@ +import { act, cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { DEFAULT_TODO_SETTINGS } from "@/features/todo/model"; +import { activateSettingsPages as activateTodoSettings } from "@/features/todo/settingsPages"; +import { useTodoSettings } from "@/features/todo/store"; +import { DEFAULT_VIM_SETTINGS, useVimSettings, VIM_SETTINGS_KEY } from "@/features/vim/settings"; +import { activateSettingsPages as activateVimSettings } from "@/features/vim/settingsPages"; +import type { Settings } from "@/platform/ipc"; +import { useProject } from "@/platform/project"; +import { SettingsDialog } from "../dialog/SettingsDialog"; +import { DEFAULT_SETTINGS } from "../model/schema"; +import { closeSettings, openSettings } from "../model/staging"; + +const TODO_SETTINGS_KEY = "openidea.todo.v1"; +const dialog = () => within(screen.getByRole("dialog", { name: "Settings" })); +const button = (name: string) => dialog().getByRole("button", { name }); + +describe("TODO and Vim preferences in Settings", () => { + const previousProject = useProject.getState(); + let dispose: (() => void)[]; + + beforeEach(() => { + localStorage.clear(); + useTodoSettings.setState(DEFAULT_TODO_SETTINGS, true); + useVimSettings.setState(DEFAULT_VIM_SETTINGS, true); + useProject.setState({ settings: structuredClone(DEFAULT_SETTINGS) as Settings, project: null }); + dispose = [activateTodoSettings(), activateVimSettings()]; + render(); + }); + + afterEach(() => { + cleanup(); + closeSettings(); + dispose.forEach((off) => off()); + useProject.setState(previousProject); + }); + + it.each([ + { + page: "editor.todo", + label: "Only in comments", + storage: TODO_SETTINGS_KEY, + field: "commentsOnly", + current: () => useTodoSettings.getState().commentsOnly, + }, + { + page: "editor.vim", + label: "Enable Vim emulation", + storage: VIM_SETTINGS_KEY, + field: "enabled", + current: () => useVimSettings.getState().enabled, + }, + ])( + "discards uncommitted $label changes on Cancel and saves on Apply", + async ({ page, label, storage, field, current }) => { + const initial = current(); + const open = async () => { + act(() => openSettings(page)); + return dialog().findByRole("checkbox", { name: label }); + }; + + fireEvent.click(await open()); + expect(button("Apply")).toBeEnabled(); + expect(current()).toBe(initial); + expect(localStorage.getItem(storage)).toBeNull(); + fireEvent.click(button("Cancel")); + + const reopened = await open(); + expect(reopened.checked).toBe(initial); + fireEvent.click(reopened); + await act(async () => void fireEvent.click(button("Apply"))); + expect(current()).toBe(!initial); + expect(JSON.parse(localStorage.getItem(storage)!)).toMatchObject({ [field]: !initial }); + expect(button("Apply")).toBeDisabled(); + + fireEvent.click(reopened); + fireEvent.click(button("Cancel")); + expect((await open()).checked).toBe(!initial); + }, + ); + + it("stages TODO pattern edits through page Reset and OK", async () => { + act(() => openSettings("editor.todo")); + const add = await dialog().findByRole("button", { name: "Add Pattern" }); + fireEvent.click(add); + expect(useTodoSettings.getState().patterns).toEqual(DEFAULT_TODO_SETTINGS.patterns); + expect(button("Apply")).toBeEnabled(); + fireEvent.click(button("Reset")); + expect(button("Apply")).toBeDisabled(); + + fireEvent.click(add); + const keyword = dialog().getAllByRole("textbox", { name: "Keyword" }).at(-1)!; + fireEvent.change(keyword, { target: { value: "REVIEW" } }); + await act(async () => void fireEvent.click(button("OK"))); + await waitFor(() => expect(screen.queryByRole("dialog", { name: "Settings" })).not.toBeInTheDocument()); + expect(useTodoSettings.getState().patterns.at(-1)?.keyword).toBe("REVIEW"); + }); + + it("stages Vim conflict handlers and lets their reset clear the pending change", async () => { + act(() => openSettings("editor.vim")); + fireEvent.click(await dialog().findByRole("button", { name: "Handler for Escape" })); + fireEvent.click(screen.getByRole("option", { name: "Vim" })); + expect(button("Apply")).toBeEnabled(); + expect(useVimSettings.getState().handlers).toEqual({}); + fireEvent.click(button("Reset to Defaults")); + expect(button("Apply")).toBeDisabled(); + expect(localStorage.getItem(VIM_SETTINGS_KEY)).toBeNull(); + }); +}); diff --git a/src/features/todo/TodoSettingsPage.tsx b/src/features/todo/TodoSettingsPage.tsx index faedf9fd2..771d59bfe 100644 --- a/src/features/todo/TodoSettingsPage.tsx +++ b/src/features/todo/TodoSettingsPage.tsx @@ -1,5 +1,6 @@ /** Settings › Editor › TODO: patterns, colours, matching options. */ import { Button, Checkbox, IconButton, Input, Select, T } from "@/ui"; +import { useSettingsForm } from "@/workbench/contributions"; import { DEFAULT_PATTERNS, type TodoPattern } from "./model"; import { updateTodoSettings, useTodoSettings } from "./store"; import styles from "./TodoSettingsPage.module.css"; @@ -15,24 +16,28 @@ const COLORS = [ ]; export function TodoSettingsPage() { - const s = useTodoSettings(); - const setPatterns = (patterns: TodoPattern[]) => updateTodoSettings({ patterns }); + const form = useSettingsForm(); + const saved = useTodoSettings(); + const caseSensitive = form.useExternal("caseSensitive", saved.caseSensitive, async (value) => { + updateTodoSettings({ caseSensitive: value }); + return value; + }); + const commentsOnly = form.useExternal("commentsOnly", saved.commentsOnly, async (value) => { + updateTodoSettings({ commentsOnly: value }); + return value; + }); + const { value: patterns, set: setPatterns } = form.useExternal("patterns", saved.patterns, async (value) => { + updateTodoSettings({ patterns: value }); + return value; + }); const patch = (id: string, p: Partial) => - setPatterns(s.patterns.map((x) => (x.id === id ? { ...x, ...p } : x))); + setPatterns(patterns.map((x) => (x.id === id ? { ...x, ...p } : x))); return (
- updateTodoSettings({ caseSensitive: v })} - label="Case sensitive" - /> - updateTodoSettings({ commentsOnly: v })} - label="Only in comments" - /> + +
@@ -44,14 +49,14 @@ export function TodoSettingsPage() { title="Add Pattern" onClick={() => setPatterns([ - ...s.patterns, + ...patterns, { id: `p${Date.now().toString(36)}`, keyword: "NOTE", color: "var(--success)", enabled: true }, ]) } />
- {s.patterns.map((p) => ( + {patterns.map((p) => (
setPatterns(s.patterns.filter((x) => x.id !== p.id))} + onClick={() => setPatterns(patterns.filter((x) => x.id !== p.id))} />
))} diff --git a/src/features/vim/VimSettingsPage.tsx b/src/features/vim/VimSettingsPage.tsx index bc15ae9c3..47d17d8f5 100644 --- a/src/features/vim/VimSettingsPage.tsx +++ b/src/features/vim/VimSettingsPage.tsx @@ -9,6 +9,7 @@ import { loadRcFile, useVimRc } from "./rcFile"; import { updateVimSettings, useVimSettings, type Handler } from "./settings"; import styles from "./Vim.module.css"; import { tx } from "@/platform/i18n"; +import { useSettingsForm } from "@/workbench/contributions"; const HANDLERS = [ { value: "vim" as const, label: "Vim" }, @@ -16,12 +17,21 @@ const HANDLERS = [ ]; export function VimSettingsPage() { - const s = useVimSettings(); + const form = useSettingsForm(); + const saved = useVimSettings(); + const enabled = form.useExternal("enabled", saved.enabled, async (value) => { + updateVimSettings({ enabled: value }); + return value; + }); + const { value: handlers, set: setHandlers } = form.useExternal("handlers", saved.handlers, async (value) => { + updateVimSettings({ handlers: value }); + return value; + }); const rc = useVimRc(); const keymap = useKeymap(); const actions = useActions((a) => a.actions); // eslint-disable-next-line react-hooks/exhaustive-deps -- the keymap store is the input of effectiveBindings() - const rows = useMemo(() => conflictRows(effectiveBindings(), s.handlers), [keymap, s.handlers]); + const rows = useMemo(() => conflictRows(effectiveBindings(), handlers), [keymap, handlers]); useEffect(() => { if (!useVimRc.getState().loaded) void loadRcFile(); @@ -34,19 +44,15 @@ export function VimSettingsPage() { }; const setHandler = (stroke: string, handler: Handler) => { - const handlers = { ...s.handlers }; - if (VIM_KEYS.find((k) => k.stroke === stroke)?.handler === handler) delete handlers[stroke]; - else handlers[stroke] = handler; - updateVimSettings({ handlers }); + const next = { ...handlers }; + if (VIM_KEYS.find((k) => k.stroke === stroke)?.handler === handler) delete next[stroke]; + else next[stroke] = handler; + setHandlers(next); }; return (
- updateVimSettings({ enabled })} - label="Enable Vim emulation" - /> +
@@ -137,11 +143,7 @@ export function VimSettingsPage() {
-