Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: can ignore lints interactively

Elijah Potterbb5319f9dc35
diff --git a/package.json b/package.json
index 9f3491c62..02040ea69 100644
--- a/package.json
+++ b/package.json
@@ -24,6 +24,7 @@
"@wordpress/data": "^10.15.1",
"@wordpress/edit-post": "^8.16.0",
"@wordpress/editor": "^14.16.0",
+ "@wordpress/preferences-persistence": "^2.17.0",
"harper.js": "^0.19.0",
"lodash-es": "^4.17.21",
"react": "^18.0.0",
diff --git a/src/harper/Box.ts b/src/harper/Box.ts
index a36175cb1..22d541299 100644
--- a/src/harper/Box.ts
+++ b/src/harper/Box.ts
@@ -16,6 +16,10 @@ export type LintBox = Box & {
applySuggestion: (sug: Suggestion) => void;
};
+export type IgnorableLintBox = LintBox & {
+ ignoreLint: () => Promise<void>;
+};
+
export function isPointInBox(point: [number, number], box: Box) {
const [x, y] = point;
diff --git a/src/harper/HarperContext.tsx b/src/harper/HarperContext.tsx
index c1a6af231..574fd49da 100644
--- a/src/harper/HarperContext.tsx
+++ b/src/harper/HarperContext.tsx
@@ -1,4 +1,4 @@
-import { LintConfig, Linter, WorkerLinter } from 'harper.js';
+import { Lint, LintConfig, Linter, WorkerLinter } from 'harper.js';
import React, {
createContext,
ReactNode,
@@ -7,15 +7,22 @@ import React, {
useState,
} from 'react';
+export type HarperConfig = {
+ lintConfig: LintConfig;
+ ignoredState?: string;
+};
+
type HarperContextData = {
linter: Linter;
- config: LintConfig;
+ config: HarperConfig;
};
function createDefaultContextData(): HarperContextData {
return {
linter: new WorkerLinter(),
- config: {},
+ config: {
+ lintConfig: {},
+ },
};
}
@@ -28,7 +35,7 @@ export function HarperProvider({
}: {
children: ReactNode | ReactNode[];
}) {
- const dataReadWrite = useState<HarperContextData>(
+ const dataReadWrite = useState<HarperContextData>(() =>
createDefaultContextData()
);
@@ -42,11 +49,21 @@ export function HarperProvider({
export function useLinter(): Linter {
const [data, setData] = useContext(harperContext);
const [config] = useLinterConfig();
+ const [ignoredLintState] = useIgnoredLintState();
useEffect(() => {
data.linter.setLintConfig(config);
}, [config, data, setData]);
+ useEffect(() => {
+ (async () => {
+ await data.linter.clearIgnoredLints();
+ if (ignoredLintState !== undefined) {
+ await data.linter.importIgnoredLints(ignoredLintState);
+ }
+ })();
+ }, [ignoredLintState, data, setData]);
+
return data.linter;
}
@@ -72,6 +89,15 @@ export function useDefaultLintConfig(): LintConfig {
return defaultConfig;
}
+function useConfigKey<T>(key: string): [T, (newV: T) => void] {
+ const [data, setData] = useContext(harperContext);
+
+ return [
+ data.config[key],
+ (v) => setData({ ...data, config: { ...data.config, [key]: v } }),
+ ];
+}
+
/**
* Get the global linter's configuration, in addition to a callback to modify it.
* This is the preferred method of modifying the lint configuration in this project, since this will trigger re-renders of
@@ -81,15 +107,34 @@ export function useLinterConfig(): [
LintConfig,
(newConfig: LintConfig) => void,
] {
- const [data, setData] = useContext(harperContext);
+ const [val, setVal] = useConfigKey('lintConfig');
+ const defaultConfig = useDefaultLintConfig();
useEffect(() => {
- (async () => {
- const config = await data.linter.getLintConfig();
- const newData = { linter: data.linter, config };
- setData(newData);
- })();
- }, [data.linter, setData]);
+ if (Object.entries(val).length === 0) {
+ setVal(defaultConfig);
+ }
+ }, [val, setVal, defaultConfig]);
+
+ return [val as LintConfig, setVal];
+}
+
+export function useIgnoredLintState(): [
+ string | undefined,
+ (newState: string) => void,
+] {
+ const [val, setVal] = useConfigKey('ignoredState');
+
+ return [val as string, setVal];
+}
- return [data.config, (config) => setData({ ...data, config })];
+/** Grab a callback that can be used to ignore a lint. */
+export function useIgnoreLint(): (lint: Lint) => Promise<void> {
+ const linter = useLinter();
+ const [_, setIgnoredLintState] = useIgnoredLintState();
+
+ return async (lint) => {
+ await linter.ignoreLint(lint);
+ setIgnoredLintState(await linter.exportIgnoredLints());
+ };
}
diff --git a/src/harper/SuggestionControl.tsx b/src/harper/SuggestionControl.tsx
index cedf0a2c9..bc174f8d7 100644
--- a/src/harper/SuggestionControl.tsx
+++ b/src/harper/SuggestionControl.tsx
@@ -1,5 +1,5 @@
import React, { useEffect, useMemo, useRef, useState } from 'react';
-import { isPointInBox, LintBox } from './Box';
+import { IgnorableLintBox, isPointInBox, LintBox } from './Box';
import { SuggestionKind } from 'harper.js';
import { Button, Popover } from '@wordpress/components';
import { suggestionText } from './lintUtils';
@@ -10,8 +10,12 @@ import { suggestionText } from './lintUtils';
* @param root0
* @param root0.lintBox
*/
-export default function SuggestionControl({ lintBox }: { lintBox: LintBox }) {
- const { x, y, width, height, lint, applySuggestion } = lintBox;
+export default function SuggestionControl({
+ lintBox,
+}: {
+ lintBox: IgnorableLintBox;
+}) {
+ const { x, y, width, height, lint, applySuggestion, ignoreLint } = lintBox;
const underlineRef = useRef<HTMLDivElement | null>(null);
const popoverRef = useRef<HTMLDivElement | null>(null);
@@ -92,6 +96,10 @@ export default function SuggestionControl({ lintBox }: { lintBox: LintBox }) {
)}
</Button>
))}
+
+ <Button variant="link" onClick={ignoreLint}>
+ Ignore
+ </Button>
</Popover>
) : (
<></>
diff --git a/src/harper/useLintBoxes.ts b/src/harper/useLintBoxes.ts
index 9b1dccdea..cc5d4588e 100644
--- a/src/harper/useLintBoxes.ts
+++ b/src/harper/useLintBoxes.ts
@@ -1,8 +1,13 @@
import { useCallback, useEffect, useState } from 'react';
-import { LintBox } from './Box';
+import { IgnorableLintBox, LintBox } from './Box';
import RichText from './RichText';
import { Lint } from 'harper.js';
-import { useLinter, useLinterConfig } from './HarperContext';
+import {
+ useIgnoredLintState,
+ useIgnoreLint,
+ useLinter,
+ useLinterConfig,
+} from './HarperContext';
/**
* Lint given elements and return the resulting error targets.
@@ -11,26 +16,33 @@ import { useLinter, useLinterConfig } from './HarperContext';
*/
export default function useLintBoxes(
richTexts: RichText[]
-): [LintBox[][], boolean] {
+): [IgnorableLintBox[][], boolean] {
const linter = useLinter();
const [config] = useLinterConfig();
+ const [ignoreState] = useIgnoredLintState();
+ const ignoreLint = useIgnoreLint();
- const [targetBoxes, setTargetBoxes] = useState<LintBox[][]>([]);
+ const [targetBoxes, setTargetBoxes] = useState<IgnorableLintBox[][]>([]);
const [lints, setLints] = useState<Lint[][]>([]);
const [loading, setLoading] = useState(true);
const updateLints = useCallback(async () => {
// We assume that a given index always refers to the same rich text field.
+ console.log('Start');
const newLints = await Promise.all(
- richTexts.map((richText) => {
+ richTexts.map(async (richText) => {
const contents = richText.getTextContent();
- return linter.lint(contents);
+ if (ignoreState) {
+ await linter.importIgnoredLints(ignoreState);
+ }
+ return await linter.lint(contents);
})
);
+ console.log('End', newLints.flat().length);
setLoading(false);
setLints(newLints);
- }, [richTexts, linter, config]);
+ }, [richTexts, linter, config, ignoreState]);
useEffect(() => {
updateLints();
@@ -48,7 +60,7 @@ export default function useLintBoxes(
return () => {
observers.forEach((observer) => observer.disconnect());
};
- }, [richTexts, updateLints]);
+ }, [richTexts, ignoreState, updateLints]);
// Update the lint boxes each frame.
// Probably overkill.
@@ -61,9 +73,14 @@ export default function useLintBoxes(
function onFrame() {
const lintBoxes = lints.map((lintForText, index) => {
const richText = richTexts[index];
- return lintForText.flatMap((lint) =>
- richText.computeLintBox(lint)
- );
+ return lintForText
+ .flatMap((lint) => richText.computeLintBox(lint))
+ .map((box) => {
+ return {
+ ...box,
+ ignoreLint: () => ignoreLint(box.lint),
+ };
+ });
});
setTargetBoxes(lintBoxes);
@@ -78,7 +95,7 @@ export default function useLintBoxes(
return () => {
running = false;
};
- });
+ }, [lints, richTexts, ignoreLint]);
return [targetBoxes, loading];
}
diff --git a/yarn.lock b/yarn.lock
index 82242eab9..192ef6eb0 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3098,6 +3098,15 @@
"@wordpress/i18n" "^5.16.0"
"@wordpress/url" "^4.16.0"
+"@wordpress/api-fetch@^7.17.0":
+ version "7.17.0"
+ resolved "https://registry.yarnpkg.com/@wordpress/api-fetch/-/api-fetch-7.17.0.tgz#74cf59a5f65bba0584a00b5c0c9df8a534b30c50"
+ integrity sha512-L3iT/K41R6KResTy/7EOsTD+KKO20U3B4lPz/jQMRNgFdq4MOxtalEMjrRoj1mG+qiYGYdvGmpSgOzSx9o3eRg==
+ dependencies:
+ "@babel/runtime" "7.25.7"
+ "@wordpress/i18n" "^5.17.0"
+ "@wordpress/url" "^4.17.0"
+
"@wordpress/autop@^4.16.0":
version "4.16.0"
resolved "https://registry.yarnpkg.com/@wordpress/autop/-/autop-4.16.0.tgz#254ade3236f5ffda9b14251a1644cda55dca32ad"
@@ -3770,6 +3779,13 @@
dependencies:
"@babel/runtime" "7.25.7"
+"@wordpress/hooks@^4.17.0":
+ version "4.17.0"
+ resolved "https://registry.yarnpkg.com/@wordpress/hooks/-/hooks-4.17.0.tgz#ef5a2d960c848515f206195a3ef4540849298b0e"
+ integrity sha512-LGOHGuwCXCevuzaFpM2sgyPZxf3H7tWaSKzlvDzx2kmwiWIrFug/yebywv4Cxsl82I5DfZkDpxXRpqTxXrC0Nw==
+ dependencies:
+ "@babel/runtime" "7.25.7"
+
"@wordpress/html-entities@^4.16.0":
version "4.16.0"
resolved "https://registry.yarnpkg.com/@wordpress/html-entities/-/html-entities-4.16.0.tgz#8c5399174f97520b1626f5eff44130c98eb8e5af"
@@ -3801,6 +3817,18 @@
sprintf-js "^1.1.1"
tannin "^1.2.0"
+"@wordpress/i18n@^5.17.0":
+ version "5.17.0"
+ resolved "https://registry.yarnpkg.com/@wordpress/i18n/-/i18n-5.17.0.tgz#083492600b3cd5b39cc5f4c8f3aabd6b7ea88d08"
+ integrity sha512-aAsYls8sTTSEimsvjxBl9mCYbZYD3BddHVpuHgbBxzC+2SZE+JYJ+IpcwEghC712qo0jEkG8Vdzhqae1PL6vCQ==
+ dependencies:
+ "@babel/runtime" "7.25.7"
+ "@wordpress/hooks" "^4.17.0"
+ gettext-parser "^1.3.1"
+ memize "^2.1.0"
+ sprintf-js "^1.1.1"
+ tannin "^1.2.0"
+
"@wordpress/icons@^10.16.0":
version "10.16.0"
resolved "https://registry.yarnpkg.com/@wordpress/icons/-/icons-10.16.0.tgz#5b85fa425272c6468899e2a262427dd0365c5eff"
@@ -3971,6 +3999,14 @@
"@wordpress/base-styles" "^5.16.0"
autoprefixer "^10.4.20"
+"@wordpress/preferences-persistence@^2.17.0":
+ version "2.17.0"
+ resolved "https://registry.yarnpkg.com/@wordpress/preferences-persistence/-/preferences-persistence-2.17.0.tgz#eb73a433015e65010cdb69f098f5aff4107a42bd"
+ integrity sha512-isCZymyq2FOFlR4flEFhiIXdQpJmjCbFFsxfShmTz6H7f4w5Aqrivbgi/2RbN9UDu0Oi41VZqcwX8QIcOqbIxw==
+ dependencies:
+ "@babel/runtime" "7.25.7"
+ "@wordpress/api-fetch" "^7.17.0"
+
"@wordpress/preferences@^4.16.0":
version "4.16.0"
resolved "https://registry.yarnpkg.com/@wordpress/preferences/-/preferences-4.16.0.tgz#0fa3c6b4ab58e04027857821946183066c79c869"
@@ -4271,6 +4307,14 @@
"@babel/runtime" "7.25.7"
remove-accents "^0.5.0"
+"@wordpress/url@^4.17.0":
+ version "4.17.0"
+ resolved "https://registry.yarnpkg.com/@wordpress/url/-/url-4.17.0.tgz#241691988de48cbe1355c0c21363ad1e3634c38a"
+ integrity sha512-aFU1w2Wcz2/YdapPYozeXbb7C7LzfYZmAg4Bu28zTSxxrpKYocr/oYH7D8V13uHzfBoqTzL8XYM7wj17Dlcdag==
+ dependencies:
+ "@babel/runtime" "7.25.7"
+ remove-accents "^0.5.0"
+
"@wordpress/viewport@^6.16.0":
version "6.16.0"
resolved "https://registry.yarnpkg.com/@wordpress/viewport/-/viewport-6.16.0.tgz#4ae4969a818b160fc166e5b59ffda4a2b771945c"