дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 01:31:41 +02:00
feat(spelling): save typos to custom dictionaries with a default dictionary setting (#5466)
Co-authored-by: muutot <[email protected]>
Цей коміт міститься в:
1 джерело
ea7ec41064
коміт
bd0f28cd3b
16 files changed
+441
-22
No files matched your search
@@ -14,12 +14,19 @@ const settingsSet = vi.fn(async (_scope: string, _key: string, value: string) =>
|
||||
status: "ok" as const,
|
||||
data: { spelling: { ideWords: [], projectWords: JSON.parse(value), customDictionaries: [] } } as unknown as Settings,
|
||||
}));
|
||||
const spellSaveToDictionary = vi.fn(async () => ({ status: "ok" as const, data: "team.dic" }));
|
||||
vi.mock("@/platform/ipc", async (orig) => {
|
||||
const actual = await orig<typeof import("@/platform/ipc")>();
|
||||
return {
|
||||
...actual,
|
||||
isTauri: () => true,
|
||||
commands: { ...actual.commands, spellCheck, settingsSet, cancelRequest: vi.fn(async () => {}) },
|
||||
commands: {
|
||||
...actual.commands,
|
||||
spellCheck,
|
||||
settingsSet,
|
||||
spellSaveToDictionary,
|
||||
cancelRequest: vi.fn(async () => {}),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
@@ -78,10 +85,12 @@ describe("Typo inspection", () => {
|
||||
|
||||
view.dispatch({ selection: { anchor: 5 } });
|
||||
expect(spellingIntentions(view).map((i) => i.label)).toEqual(["Typo: Change to…", "Save 'mesage' to dictionary"]);
|
||||
// IDEA's → menu: each dictionary level and the inspection's controls.
|
||||
// IDEA's → menu: each dictionary level, each configured custom dictionary
|
||||
// (the settings page when there is none) and the inspection's controls.
|
||||
expect(spellingIntentions(view)[0]!.submenu?.map((i) => ("label" in i ? i.label : "-"))).toEqual([
|
||||
"Save to project-level dictionary",
|
||||
"Save to application-level dictionary",
|
||||
"Add custom dictionary…",
|
||||
"-",
|
||||
"Edit inspection profile setting",
|
||||
"Disable inspection",
|
||||
@@ -97,6 +106,67 @@ describe("Typo inspection", () => {
|
||||
expect(spellCheck).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("offers each configured custom dictionary and saves through the backend", async () => {
|
||||
vi.useFakeTimers();
|
||||
useProject.setState({
|
||||
project: { root: "/p" } as never,
|
||||
settings: { spelling: { customDictionaries: ["~/words/team.dic", " docs/local.txt "] } } as never,
|
||||
});
|
||||
const view = editor("// mesage");
|
||||
await settle();
|
||||
view.dispatch({ selection: { anchor: 5 } });
|
||||
expect(spellingIntentions(view)[0]!.submenu?.map((i) => ("label" in i ? i.label : "-"))).toEqual([
|
||||
"Save to project-level dictionary",
|
||||
"Save to application-level dictionary",
|
||||
"Save to '~/words/team.dic'",
|
||||
"Save to 'docs/local.txt'",
|
||||
"-",
|
||||
"Edit inspection profile setting",
|
||||
"Disable inspection",
|
||||
]);
|
||||
const save = spellingIntentions(view)[0]!.submenu!.find(
|
||||
(i) => "id" in i && i.id === "spelling.saveCustom.docs/local.txt",
|
||||
);
|
||||
if (!save || !("run" in save) || typeof save.run !== "function") throw new Error("no save row");
|
||||
await save.run();
|
||||
expect(spellSaveToDictionary).toHaveBeenCalledWith("docs/local.txt", "mesage");
|
||||
// The dictionary file changed: the file is re-checked.
|
||||
await settle();
|
||||
expect(spellCheck).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("saves to the default dictionary the settings chose", async () => {
|
||||
vi.useFakeTimers();
|
||||
// A custom dictionary as the default: the row writes the file, not settings.
|
||||
useProject.setState({
|
||||
project: { root: "/p" } as never,
|
||||
settings: {
|
||||
spelling: { customDictionaries: ["docs/local.txt"], defaultDictionary: "docs/local.txt" },
|
||||
} as never,
|
||||
});
|
||||
const view = editor("// mesage");
|
||||
await settle();
|
||||
view.dispatch({ selection: { anchor: 5 } });
|
||||
await spellingIntentions(view)[1]!.run();
|
||||
expect(spellSaveToDictionary).toHaveBeenCalledWith("docs/local.txt", "mesage");
|
||||
expect(settingsSet).not.toHaveBeenCalled();
|
||||
|
||||
// The application dictionary as the default, and a removed custom
|
||||
// dictionary falls back to the project one.
|
||||
const application = { spelling: { customDictionaries: ["docs/local.txt"], defaultDictionary: "application" } };
|
||||
useProject.setState({ settings: application as never });
|
||||
await spellingIntentions(view)[1]!.run();
|
||||
expect(settingsSet).toHaveBeenCalledWith("global", "spelling.ideWords", JSON.stringify(["mesage"]));
|
||||
vi.clearAllMocks();
|
||||
useProject.setState({
|
||||
settings: {
|
||||
spelling: { customDictionaries: ["docs/local.txt"], defaultDictionary: "~/words/gone.dic" },
|
||||
} as never,
|
||||
});
|
||||
await spellingIntentions(view)[1]!.run();
|
||||
expect(settingsSet).toHaveBeenCalledWith("project", "spelling.projectWords", JSON.stringify(["mesage"]));
|
||||
});
|
||||
|
||||
it("does not run while the inspection is disabled", async () => {
|
||||
vi.useFakeTimers();
|
||||
useProject.setState({
|
||||
|
||||
@@ -10,6 +10,7 @@ import { StateEffect, StateField, type EditorState, type Extension } from "@code
|
||||
import { EditorView, ViewPlugin } from "@codemirror/view";
|
||||
import { whenSynced } from "@/editor/api";
|
||||
import { call, commands, isCancelled, isTauri, nextRequestId, type RequestId } from "@/platform/ipc";
|
||||
import { executeAction } from "@/platform/actions";
|
||||
import { useProject } from "@/platform/project";
|
||||
import {
|
||||
inspectionOption,
|
||||
@@ -40,6 +41,9 @@ interface PlacedTypo {
|
||||
|
||||
const setTypos = StateEffect.define<readonly PlacedTypo[]>();
|
||||
|
||||
/** A custom dictionary file changed on disk; the file is checked again. */
|
||||
const dictionariesChanged = StateEffect.define<null>();
|
||||
|
||||
/** The last check's typos, mapped through later edits (for the quick fixes). */
|
||||
const typosField = StateField.define<readonly PlacedTypo[]>({
|
||||
create: () => [],
|
||||
@@ -83,6 +87,48 @@ export async function saveToDictionary(word: string, level: "project" | "ide") {
|
||||
useProject.setState({ settings });
|
||||
}
|
||||
|
||||
/** Configured custom dictionaries (`spelling.customDictionaries`), trimmed and in order. */
|
||||
function customDictionaries(): string[] {
|
||||
return (spellingOf(useProject.getState().settings)?.customDictionaries ?? []).map((d) => d.trim()).filter(Boolean);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the word to a custom dictionary file (the backend resolves `~/…`
|
||||
* against the home folder, relative paths against the project root, and
|
||||
* creates the file when missing). The dictionary change re-checks the file
|
||||
* like the other saves; an error is reported at the caret.
|
||||
*/
|
||||
async function saveToCustomDictionary(view: EditorView, dictionary: string, word: string) {
|
||||
try {
|
||||
await call(commands.spellSaveToDictionary(dictionary, word));
|
||||
view.dispatch({ effects: dictionariesChanged.of(null) });
|
||||
} catch (e) {
|
||||
if (!isCancelled(e)) showHint(view, e instanceof Error ? e.message : String(e), { tone: "error" });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The dictionary "Save '…' to dictionary" writes to: the Default dictionary
|
||||
* setting (`project`, `application`, or a custom dictionary's configured
|
||||
* path); without one, project when a project is open, application otherwise.
|
||||
* A default naming a removed dictionary, or a project's file with no project
|
||||
* open, falls back — the row always works.
|
||||
*/
|
||||
function defaultSaveTarget(view: EditorView, word: string): () => void {
|
||||
const hasProject = !!useProject.getState().project;
|
||||
const setting = spellingOf(useProject.getState().settings)?.defaultDictionary;
|
||||
if (
|
||||
hasProject &&
|
||||
setting &&
|
||||
setting !== "project" &&
|
||||
setting !== "application" &&
|
||||
customDictionaries().includes(setting)
|
||||
) {
|
||||
return () => saveToCustomDictionary(view, setting, word);
|
||||
}
|
||||
return () => saveToDictionary(word, hasProject && setting !== "application" ? "project" : "ide");
|
||||
}
|
||||
|
||||
function typoAt(state: EditorState, pos: number): PlacedTypo | undefined {
|
||||
return state
|
||||
.field(typosField, false)
|
||||
@@ -146,8 +192,8 @@ export function spellingIntentions(view: EditorView): ListPopupItem[] {
|
||||
const typo = typoAt(view.state, sel.head);
|
||||
if (!typo) return [];
|
||||
const hasProject = !!useProject.getState().project;
|
||||
// One row saves to the project dictionary (application-level without a project);
|
||||
// its submenu still offers each level, then the inspection controls.
|
||||
const dictionaries = hasProject ? customDictionaries() : [];
|
||||
// The submenu offers each available dictionary and the inspection controls.
|
||||
const saveSubmenu: MenuItem[] = [
|
||||
...(hasProject
|
||||
? [
|
||||
@@ -163,6 +209,20 @@ export function spellingIntentions(view: EditorView): ListPopupItem[] {
|
||||
label: "Save to application-level dictionary",
|
||||
run: () => void saveToDictionary(typo.word, "ide"),
|
||||
},
|
||||
...dictionaries.map((d) => ({
|
||||
id: `spelling.saveCustom.${d}`,
|
||||
label: tx`Save to '${d}'`,
|
||||
run: () => void saveToCustomDictionary(view, d, typo.word),
|
||||
})),
|
||||
...(dictionaries.length
|
||||
? []
|
||||
: [
|
||||
{
|
||||
id: "spelling.addCustom",
|
||||
label: "Add custom dictionary…",
|
||||
run: () => void executeAction("file.settings", "editor.naturalLanguages.spelling"),
|
||||
},
|
||||
]),
|
||||
{ kind: "separator" },
|
||||
...intentionControls({ id: SPELLING_INSPECTION, name: "Typo" }),
|
||||
];
|
||||
@@ -184,7 +244,7 @@ export function spellingIntentions(view: EditorView): ListPopupItem[] {
|
||||
id: "spelling.save",
|
||||
label: tx`Save '${typo.word}' to dictionary`,
|
||||
icon: "bulb-fix",
|
||||
run: () => saveToDictionary(typo.word, hasProject ? "project" : "ide"),
|
||||
run: defaultSaveTarget(view, typo.word),
|
||||
submenu: saveSubmenu,
|
||||
},
|
||||
];
|
||||
@@ -256,7 +316,7 @@ export function spellingExtension(): Extension {
|
||||
});
|
||||
return {
|
||||
update: (u) => {
|
||||
if (u.docChanged) schedule();
|
||||
if (u.docChanged || u.transactions.some((tr) => tr.effects.some((e) => e.is(dictionariesChanged)))) schedule();
|
||||
},
|
||||
destroy: () => {
|
||||
clearTimeout(timer);
|
||||
|
||||
Reference in new issue
Заблокувати користувача