Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): extend `MergeWords` to include contractions
Elijah Potter10f88306520f
diff --git a/harper-core/src/linting/matcher.rs b/harper-core/src/linting/matcher.rsindex aff346612..9aaea55b5 100644--- a/harper-core/src/linting/matcher.rs+++ b/harper-core/src/linting/matcher.rs@@ -168,10 +168,6 @@ impl Matcher {"simply","grammatical" => "simple grammatical","you","r" => "your","you","re" => "you're",- "that","s" => "that's",- "That","s" => "That's",- "that","s" => "that is",- "That","s" => "that is","ms" => "milliseconds","case", "sensitive" => "case-sensitive","Tree", "sitter" => "Tree-sitter",diff --git a/harper-core/src/linting/merge_words.rs b/harper-core/src/linting/merge_words.rsindex 8bd73fb58..f732e63eb 100644--- a/harper-core/src/linting/merge_words.rs+++ b/harper-core/src/linting/merge_words.rs@@ -2,7 +2,7 @@ use std::sync::Arc;use itertools::Itertools;-use crate::{CharString, CharStringExt, Dictionary, Document, FstDictionary, Span};+use crate::{CharString, Dictionary, Document, FstDictionary, Span};use super::{Lint, LintKind, Linter, Suggestion};@@ -44,8 +44,8 @@ impl Linter for MergeWords {}merged_word.clear();- merged_word.extend_from_slice(&a_chars.to_lower());- merged_word.extend_from_slice(&b_chars.to_lower());+ merged_word.extend_from_slice(a_chars);+ merged_word.extend_from_slice(b_chars);if self.dict.contains_word(&merged_word)&& (!self.dict.contains_word(a_chars) || !self.dict.contains_word(b_chars))@@ -58,6 +58,23 @@ impl Linter for MergeWords {priority: 63,});}++ merged_word.clear();+ merged_word.extend_from_slice(a_chars);+ merged_word.push('\'');+ merged_word.extend_from_slice(b_chars);++ if self.dict.contains_word(&merged_word)+ && (!self.dict.contains_word(a_chars) || !self.dict.contains_word(b_chars))+ {+ lints.push(Lint {+ span: Span::new(a.span.start, b.span.end),+ lint_kind: LintKind::Spelling,+ suggestions: vec![Suggestion::ReplaceWith(merged_word.to_vec())],+ message: "It seems you intended to make this a contraction.".to_owned(),+ priority: 63,+ });+ }}lints@@ -70,7 +87,7 @@ impl Linter for MergeWords {#[cfg(test)]mod tests {- use crate::linting::tests::assert_lint_count;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};use super::MergeWords;@@ -96,4 +113,9 @@ mod tests {fn therefore() {assert_lint_count("The refore", MergeWords::default(), 1);}++ #[test]+ fn that_is_contraction() {+ assert_suggestion_result("That s", MergeWords::default(), "That's");+ }}