Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
refactor(core): created macro for closed compounds
Elijah Potter7ab9fc11038f
diff --git a/harper-core/src/linting/closed_compounds.rs b/harper-core/src/linting/closed_compounds.rsnew file mode 100644index 000000000..08e5ffa6f--- /dev/null+++ b/harper-core/src/linting/closed_compounds.rs@@ -0,0 +1,69 @@+use crate::{+ patterns::{ExactPhrase, Pattern},+ Token, TokenStringExt,+};++use super::{Lint, LintKind, PatternLinter, Suggestion};++macro_rules! create_closed_compound_linter {+ ($name:ident, $phrase:literal, $correct:expr) => {+ pub struct $name {+ pattern: Box<dyn Pattern>,+ }++ impl Default for $name {+ fn default() -> Self {+ let pattern = ExactPhrase::from_phrase($phrase);++ Self {+ pattern: Box::new(pattern),+ }+ }+ }++ impl PatternLinter for $name {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {+ let span = matched_tokens.span().unwrap();+ let orig_chars = span.get_content(source);++ Lint {+ span,+ lint_kind: LintKind::WordChoice,+ suggestions: vec![Suggestion::replace_with_match_case(+ $correct.chars().collect(),+ orig_chars,+ )],+ message: format!("Did you mean the closed compound `{}`?", $correct),+ ..Default::default()+ }+ }++ fn description(&self) -> &'static str {+ concat!(+ "Looks for incorrect spacing inside the closed compound `",+ $correct,+ "`."+ )+ }+ }+ };+}++create_closed_compound_linter!(Itself, "it self", "itself");++#[cfg(test)]+mod tests {+ use super::Itself;+ use crate::linting::tests::assert_suggestion_result;++ #[test]+ fn it_self() {+ let test_sentence = "The project, it self, was quite challenging.";+ let expected = "The project, itself, was quite challenging.";+ assert_suggestion_result(test_sentence, Itself::default(), expected);+ }+}diff --git a/harper-core/src/linting/itself.rs b/harper-core/src/linting/itself.rsdeleted file mode 100644index dfe36c911..000000000--- a/harper-core/src/linting/itself.rs+++ /dev/null@@ -1,62 +0,0 @@-use crate::{- patterns::{Pattern, SequencePattern},- Token, TokenStringExt,-};--use super::{Lint, LintKind, PatternLinter, Suggestion};--pub struct Itself {- pattern: Box<dyn Pattern>,-}--impl Default for Itself {- fn default() -> Self {- let pattern = SequencePattern::default()- .t_aco("it")- .then_whitespace()- .t_aco("self");-- Self {- pattern: Box::new(pattern),- }- }-}--impl PatternLinter for Itself {- fn pattern(&self) -> &dyn Pattern {- self.pattern.as_ref()- }-- fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {- let span = matched_tokens.span().unwrap();- let orig_chars = span.get_content(source);-- Lint {- span,- lint_kind: LintKind::WordChoice,- suggestions: vec![Suggestion::replace_with_match_case(- vec!['i', 't', 's', 'e', 'l', 'f'],- orig_chars,- )],- message: "`Itself` is commonly mistaken for `it self`.".to_owned(),- ..Default::default()- }- }-- fn description(&self) -> &'static str {- "Lint for condensing `it self` to `itself`."- }-}--#[cfg(test)]-mod tests {- use super::Itself;- use crate::linting::tests::assert_suggestion_result;-- #[test]- fn it_self() {- let test_sentence = "The project, it self, was quite challenging.";- let expected = "The project, itself, was quite challenging.";- assert_suggestion_result(test_sentence, Itself::default(), expected);- }-}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 6a98b0c92..b0229154c 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -5,11 +5,11 @@ use super::an_a::AnA;use super::avoid_curses::AvoidCurses;use super::boring_words::BoringWords;use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;+use super::closed_compounds::Itself;use super::correct_number_suffix::CorrectNumberSuffix;use super::despite_of::DespiteOf;use super::dot_initialisms::DotInitialisms;use super::ellipsis_length::EllipsisLength;-use super::itself::Itself;use super::lets_confusion::LetsConfusion;use super::linking_verbs::LinkingVerbs;use super::long_sentences::LongSentences;diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 528bbdcd9..8d97673b1 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -6,13 +6,13 @@ mod an_a;mod avoid_curses;mod boring_words;mod capitalize_personal_pronouns;+mod closed_compounds;mod correct_number_suffix;mod currency_placement;mod dashes;mod despite_of;mod dot_initialisms;mod ellipsis_length;-mod itself;mod lets_confusion;mod linking_verbs;mod lint;@@ -53,7 +53,6 @@ pub use currency_placement::CurrencyPlacement;pub use despite_of::DespiteOf;pub use dot_initialisms::DotInitialisms;pub use ellipsis_length::EllipsisLength;-pub use itself::Itself;pub use lets_confusion::LetsConfusion;pub use linking_verbs::LinkingVerbs;pub use lint::Lint;diff --git a/harper-core/src/patterns/exact_phrase.rs b/harper-core/src/patterns/exact_phrase.rsnew file mode 100644index 000000000..ec7ab9b73--- /dev/null+++ b/harper-core/src/patterns/exact_phrase.rs@@ -0,0 +1,43 @@+use crate::{Document, Token, TokenKind};++use super::{AnyCapitalization, Pattern, SequencePattern};++pub struct ExactPhrase {+ inner: SequencePattern,+}++impl ExactPhrase {+ pub fn from_phrase(text: &str) -> Self {+ let document = Document::new_markdown_default_curated(text);+ Self::from_document(&document)+ }++ pub fn from_document(doc: &Document) -> Self {+ let mut phrase = SequencePattern::default();++ for token in doc.fat_tokens() {+ match token.kind {+ TokenKind::Word(_word_metadata) => {+ phrase = phrase.then(Box::new(AnyCapitalization::new(+ token.content.as_slice().into(),+ )));+ }+ TokenKind::Space(_) => {+ phrase = phrase.then_whitespace();+ }+ TokenKind::ParagraphBreak => {+ phrase = phrase.then_whitespace();+ }+ _ => panic!("Fell out of expected document formats."),+ }+ }++ Self { inner: phrase }+ }+}++impl Pattern for ExactPhrase {+ 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 49d7a84d8..2a0b867c5 100644--- a/harper-core/src/patterns/mod.rs+++ b/harper-core/src/patterns/mod.rs@@ -14,6 +14,7 @@ mod any_capitalization;mod any_pattern;mod consumes_remaining_pattern;mod either_pattern;+mod exact_phrase;mod invert;mod is_not_title_case;mod naive_pattern_group;@@ -33,6 +34,7 @@ pub use any_pattern::AnyPattern;use blanket::blanket;pub use consumes_remaining_pattern::ConsumesRemainingPattern;pub use either_pattern::EitherPattern;+pub use exact_phrase::ExactPhrase;pub use invert::Invert;pub use is_not_title_case::IsNotTitleCase;pub use naive_pattern_group::NaivePatternGroup;diff --git a/justfile b/justfileindex 08b64b4d7..757be190a 100644--- a/justfile+++ b/justfile@@ -296,7 +296,6 @@ registerlinter module name:D="{{justfile_directory()}}/harper-core/src/linting"- sed -i "/mod an_a;/a mod {{module}};" "$D/mod.rs"sed -i "/pub use an_a::AnA;/a pub use {{module}}::{{name}};" "$D/mod.rs"sed -i "/use super::an_a::AnA;/a use super::{{module}}::{{name}};" "$D/lint_group.rs"sed -i "/create_lint_group_config\!/a \ \ \ \ {{name}} => true," "$D/lint_group.rs"