Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: added lint priorities
Elijah Potter9b6f4d93ab3e
diff --git a/harper-core/src/lib.rs b/harper-core/src/lib.rsindex 7f42a8197..9c72889bf 100644--- a/harper-core/src/lib.rs+++ b/harper-core/src/lib.rs@@ -14,3 +14,37 @@ pub use linting::{Lint, LintKind, Linter, Suggestion};pub use span::Span;pub use spell::Dictionary;pub use token::{FatToken, Punctuation, Token, TokenKind, TokenStringExt};++/// A utility function that removes overlapping lints in a vector,+/// keeping the more important ones.+///+/// Note: this function will change the ordering of the lints.+pub fn remove_overlaps(lints: &mut Vec<Lint>) {+ if lints.len() < 2 {+ return;+ }++ lints.sort_by_key(|l| l.span.start);++ let mut remove_indices = Vec::new();++ for i in 0..lints.len() - 1 {+ let cur = &lints[i];+ let next = &lints[i + 1];++ if cur.span.overlaps_with(next.span) {+ // Remember, lower priority means higher importance.+ if next.priority < cur.priority {+ remove_indices.push(i);+ } else {+ remove_indices.push(i + 1);+ }+ }+ }++ let mut index = 0;+ lints.retain(|_| {+ index += 1;+ !remove_indices.contains(&(index - 1))+ })+}diff --git a/harper-core/src/linting/lint.rs b/harper-core/src/linting/lint.rsindex a220825c0..fc6399ae3 100644--- a/harper-core/src/linting/lint.rs+++ b/harper-core/src/linting/lint.rs@@ -5,12 +5,27 @@ use serde::{Deserialize, Serialize};use crate::span::Span;-#[derive(Debug, Clone, Serialize, Deserialize, Default)]+#[derive(Debug, Clone, Serialize, Deserialize)]pub struct Lint {pub span: Span,pub lint_kind: LintKind,pub suggestions: Vec<Suggestion>,pub message: String,+ /// A numerical value for the importance of a lint.+ /// Lower = more important.+ pub priority: u8,+}++impl Default for Lint {+ fn default() -> Self {+ Self {+ span: Default::default(),+ lint_kind: Default::default(),+ suggestions: Default::default(),+ message: Default::default(),+ priority: 127,+ }+ }}#[derive(Debug, Clone, Copy, Serialize, Deserialize, Is, Default)]diff --git a/harper-core/src/linting/lint_set.rs b/harper-core/src/linting/lint_set.rsindex a4e8f789d..f52637ae0 100644--- a/harper-core/src/linting/lint_set.rs+++ b/harper-core/src/linting/lint_set.rs@@ -39,8 +39,8 @@ impl LintSet {.add_long_sentences().add_unclosed_quotes().add_sentence_capitalization()- .add_spell_check(dictionary).add_matcher()+ .add_spell_check(dictionary).add_spaces();self}diff --git a/harper-core/src/linting/matcher.rs b/harper-core/src/linting/matcher.rsindex bf715d801..f9e46064c 100644--- a/harper-core/src/linting/matcher.rs+++ b/harper-core/src/linting/matcher.rs@@ -144,7 +144,7 @@ impl Matcher {"simply","grammatical" => "simple grammatical","you","r" => "your","that","s" => "that's",- "That","s" => "that's",+ "That","s" => "That's","that","s" => "that is","That","s" => "that is","ms" => "milliseconds",@@ -228,6 +228,7 @@ impl Linter for Matcher {"Did you mean “{}”?",trigger.replace_with.iter().collect::<String>()),+ priority: 15,})}}diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rsindex e81ae78e9..4079b9211 100644--- a/harper-core/src/linting/repeated_words.rs+++ b/harper-core/src/linting/repeated_words.rs@@ -87,6 +87,7 @@ impl Linter for RepeatedWords {lint_kind: LintKind::Repetition,suggestions: vec![Suggestion::ReplaceWith(Vec::new())],message: "Did you mean to repeat this word?".to_string(),+ ..Default::default()})}}diff --git a/harper-core/src/linting/sentence_capitalization.rs b/harper-core/src/linting/sentence_capitalization.rsindex b0c31cb8e..f08891ab4 100644--- a/harper-core/src/linting/sentence_capitalization.rs+++ b/harper-core/src/linting/sentence_capitalization.rs@@ -29,6 +29,7 @@ impl Linter for SentenceCapitalization {suggestions: vec![Suggestion::ReplaceWith(first_letter.to_uppercase().collect_vec(),)],+ priority: 31,message: "This sentence does not start with a capital letter".to_string(),})diff --git a/harper-core/src/linting/spaces.rs b/harper-core/src/linting/spaces.rsindex 2a3fda75e..aade81692 100644--- a/harper-core/src/linting/spaces.rs+++ b/harper-core/src/linting/spaces.rs@@ -23,6 +23,7 @@ impl Linter for Spaces {"There are {} spaces where there should be only one.",count),+ priority: 15,})}}diff --git a/harper-core/src/linting/spell_check.rs b/harper-core/src/linting/spell_check.rsindex a86882de0..1de4ccc19 100644--- a/harper-core/src/linting/spell_check.rs+++ b/harper-core/src/linting/spell_check.rs@@ -75,6 +75,7 @@ impl Linter for SpellCheck {"Did you mean to spell “{}” this way?",document.get_span_content_str(word.span)),+ priority: 63,})}diff --git a/harper-core/src/linting/unclosed_quotes.rs b/harper-core/src/linting/unclosed_quotes.rsindex f723d440c..1d0cdf738 100644--- a/harper-core/src/linting/unclosed_quotes.rs+++ b/harper-core/src/linting/unclosed_quotes.rs@@ -18,6 +18,7 @@ impl Linter for UnclosedQuotes {lint_kind: LintKind::Formatting,suggestions: vec![],message: "This quote has no termination.".to_string(),+ priority: 255,})}}diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rsindex 0f5499786..aa4841f3a 100644--- a/harper-wasm/src/lib.rs+++ b/harper-wasm/src/lib.rs@@ -1,6 +1,6 @@use std::sync::Mutex;-use harper_core::{Dictionary, Document, LintSet, Linter};+use harper_core::{remove_overlaps, Dictionary, Document, LintSet, Linter};use once_cell::sync::Lazy;use serde::Serialize;use wasm_bindgen::{prelude::wasm_bindgen, JsValue};@@ -27,7 +27,9 @@ pub fn setup() {pub fn lint(text: String) -> Vec<JsValue> {let document = Document::new_markdown(&text);- let lints = LINTER.lock().unwrap().lint(&document);+ let mut lints = LINTER.lock().unwrap().lint(&document);++ remove_overlaps(&mut lints);lints.into_iter()diff --git a/web/src/lib/analysis.ts b/web/src/lib/analysis.tsindex c1ab11dce..d1cf198d6 100644--- a/web/src/lib/analysis.ts+++ b/web/src/lib/analysis.ts@@ -104,8 +104,6 @@ export async function lintText(text: string, useWasm = defaultUseWasm): Promise<// We only want to show fixable errors.lints = lints.filter((lint) => lint.suggestions.length > 0);- // The `Underlines` component assumes the lints do not overlap.- lints = removeOverlaps(lints);console.timeEnd('lintText');@@ -136,28 +134,3 @@ export async function applySuggestion(return res.text;}}--/** Removes lints whose spans overlap.- * NOTE: __Will__ reorder the lints. */-function removeOverlaps(lints: Lint[]) {- const sorted = lints.sort((a, b) => a.span.start - b.span.start);-- let overlapsFound = false;-- const filtered = sorted.filter((value, idx, arr) => {- const next = arr[idx + 1];-- if (next != null && next.span.start < value.span.end) {- overlapsFound = true;- return false;- } else {- return true;- }- });-- if (overlapsFound) {- return removeOverlaps(filtered);- } else {- return filtered;- }-}