Skip to content

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

feat(harper.js): significantly improve worker performance

Elijah Potter506cf4462413
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 6fdc80b77..3ec0704eb 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -208,9 +208,11 @@ impl Linter {
Ok(())
}
- pub fn ignore_lint(&mut self, lint: Lint) {
+ pub fn ignore_lint(&mut self, source_text: String, lint: Lint) {
+ let source: Vec<_> = source_text.chars().collect();
+
let document = Document::new_from_vec(
- lint.source.into(),
+ source.into(),
&lint.language.create_parser(),
&self.dictionary,
);
@@ -240,7 +242,10 @@ impl Linter {
lints
.into_iter()
- .map(|l| Lint::new(l, source.to_vec(), language))
+ .map(|l| {
+ let problem_text = l.span.get_content_string(&source);
+ Lint::new(l, problem_text, language)
+ })
.collect()
}
@@ -298,10 +303,11 @@ impl Linter {
/// This action will be logged to the Linter's statistics.
pub fn apply_suggestion(
&mut self,
+ source_text: String,
lint: &Lint,
suggestion: &Suggestion,
) -> Result<String, String> {
- let mut source = lint.source.clone();
+ let mut source: Vec<_> = source_text.chars().collect();
let doc = Document::new_from_vec(
source.clone().into(),
@@ -393,7 +399,8 @@ impl Suggestion {
#[wasm_bindgen]
pub struct Lint {
inner: harper_core::linting::Lint,
- source: Vec<char>,
+ /// The problematic text that produced this lint.
+ problem_text: String,
language: Language,
}
@@ -401,19 +408,19 @@ pub struct Lint {
impl Lint {
pub(crate) fn new(
inner: harper_core::linting::Lint,
- source: Vec<char>,
+ problem_text: String,
language: Language,
) -> Self {
Self {
inner,
- source,
+ problem_text,
language,
}
}
/// Get the content of the source material pointed to by [`Self::span`]
pub fn get_problem_text(&self) -> String {
- self.inner.span.get_content_string(&self.source)
+ self.problem_text.clone()
}
/// Get a string representing the general category of the lint.
diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.ts
index 1b6e09924..84d2633f0 100644
--- a/packages/harper.js/src/Linter.test.ts
+++ b/packages/harper.js/src/Linter.test.ts
@@ -137,7 +137,7 @@ for (const [linterName, Linter] of Object.entries(linters)) {
expect(firstRound.length).toBeGreaterThanOrEqual(1);
- await linter.ignoreLint(firstRound[0]);
+ await linter.ignoreLint(source, firstRound[0]);
const secondRound = await linter.lint(source);
@@ -152,7 +152,7 @@ for (const [linterName, Linter] of Object.entries(linters)) {
const firstLints = await firstLinter.lint(source);
for (const lint of firstLints) {
- await firstLinter.ignoreLint(lint);
+ await firstLinter.ignoreLint(source, lint);
}
const exported = await firstLinter.exportIgnoredLints();
@@ -204,7 +204,7 @@ for (const [linterName, Linter] of Object.entries(linters)) {
expect(sug).not.toBeNull();
- const applied = await linter.applySuggestion(lint, sug);
+ const applied = await linter.applySuggestion(source, lint, sug);
expect(applied).toBe('This is a test.');
@@ -228,7 +228,7 @@ for (const [linterName, Linter] of Object.entries(linters)) {
expect(sug).not.toBeNull();
- const applied = await linter.applySuggestion(lint, sug);
+ const applied = await linter.applySuggestion(source, lint, sug);
expect(applied).toBe('This is a test.');
diff --git a/packages/harper.js/src/Linter.ts b/packages/harper.js/src/Linter.ts
index 5727a3b0b..657b146df 100644
--- a/packages/harper.js/src/Linter.ts
+++ b/packages/harper.js/src/Linter.ts
@@ -13,8 +13,8 @@ export default interface Linter {
/** Lint the provided text. */
lint(text: string, options?: LintOptions): Promise<Lint[]>;
- /** Apply a suggestion from a lint, returning the changed text. */
- applySuggestion(lint: Lint, suggestion: Suggestion): Promise<string>;
+ /** Apply a suggestion from a lint to text, returning the changed text. */
+ applySuggestion(text: string, lint: Lint, suggestion: Suggestion): Promise<string>;
/** Determine if the provided text is likely to be intended to be English.
* The algorithm can be described as "proof of concept" and as such does not work terribly well.*/
@@ -54,7 +54,7 @@ export default interface Linter {
toTitleCase(text: string): Promise<string>;
/** Ignore future instances of a lint from a previous linting run in future invocations. */
- ignoreLint(lint: Lint): Promise<void>;
+ ignoreLint(source: string, lint: Lint): Promise<void>;
/** Export the ignored lints to a JSON list of privacy-respecting hashes. */
exportIgnoredLints(): Promise<string>;
diff --git a/packages/harper.js/src/LocalLinter.ts b/packages/harper.js/src/LocalLinter.ts
index e52eeaab2..d8d0e4a3a 100644
--- a/packages/harper.js/src/LocalLinter.ts
+++ b/packages/harper.js/src/LocalLinter.ts
@@ -38,9 +38,9 @@ export default class LocalLinter implements Linter {
return lints;
}
- async applySuggestion(lint: Lint, suggestion: Suggestion): Promise<string> {
+ async applySuggestion(text: string, lint: Lint, suggestion: Suggestion): Promise<string> {
const inner = await this.inner;
- return inner.apply_suggestion(lint, suggestion);
+ return inner.apply_suggestion(text, lint, suggestion);
}
async isLikelyEnglish(text: string): Promise<boolean> {
@@ -95,9 +95,9 @@ export default class LocalLinter implements Linter {
return inner.get_lint_descriptions_as_json();
}
- async ignoreLint(lint: Lint): Promise<void> {
+ async ignoreLint(source: string, lint: Lint): Promise<void> {
const inner = await this.inner;
- inner.ignore_lint(lint);
+ inner.ignore_lint(source, lint);
}
async exportIgnoredLints(): Promise<string> {
diff --git a/packages/harper.js/src/WorkerLinter/index.ts b/packages/harper.js/src/WorkerLinter/index.ts
index e7afe49d9..d4b42c4ec 100644
--- a/packages/harper.js/src/WorkerLinter/index.ts
+++ b/packages/harper.js/src/WorkerLinter/index.ts
@@ -69,8 +69,8 @@ export default class WorkerLinter implements Linter {
return this.rpc('lint', [text, options]);
}
- applySuggestion(lint: Lint, suggestion: Suggestion): Promise<string> {
- return this.rpc('applySuggestion', [lint, suggestion]);
+ applySuggestion(text: string, lint: Lint, suggestion: Suggestion): Promise<string> {
+ return this.rpc('applySuggestion', [text, lint, suggestion]);
}
isLikelyEnglish(text: string): Promise<boolean> {
@@ -117,8 +117,8 @@ export default class WorkerLinter implements Linter {
return JSON.parse(await this.getDefaultLintConfigAsJSON()) as LintConfig;
}
- ignoreLint(lint: Lint): Promise<void> {
- return this.rpc('ignoreLint', [lint]);
+ ignoreLint(source: string, lint: Lint): Promise<void> {
+ return this.rpc('ignoreLint', [source, lint]);
}
exportIgnoredLints(): Promise<string> {
diff --git a/packages/obsidian-plugin/src/index.ts b/packages/obsidian-plugin/src/index.ts
index 243ed2d38..d19bbe5ff 100644
--- a/packages/obsidian-plugin/src/index.ts
+++ b/packages/obsidian-plugin/src/index.ts
@@ -191,8 +191,6 @@ export default class HarperPlugin extends Plugin {
const lints = await this.harper.lint(text);
- console.log(lints);
-
return lints.map((lint) => {
const span = lint.span();
diff --git a/packages/web/src/lib/Editor.svelte b/packages/web/src/lib/Editor.svelte
index 0433bea67..260c9e2e2 100644
--- a/packages/web/src/lib/Editor.svelte
+++ b/packages/web/src/lib/Editor.svelte
@@ -106,7 +106,7 @@ $: superSmall = (w ?? 1024) < 550;
style="height: 40px; margin: 5px 0px;"
on:click={() =>
linter
- .applySuggestion(lint, suggestion)
+ .applySuggestion(content, lint, suggestion)
.then((edited) => (content = edited))}
>
{#if suggestion.kind() == SuggestionKind.Remove}
@@ -142,7 +142,7 @@ $: superSmall = (w ?? 1024) < 550;
on:click={() =>
focused != null &&
linter
- .applySuggestion(lints[focused], suggestion)
+ .applySuggestion(content, lints[focused], suggestion)
.then((edited) => (content = edited))}
>
{#if suggestion.kind() == SuggestionKind.Remove}
diff --git a/packages/wordpress-plugin/src/harper/RichText.ts b/packages/wordpress-plugin/src/harper/RichText.ts
index 64d9a7b59..6e8a75003 100644
--- a/packages/wordpress-plugin/src/harper/RichText.ts
+++ b/packages/wordpress-plugin/src/harper/RichText.ts
@@ -54,7 +54,7 @@ export default class RichText {
height: targetRect.height,
lint,
applySuggestion: async (sug: Suggestion) => {
- const fixed = await linter.applySuggestion(lint, sug);
+ const fixed = await linter.applySuggestion(text, lint, sug);
this.editContent(fixed);
},