mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
Stage TODO and Vim preferences until Settings applies them (#4283)
This commit is contained in:
@@ -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(<SettingsDialog />);
|
||||
});
|
||||
|
||||
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<HTMLInputElement>("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();
|
||||
});
|
||||
});
|
||||
@@ -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<TodoPattern>) =>
|
||||
setPatterns(s.patterns.map((x) => (x.id === id ? { ...x, ...p } : x)));
|
||||
setPatterns(patterns.map((x) => (x.id === id ? { ...x, ...p } : x)));
|
||||
|
||||
return (
|
||||
<div className={styles.page}>
|
||||
<div className={styles.options}>
|
||||
<Checkbox
|
||||
checked={s.caseSensitive}
|
||||
onChange={(v) => updateTodoSettings({ caseSensitive: v })}
|
||||
label="Case sensitive"
|
||||
/>
|
||||
<Checkbox
|
||||
checked={s.commentsOnly}
|
||||
onChange={(v) => updateTodoSettings({ commentsOnly: v })}
|
||||
label="Only in comments"
|
||||
/>
|
||||
<Checkbox checked={caseSensitive.value} onChange={caseSensitive.set} label="Case sensitive" />
|
||||
<Checkbox checked={commentsOnly.value} onChange={commentsOnly.set} label="Only in comments" />
|
||||
</div>
|
||||
<div className={styles.header}>
|
||||
<span>
|
||||
@@ -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 },
|
||||
])
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<div className={styles.list} role="list" aria-label={tx("TODO patterns")}>
|
||||
{s.patterns.map((p) => (
|
||||
{patterns.map((p) => (
|
||||
<div key={p.id} className={styles.row} role="listitem">
|
||||
<Checkbox
|
||||
checked={p.enabled}
|
||||
@@ -76,7 +81,7 @@ export function TodoSettingsPage() {
|
||||
icon="trash"
|
||||
size="small"
|
||||
title="Remove Pattern"
|
||||
onClick={() => setPatterns(s.patterns.filter((x) => x.id !== p.id))}
|
||||
onClick={() => setPatterns(patterns.filter((x) => x.id !== p.id))}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -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 (
|
||||
<div className={styles.page}>
|
||||
<Checkbox
|
||||
checked={s.enabled}
|
||||
onChange={(enabled) => updateVimSettings({ enabled })}
|
||||
label="Enable Vim emulation"
|
||||
/>
|
||||
<Checkbox checked={enabled.value} onChange={enabled.set} label="Enable Vim emulation" />
|
||||
|
||||
<section className={styles.section} aria-label={tx("Vim rc file")}>
|
||||
<div className={styles.header}>
|
||||
@@ -137,11 +143,7 @@ export function VimSettingsPage() {
|
||||
</tbody>
|
||||
</table>
|
||||
<div>
|
||||
<Button
|
||||
size="small"
|
||||
onClick={() => updateVimSettings({ handlers: {} })}
|
||||
disabled={!Object.keys(s.handlers).length}
|
||||
>
|
||||
<Button size="small" onClick={() => setHandlers({})} disabled={!Object.keys(handlers).length}>
|
||||
Reset to Defaults
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user