Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): add several linters for common phrase errors
Elijah Potter0790de5f4b21
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 3ce7cbba9..e373f5464 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -19390,7 +19390,7 @@ davit/1MSdawdle/41ZGDRSdawdler/1Mdawn/41SGMD-day/14SM~+day/1SM~daybed/1MSdaybreak/1Mdaycare/1M@@ -29337,7 +29337,8 @@ julep/1SMjulienne/14jumble/41MGDSjumbo/51SM-jump/415MDRSZG+jump/415MDRZG+jumps/4jumper/14Mjumpily/jumpiness/1M@@ -40222,7 +40223,8 @@ rumormonger/14SMrump/14MYSrumple/41DSMGrumpus/1MS-run/415ASM+run/415AM+runs/4runabout/1MSrunaround/1SMrunaway/15MSdiff --git a/harper-core/src/char_string.rs b/harper-core/src/char_string.rsindex fa151adc0..94f620caa 100644--- a/harper-core/src/char_string.rs+++ b/harper-core/src/char_string.rs@@ -22,3 +22,11 @@ impl CharStringExt for [char] {self.iter().collect()}}++macro_rules! char_string {+ ($string:literal) => {+ $string.chars().collect::<CharString>()+ };+}++pub(crate) use char_string;diff --git a/harper-core/src/linting/hop_hope/mod.rs b/harper-core/src/linting/hop_hope/mod.rsnew file mode 100644index 000000000..8eedc7d1d--- /dev/null+++ b/harper-core/src/linting/hop_hope/mod.rs@@ -0,0 +1,86 @@+use super::merge_linters::merge_linters;++mod to_hop;+mod to_hope;+use to_hop::ToHop;+use to_hope::ToHope;++merge_linters!(HopHope => ToHop, ToHope => "" );++#[cfg(test)]+mod tests {+ use super::HopHope;+ use crate::linting::tests::assert_suggestion_result;++ #[test]+ fn corrects_hop_to_hope() {+ assert_suggestion_result(+ "I hop we can clarify this soon.",+ HopHope::default(),+ "I hope we can clarify this soon.",+ );+ }++ #[test]+ fn does_not_correct_unrelated_use() {+ assert_suggestion_result(+ "I hop on one foot for fun.",+ HopHope::default(),+ "I hop on one foot for fun.",+ );+ }++ #[test]+ fn corrects_mixed_case_hop() {+ assert_suggestion_result(+ "I HoP we can find a solution.",+ HopHope::default(),+ "I HoPe we can find a solution.",+ );+ }++ #[test]+ fn corrects_hoping_on_call() {+ assert_suggestion_result(+ "I was hoping on a call to discuss this.",+ HopHope::default(),+ "I was hopping on a call to discuss this.",+ );+ }++ #[test]+ fn corrects_hoped_on_plane() {+ assert_suggestion_result(+ "She hoped on an airplane to visit family.",+ HopHope::default(),+ "She hopped on an airplane to visit family.",+ );+ }++ #[test]+ fn corrects_hope_on_bus() {+ assert_suggestion_result(+ "They hope on a bus every morning.",+ HopHope::default(),+ "They hop on a bus every morning.",+ );+ }++ #[test]+ fn does_not_correct_unrelated_context() {+ assert_suggestion_result(+ "I hope everything goes well with your project.",+ HopHope::default(),+ "I hope everything goes well with your project.",+ );+ }++ #[test]+ fn corrects_mixed_case() {+ assert_suggestion_result(+ "She HoPeD on a train to get home.",+ HopHope::default(),+ "She HoPpEd on a train to get home.",+ );+ }+}diff --git a/harper-core/src/linting/hop_hope/to_hop.rs b/harper-core/src/linting/hop_hope/to_hop.rsnew file mode 100644index 000000000..86fd42b6f--- /dev/null+++ b/harper-core/src/linting/hop_hope/to_hop.rs@@ -0,0 +1,68 @@+use super::super::{Lint, LintKind, PatternLinter};+use crate::linting::Suggestion;+use crate::patterns::{Pattern, PatternExt, SequencePattern, SimilarToPhrase, WordSet};+use crate::CharStringExt;+use crate::{char_string::char_string, CharString, Token, TokenStringExt};++pub struct ToHop {+ pattern: Box<dyn Pattern>,+}++impl Default for ToHop {+ fn default() -> Self {+ let pattern = SequencePattern::default()+ .then_word_set(WordSet::all(&["hoping", "hoped", "hope"]))+ .then_whitespace()+ .t_aco("on")+ .then_whitespace()+ .then_article()+ .then_whitespace()+ .then_word_set(WordSet::all(&["airplane", "plane", "bus", "call", "train"]));++ Self {+ pattern: Box::new(pattern),+ }+ }+}++impl ToHop {+ fn to_correct(word: &str) -> Option<CharString> {+ Some(match word.to_lowercase().as_str() {+ "hoping" => char_string!("hopping"),+ "hoped" => char_string!("hopped"),+ "hope" => char_string!("hop"),+ _ => return None,+ })+ }+}++impl PatternLinter for ToHop {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {+ let offending_word = matched_tokens[0];+ let word_chars = offending_word.span.get_content(source);+ let word = word_chars.to_string();+ let correct = Self::to_correct(&word).unwrap();++ Lint {+ span: offending_word.span,+ lint_kind: LintKind::WordChoice,+ suggestions: vec![Suggestion::replace_with_match_case(+ correct.to_vec(),+ word_chars,+ )],+ message: format!(+ "Did you mean to use {word} instead of {} in this context?",+ correct.to_string()+ ),+ ..Default::default()+ }+ }++ fn description(&self) -> &'static str {+ "Detects incorrect usage of the words 'hoping,' 'hoped,' or 'hope' when referring to boarding or entering a mode of transportation. Suggests replacing them with the correct verb form such as 'hopping,' 'hopped,' or 'hop.'"+ }+}diff --git a/harper-core/src/linting/hop_hope/to_hope.rs b/harper-core/src/linting/hop_hope/to_hope.rsnew file mode 100644index 000000000..93290306a--- /dev/null+++ b/harper-core/src/linting/hop_hope/to_hope.rs@@ -0,0 +1,50 @@+use super::super::{Lint, LintKind, PatternLinter};+use crate::linting::Suggestion;+use crate::patterns::{OwnedPatternExt, Pattern, PatternExt, SequencePattern, WordSet};+use crate::CharStringExt;+use crate::{char_string::char_string, CharString, Token, TokenStringExt};++pub struct ToHope {+ pattern: Box<dyn Pattern>,+}++impl Default for ToHope {+ fn default() -> Self {+ let pattern = SequencePattern::default()+ .then_singular_subject()+ .then_whitespace()+ .then_word_set(WordSet::all(&["hop", "hopped"]))+ .then_whitespace()+ .then_singular_subject();++ Self {+ pattern: Box::new(pattern),+ }+ }+}++impl PatternLinter for ToHope {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {+ let offending_word = matched_tokens[2];+ let word_chars = offending_word.span.get_content(source);++ Lint {+ span: offending_word.span,+ lint_kind: LintKind::WordChoice,+ suggestions: vec![Suggestion::replace_with_match_case(+ char_string!("hope").to_vec(),+ word_chars,+ )],+ message: "Did you mean to use 'hope' instead of 'hop' in this context?".to_string(),+ ..Default::default()+ }+ }++ fn description(&self) -> &'static str {+ "Detects incorrect use of 'hop' when the correct verb 'hope' should be used in a sentence."+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex fc901c3fe..ae33ca708 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -20,6 +20,7 @@ use super::despite_of::DespiteOf;use super::dot_initialisms::DotInitialisms;use super::ellipsis_length::EllipsisLength;use super::hereby::Hereby;+use super::hop_hope::HopHope;use super::lets_confusion::LetsConfusion;use super::likewise::Likewise;use super::linking_verbs::LinkingVerbs;@@ -29,6 +30,9 @@ use super::merge_words::MergeWords;use super::multiple_sequential_pronouns::MultipleSequentialPronouns;use super::nobody::Nobody;use super::number_suffix_capitalization::NumberSuffixCapitalization;+use super::phrase_corrections::FastPaste;+use super::phrase_corrections::StateOfTheArt;+use super::phrase_corrections::WantBe;use super::phrase_corrections::{AndAlike, BadRap, BatedBreath, BeckAndCall, ChangeTack, EnMasse, HumanLife, HungerPang,LetAlone, LoAndBehold, NeedHelp, NoLonger, OfCourse, SneakingSuspicion, SpecialAttention,@@ -175,6 +179,10 @@ macro_rules! create_lint_group_config {}create_lint_group_config!(+ FastPaste => true,+ StateOfTheArt => true,+ WantBe => true,+ HopHope => true,Furthermore => true,Overnight => true,Hereby => true,diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex ce8bdc037..8ece4f1e0 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -15,6 +15,7 @@ mod despite_of;mod dot_initialisms;mod ellipsis_length;mod hereby;+mod hop_hope;mod lets_confusion;mod likewise;mod linking_verbs;@@ -70,6 +71,7 @@ pub use despite_of::DespiteOf;pub use dot_initialisms::DotInitialisms;pub use ellipsis_length::EllipsisLength;pub use hereby::Hereby;+pub use hop_hope::HopHope;pub use lets_confusion::LetsConfusion;pub use likewise::Likewise;pub use linking_verbs::LinkingVerbs;@@ -85,6 +87,9 @@ pub use nobody::Nobody;pub use number_suffix_capitalization::NumberSuffixCapitalization;pub use oxford_comma::OxfordComma;pub use pattern_linter::PatternLinter;+pub use phrase_corrections::FastPaste;+pub use phrase_corrections::StateOfTheArt;+pub use phrase_corrections::WantBe;pub use phrase_corrections::{AndAlike, BadRap, BatedBreath, BeckAndCall, ChangeTack, EnMasse, HumanLife, HungerPang,LetAlone, LoAndBehold, NeedHelp, NoLonger, OfCourse, SneakingSuspicion, SpecialAttention,diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rsindex b7c8828f3..0351cb043 100644--- a/harper-core/src/linting/phrase_corrections.rs+++ b/harper-core/src/linting/phrase_corrections.rs@@ -1,10 +1,15 @@+use serde::de::SeqAccess;+use super::{Lint, LintKind, PatternLinter};use crate::linting::Suggestion;-use crate::patterns::{ExactPhrase, Pattern, SimilarToPhrase};+use crate::patterns::{+ EitherPattern, ExactPhrase, OwnedPatternExt, Pattern, PatternExt, SequencePattern,+ SimilarToPhrase, WordSet,+};use crate::{Token, TokenStringExt};macro_rules! create_linter_map_phrase {- ($name:ident, $pattern:expr, $correct_form:expr, $message:expr, $description:expr) => {+ ($name:ident, $pattern:expr, $($correct_form:literal).*, $message:expr, $description:expr) => {#[doc = $description]pub struct $name {pattern: Box<dyn Pattern>,@@ -36,10 +41,12 @@ macro_rules! create_linter_map_phrase {Lint {span,lint_kind: LintKind::Miscellaneous,- suggestions: vec![Suggestion::replace_with_match_case(- $correct_form.chars().collect(),- matched_text,- )],+ suggestions: vec![$(+ Suggestion::replace_with_match_case(+ $correct_form.chars().collect(),+ matched_text,+ ),+ )*],message: $message.to_string(),priority: 31,}@@ -91,6 +98,10 @@ create_linter_for_phrase!(SupposedTo, "supposed to", 1);create_linter_map_phrase!(LoAndBehold, ExactPhrase::from_phrase("long and behold"), "lo and behold", "Did you mean `lo and behold`?", "Detects the exact phrase `long and behold` and suggests replacing it with the idiomatically correct `lo and behold`");+create_linter_map_phrase!(WantBe, ExactPhrase::from_phrase("want be"),"won't be"."want to be","Did you mean `won't be` or `want to be`?", "Detects incorrect usage of `want be` and suggests `won't be` or `want to be` based on context.");+create_linter_map_phrase!(StateOfTheArt, ExactPhrase::from_phrase("state of art"), "state of the art", "Did you mean `state of the art`?", "Detects incorrect usage of `state of art` and suggests `state of the art` as the correct phrase.");+create_linter_map_phrase!(FastPaste, ExactPhrase::from_phrase("fast paste").or(Box::new(ExactPhrase::from_phrase("fast-paste"))), "fast-paced", "Did you mean `fast-paced`?", "Detects incorrect usage of `fast paste` or `fast-paste` and suggests `fast-paced` as the correct phrase.");+#[cfg(test)]mod tests {use crate::linting::tests::{assert_lint_count, assert_suggestion_result};diff --git a/harper-core/src/patterns/exact_phrase.rs b/harper-core/src/patterns/exact_phrase.rsindex ec7ab9b73..4cece0cad 100644--- a/harper-core/src/patterns/exact_phrase.rs+++ b/harper-core/src/patterns/exact_phrase.rs@@ -25,6 +25,11 @@ impl ExactPhrase {TokenKind::Space(_) => {phrase = phrase.then_whitespace();}+ TokenKind::Punctuation(p) => {+ phrase = phrase.then(Box::new(move |t: &Token, _source: &[char]| {+ t.kind.as_punctuation().cloned() == Some(p)+ }))+ }TokenKind::ParagraphBreak => {phrase = phrase.then_whitespace();}diff --git a/harper-core/src/patterns/indefinite_article.rs b/harper-core/src/patterns/indefinite_article.rsnew file mode 100644index 000000000..c1908e317--- /dev/null+++ b/harper-core/src/patterns/indefinite_article.rs@@ -0,0 +1,21 @@+use crate::Token;++use super::{Pattern, SequencePattern, WordSet};++pub struct IndefiniteArticle {+ inner: SequencePattern,+}++impl Default for IndefiniteArticle {+ fn default() -> Self {+ Self {+ inner: SequencePattern::default().then_word_set(WordSet::all(&["a", "an"])),+ }+ }+}++impl Pattern for IndefiniteArticle {+ fn matches(&self, tokens: &[Token], source: &[char]) -> usize {+ self.inner.matches(tokens, source)+ }+}diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rsindex 870d0e88d..ba5ba36e4 100644--- a/harper-core/src/patterns/mod.rs+++ b/harper-core/src/patterns/mod.rs@@ -15,6 +15,7 @@ mod any_pattern;mod consumes_remaining_pattern;mod either_pattern;mod exact_phrase;+mod indefinite_article;mod invert;mod is_not_title_case;mod naive_pattern_group;@@ -22,6 +23,7 @@ mod noun_phrase;mod repeating_pattern;mod sequence_pattern;mod similar_to_phrase;+mod singular_subject;mod split_compound_word;mod token_kind_pattern_group;mod whitespace_pattern;@@ -36,6 +38,7 @@ use blanket::blanket;pub use consumes_remaining_pattern::ConsumesRemainingPattern;pub use either_pattern::EitherPattern;pub use exact_phrase::ExactPhrase;+pub use indefinite_article::IndefiniteArticle;pub use invert::Invert;pub use is_not_title_case::IsNotTitleCase;pub use naive_pattern_group::NaivePatternGroup;@@ -43,6 +46,7 @@ pub use noun_phrase::NounPhrase;pub use repeating_pattern::RepeatingPattern;pub use sequence_pattern::SequencePattern;pub use similar_to_phrase::SimilarToPhrase;+pub use singular_subject::SingularSubject;pub use split_compound_word::SplitCompoundWord;pub use token_kind_pattern_group::TokenKindPatternGroup;pub use whitespace_pattern::WhitespacePattern;@@ -102,6 +106,19 @@ where}}+pub trait OwnedPatternExt {+ fn or(self, other: Box<dyn Pattern>) -> EitherPattern;+}++impl<P> OwnedPatternExt for P+where+ P: Pattern + 'static,+{+ fn or(self, other: Box<dyn Pattern>) -> EitherPattern {+ EitherPattern::new(vec![Box::new(self), other])+ }+}+#[cfg(feature = "concurrent")]impl<F> Pattern for Fwherediff --git a/harper-core/src/patterns/noun_phrase.rs b/harper-core/src/patterns/noun_phrase.rsindex dedf54303..cc0becce5 100644--- a/harper-core/src/patterns/noun_phrase.rs+++ b/harper-core/src/patterns/noun_phrase.rs@@ -2,7 +2,6 @@ use crate::Token;use super::Pattern;-/// A pattern that returns the value of the first non-zero match in a list.#[derive(Default)]pub struct NounPhrase;diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rsindex e8843651e..3c51b474e 100644--- a/harper-core/src/patterns/sequence_pattern.rs+++ b/harper-core/src/patterns/sequence_pattern.rs@@ -2,7 +2,10 @@ use hashbrown::HashSet;use paste::paste;use super::whitespace_pattern::WhitespacePattern;-use super::{AnyCapitalization, AnyPattern, NounPhrase, Pattern, RepeatingPattern, WordSet};+use super::{+ AnyCapitalization, AnyPattern, IndefiniteArticle, NounPhrase, Pattern, RepeatingPattern,+ SingularSubject, WordSet,+};use crate::Lrc;use crate::{CharStringExt, Token, TokenKind};@@ -83,11 +86,16 @@ impl SequencePattern {gen_then_from_is!(apostrophe);gen_then_from_is!(hyphen);gen_then_from_is!(article);+ gen_then_from_is!(proper_noun);pub fn then_word_set(self, set: WordSet) -> Self {self.then(Box::new(set))}+ pub fn then_indefinite_article(self) -> Self {+ self.then(Box::new(IndefiniteArticle::default()))+ }+/// Add a pattern that looks for more complex ideas, like nouns with adjectives attached.pub fn then_noun_phrase(self) -> Self {self.then(Box::new(NounPhrase))@@ -116,6 +124,10 @@ impl SequencePattern {self}+ pub fn then_singular_subject(self) -> Self {+ self.then(Box::new(SingularSubject::default()))+ }+/// Shorthand for [`Self::any_capitalization_of`].pub fn aco(word: &'static str) -> Self {Self::any_capitalization_of(word)diff --git a/harper-core/src/patterns/singular_subject.rs b/harper-core/src/patterns/singular_subject.rsnew file mode 100644index 000000000..4adfe1faa--- /dev/null+++ b/harper-core/src/patterns/singular_subject.rs@@ -0,0 +1,23 @@+use crate::Token;++use super::{EitherPattern, OwnedPatternExt, Pattern, SequencePattern};++pub struct SingularSubject {+ inner: EitherPattern,+}++impl Default for SingularSubject {+ fn default() -> Self {+ Self {+ inner: SequencePattern::default()+ .then_pronoun()+ .or(Box::new(SequencePattern::default().then_proper_noun())),+ }+ }+}++impl Pattern for SingularSubject {+ fn matches(&self, tokens: &[Token], source: &[char]) -> usize {+ self.inner.matches(tokens, source)+ }+}diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rsindex 0e1acd857..707bd7301 100644--- a/harper-core/src/token_kind.rs+++ b/harper-core/src/token_kind.rs@@ -76,6 +76,19 @@ impl TokenKind {)}+ pub fn is_proper_noun(&self) -> bool {+ matches!(+ self,+ TokenKind::Word(WordMetadata {+ noun: Some(NounData {+ is_proper: Some(true),+ ..+ }),+ ..+ })+ )+ }+pub fn is_conjunction(&self) -> bool {matches!(self,