Skip to content

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

refactor(core): use `LintGroup` for `closed_compounds`

Elijah Potter7f7527c3ec76
diff --git a/harper-core/src/linting/closed_compounds.rs b/harper-core/src/linting/closed_compounds.rs
index 4ceaa393d..1b293f482 100644
--- a/harper-core/src/linting/closed_compounds.rs
+++ b/harper-core/src/linting/closed_compounds.rs
@@ -1,261 +1,227 @@
-use crate::{
- patterns::{ExactPhrase, Pattern},
- Token, TokenStringExt,
-};
+use crate::linting::LintGroup;
-use super::{Lint, LintKind, PatternLinter, Suggestion};
+use super::MapPhraseLinter;
-macro_rules! create_closed_compound_linter {
- ($name:ident, $phrase:literal, $correct:expr) => {
- pub struct $name {
- pattern: Box<dyn Pattern>,
- }
+pub fn lint_group() -> LintGroup {
+ let mut group = LintGroup::empty();
- 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,
- "`."
- )
- }
- }
+ macro_rules! add_compound_mappings {
+ ($group:expr, { $($name:expr => ($bad:expr, $good:expr)),+ $(,)? }) => {
+ $(
+ $group.add(
+ $name,
+ Box::new(MapPhraseLinter::new_closed_compound($bad, $good)),
+ );
+ )+
};
}
-create_closed_compound_linter!(Itself, "it self", "itself");
-create_closed_compound_linter!(Myself, "my self", "myself");
-create_closed_compound_linter!(Therefore, "there fore", "therefore");
-create_closed_compound_linter!(Misunderstand, "miss understand", "misunderstand");
-create_closed_compound_linter!(Misunderstood, "miss understood", "misunderstood");
-create_closed_compound_linter!(Misuse, "miss use", "misuse");
-create_closed_compound_linter!(Misused, "miss used", "misused");
-create_closed_compound_linter!(Postpone, "post pone", "postpone");
-create_closed_compound_linter!(Worldwide, "world wide", "worldwide");
-create_closed_compound_linter!(Overall, "over all", "overall");
-create_closed_compound_linter!(However, "how ever", "however");
-create_closed_compound_linter!(Upset, "up set", "upset");
-create_closed_compound_linter!(Intact, "in tact", "intact");
-create_closed_compound_linter!(Somehow, "some how", "somehow");
-create_closed_compound_linter!(Proofread, "proof read", "proofread");
-create_closed_compound_linter!(Somebody, "some body", "somebody");
-create_closed_compound_linter!(Anybody, "any body", "anybody");
-create_closed_compound_linter!(Nothing, "no thing", "nothing");
-create_closed_compound_linter!(Anywhere, "any where", "anywhere");
-create_closed_compound_linter!(Instead, "in stead", "instead");
-create_closed_compound_linter!(Somewhere, "some where", "somewhere");
-create_closed_compound_linter!(Middleware, "middle ware", "middleware");
-create_closed_compound_linter!(Into, "in to", "into");
-create_closed_compound_linter!(Overclocking, "over clocking", "overclocking");
-create_closed_compound_linter!(Backplane, "back plane", "backplane");
-create_closed_compound_linter!(Overload, "over load", "overload");
-create_closed_compound_linter!(Underclock, "under clock", "underclock");
-create_closed_compound_linter!(Devops, "dev ops", "devops");
-create_closed_compound_linter!(Multithreading, "multi threading", "multithreading");
-create_closed_compound_linter!(Everywhere, "every where", "everywhere");
-create_closed_compound_linter!(Multicore, "multi core", "multicore");
-create_closed_compound_linter!(Multimedia, "multi media", "multimedia");
-create_closed_compound_linter!(Widespread, "wide spread", "widespread");
-create_closed_compound_linter!(Notwithstanding, "not with standing", "notwithstanding");
-create_closed_compound_linter!(Anyhow, "any how", "anyhow");
-create_closed_compound_linter!(Nonetheless, "none the less", "nonetheless");
-create_closed_compound_linter!(Thereupon, "there upon", "thereupon");
-create_closed_compound_linter!(Forthwith, "forth with", "forthwith");
-create_closed_compound_linter!(Insofar, "in so far", "insofar");
-create_closed_compound_linter!(Whereupon, "where upon", "whereupon");
-create_closed_compound_linter!(Upward, "up ward", "upward");
-create_closed_compound_linter!(Henceforth, "hence forth", "henceforth");
-create_closed_compound_linter!(Regardless, "regard less", "regardless");
-create_closed_compound_linter!(Overnight, "over night", "overnight");
-create_closed_compound_linter!(Furthermore, "further more", "furthermore");
-create_closed_compound_linter!(Laptop, "lap top", "laptop");
-create_closed_compound_linter!(Desktop, "desk top", "desktop");
+ // Use the macro with a grid of mappings:
+ add_compound_mappings!(group, {
+ "Itself" => ("it self", "itself"),
+ "Myself" => ("my self", "myself"),
+ "Therefore" => ("there fore", "therefore"),
+ "Misunderstand" => ("miss understand", "misunderstand"),
+ "Misunderstood" => ("miss understood", "misunderstood"),
+ "Misuse" => ("miss use", "misuse"),
+ "Misused" => ("miss used", "misused"),
+ "Postpone" => ("post pone", "postpone"),
+ "Worldwide" => ("world wide", "worldwide"),
+ "Overall" => ("over all", "overall"),
+ "However" => ("how ever", "however"),
+ "Upset" => ("up set", "upset"),
+ "Intact" => ("in tact", "intact"),
+ "Somehow" => ("some how", "somehow"),
+ "Proofread" => ("proof read", "proofread"),
+ "Somebody" => ("some body", "somebody"),
+ "Anybody" => ("any body", "anybody"),
+ "Nothing" => ("no thing", "nothing"),
+ "Anywhere" => ("any where", "anywhere"),
+ "Instead" => ("in stead", "instead"),
+ "Somewhere" => ("some where", "somewhere"),
+ "Middleware" => ("middle ware", "middleware"),
+ "Into" => ("in to", "into"),
+ "Overclocking" => ("over clocking", "overclocking"),
+ "Backplane" => ("back plane", "backplane"),
+ "Overload" => ("over load", "overload"),
+ "Underclock" => ("under clock", "underclock"),
+ "Devops" => ("dev ops", "devops"),
+ "Multithreading" => ("multi threading", "multithreading"),
+ "Everywhere" => ("every where", "everywhere"),
+ "Multicore" => ("multi core", "multicore"),
+ "Multimedia" => ("multi media", "multimedia"),
+ "Widespread" => ("wide spread", "widespread"),
+ "Notwithstanding" => ("not with standing", "notwithstanding"),
+ "Anyhow" => ("any how", "anyhow"),
+ "Nonetheless" => ("none the less", "nonetheless"),
+ "Thereupon" => ("there upon", "thereupon"),
+ "Forthwith" => ("forth with", "forthwith"),
+ "Insofar" => ("in so far", "insofar"),
+ "Whereupon" => ("where upon", "whereupon"),
+ "Upward" => ("up ward", "upward"),
+ "Henceforth" => ("hence forth", "henceforth"),
+ "Regardless" => ("regard less", "regardless"),
+ "Overnight" => ("over night", "overnight"),
+ "Furthermore" => ("further more", "furthermore"),
+ "Laptop" => ("lap top", "laptop"),
+ "Desktop" => ("desk top", "desktop"),
+ });
+
+ group.set_all_rules_to(Some(true));
+
+ group
+}
#[cfg(test)]
mod tests {
- use super::{
- Anyhow, Forthwith, Henceforth, Insofar, Nonetheless, Notwithstanding, Overnight,
- Regardless, Thereupon, Upward, Whereupon, Widespread,
- };
- use super::{
- However, Itself, Misunderstood, Misuse, Misused, Myself, Overall, Therefore, Worldwide,
- };
use crate::linting::tests::assert_suggestion_result;
+ use super::lint_group;
+
#[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);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn my_self() {
let test_sentence = "He treated my self with respect.";
let expected = "He treated myself with respect.";
- assert_suggestion_result(test_sentence, Myself::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn there_fore() {
let test_sentence = "This is the reason; there fore, this is true.";
let expected = "This is the reason; therefore, this is true.";
- assert_suggestion_result(test_sentence, Therefore::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn mis_understood() {
let test_sentence = "She miss understood the instructions.";
let expected = "She misunderstood the instructions.";
- assert_suggestion_result(test_sentence, Misunderstood::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn mis_use() {
let test_sentence = "He tends to miss use the tool.";
let expected = "He tends to misuse the tool.";
- assert_suggestion_result(test_sentence, Misuse::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn mis_used() {
let test_sentence = "The software was miss used.";
let expected = "The software was misused.";
- assert_suggestion_result(test_sentence, Misused::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn world_wide() {
let test_sentence = "The world wide impact was significant.";
let expected = "The worldwide impact was significant.";
- assert_suggestion_result(test_sentence, Worldwide::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn over_all() {
let test_sentence = "The over all performance was good.";
let expected = "The overall performance was good.";
- assert_suggestion_result(test_sentence, Overall::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn how_ever() {
let test_sentence = "This is true, how ever, details matter.";
let expected = "This is true, however, details matter.";
- assert_suggestion_result(test_sentence, However::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn wide_spread() {
let test_sentence = "The news was wide spread throughout the region.";
let expected = "The news was widespread throughout the region.";
- assert_suggestion_result(test_sentence, Widespread::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn not_with_standing() {
let test_sentence = "They decided to proceed not with standing any further delay.";
let expected = "They decided to proceed notwithstanding any further delay.";
- assert_suggestion_result(test_sentence, Notwithstanding::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn any_how() {
let test_sentence = "She solved the problem any how, even under pressure.";
let expected = "She solved the problem anyhow, even under pressure.";
- assert_suggestion_result(test_sentence, Anyhow::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn none_the_less() {
let test_sentence = "The results were disappointing, none the less, they continued.";
let expected = "The results were disappointing, nonetheless, they continued.";
- assert_suggestion_result(test_sentence, Nonetheless::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn there_upon() {
let test_sentence = "A decision was made there upon reviewing the data.";
let expected = "A decision was made thereupon reviewing the data.";
- assert_suggestion_result(test_sentence, Thereupon::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn forth_with() {
let test_sentence = "Please reply forth with to our previous inquiry.";
let expected = "Please reply forthwith to our previous inquiry.";
- assert_suggestion_result(test_sentence, Forthwith::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn in_so_far() {
let test_sentence = "This rule applies in so far as it covers all cases.";
let expected = "This rule applies insofar as it covers all cases.";
- assert_suggestion_result(test_sentence, Insofar::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn where_upon() {
let test_sentence = "They acted where upon the circumstances allowed.";
let expected = "They acted whereupon the circumstances allowed.";
- assert_suggestion_result(test_sentence, Whereupon::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn up_ward() {
let test_sentence = "The temperature moved up ward during the afternoon.";
let expected = "The temperature moved upward during the afternoon.";
- assert_suggestion_result(test_sentence, Upward::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn hence_forth() {
let test_sentence = "All new policies apply hence forth immediately.";
let expected = "All new policies apply henceforth immediately.";
- assert_suggestion_result(test_sentence, Henceforth::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn regard_less() {
let test_sentence = "The decision was made, regard less of the opposition.";
let expected = "The decision was made, regardless of the opposition.";
- assert_suggestion_result(test_sentence, Regardless::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
#[test]
fn over_night() {
let test_sentence = "They set off on their journey over night.";
let expected = "They set off on their journey overnight.";
- assert_suggestion_result(test_sentence, Overnight::default(), expected);
+ assert_suggestion_result(test_sentence, lint_group(), expected);
}
}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index bb817fa09..367b5e7f6 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -8,17 +8,6 @@ use super::avoid_curses::AvoidCurses;
use super::boring_words::BoringWords;
use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;
use super::chock_full::ChockFull;
-use super::closed_compounds::Desktop;
-use super::closed_compounds::Furthermore;
-use super::closed_compounds::Laptop;
-use super::closed_compounds::Overnight;
-use super::closed_compounds::{
- Anybody, Anyhow, Anywhere, Backplane, Devops, Everywhere, Henceforth, However, Insofar,
- Instead, Intact, Into, Itself, Middleware, Misunderstand, Misunderstood, Misuse, Misused,
- Multicore, Multimedia, Multithreading, Myself, Nonetheless, Nothing, Notwithstanding, Overall,
- Overclocking, Overload, Postpone, Proofread, Regardless, Somebody, Somehow, Somewhere,
- Therefore, Thereupon, Underclock, Upset, Upward, Whereupon, Widespread, Worldwide,
-};
use super::compound_nouns::CompoundNouns;
use super::correct_number_suffix::CorrectNumberSuffix;
use super::despite_of::DespiteOf;
@@ -60,7 +49,7 @@ use super::wordpress_dotcom::WordPressDotcom;
use super::wrong_quotes::WrongQuotes;
use super::Lint;
use super::{CurrencyPlacement, Linter, NoOxfordComma, OxfordComma};
-use crate::linting::phrase_corrections;
+use crate::linting::{closed_compounds, phrase_corrections};
use crate::Dictionary;
use crate::Document;
@@ -175,64 +164,20 @@ impl LintGroup {
out.merge_from(&mut proper_noun_capitalization_linters::lint_group(
dictionary.clone(),
));
+ out.merge_from(&mut closed_compounds::lint_group());
insert_struct_rule!(WordPressDotcom, true);
insert_struct_rule!(OutOfDate, true);
- insert_struct_rule!(Desktop, true);
- insert_struct_rule!(Laptop, true);
insert_struct_rule!(ThenThan, true);
insert_struct_rule!(PiqueInterest, true);
insert_struct_rule!(WasAloud, true);
insert_struct_rule!(HyphenateNumberDay, true);
insert_struct_rule!(LeftRightHand, true);
insert_struct_rule!(HopHope, true);
- insert_struct_rule!(Furthermore, true);
- insert_struct_rule!(Overnight, true);
insert_struct_rule!(Hereby, true);
insert_struct_rule!(Likewise, true);
insert_struct_rule!(CompoundNouns, true);
- insert_struct_rule!(Regardless, true);
- insert_struct_rule!(Henceforth, true);
- insert_struct_rule!(Upward, true);
- insert_struct_rule!(Whereupon, true);
- insert_struct_rule!(Insofar, true);
- insert_struct_rule!(Thereupon, true);
- insert_struct_rule!(Nonetheless, true);
- insert_struct_rule!(Anyhow, true);
- insert_struct_rule!(Notwithstanding, true);
- insert_struct_rule!(Widespread, true);
- insert_struct_rule!(Multimedia, true);
- insert_struct_rule!(Multicore, true);
- insert_struct_rule!(Multithreading, true);
- insert_struct_rule!(Devops, true);
- insert_struct_rule!(Underclock, true);
- insert_struct_rule!(Overload, true);
- insert_struct_rule!(Backplane, true);
- insert_struct_rule!(Overclocking, true);
- insert_struct_rule!(Middleware, true);
- insert_struct_rule!(Somewhere, true);
- insert_struct_rule!(Instead, true);
- insert_struct_rule!(Anywhere, true);
- insert_struct_rule!(Nothing, true);
- insert_struct_rule!(Anybody, true);
- insert_struct_rule!(Somebody, true);
insert_struct_rule!(Nobody, true);
- insert_struct_rule!(Into, true);
- insert_struct_rule!(Proofread, true);
- insert_struct_rule!(Somehow, true);
- insert_struct_rule!(Intact, true);
- insert_struct_rule!(Upset, true);
- insert_struct_rule!(Misunderstood, true);
- insert_struct_rule!(However, true);
- insert_struct_rule!(Overall, true);
- insert_struct_rule!(Worldwide, true);
- insert_struct_rule!(Postpone, true);
- insert_struct_rule!(Misused, true);
- insert_struct_rule!(Misuse, true);
- insert_struct_rule!(Misunderstand, true);
- insert_struct_rule!(Therefore, true);
- insert_struct_rule!(Myself, true);
- insert_struct_rule!(Itself, true);
insert_struct_rule!(Whereas, true);
insert_struct_rule!(PossessiveYour, true);
insert_struct_rule!(SpelledNumbers, false);
@@ -266,7 +211,6 @@ impl LintGroup {
insert_struct_rule!(LetsConfusion, true);
insert_struct_rule!(DespiteOf, true);
insert_struct_rule!(ChockFull, true);
- insert_struct_rule!(Everywhere, true);
out.add("SpellCheck", Box::new(SpellCheck::new(dictionary)));
out.config.set_rule_enabled("SpellCheck", true);
@@ -297,7 +241,7 @@ impl Linter for LintGroup {
mod tests {
use crate::{linting::Linter, Document, FstDictionary, Lrc, MutableDictionary};
- use super::{LintGroup, LintGroupConfig};
+ use super::LintGroup;
#[test]
fn can_get_all_descriptions() {
diff --git a/harper-core/src/linting/map_phrase_linter.rs b/harper-core/src/linting/map_phrase_linter.rs
index d4cbeccfd..c696c9640 100644
--- a/harper-core/src/linting/map_phrase_linter.rs
+++ b/harper-core/src/linting/map_phrase_linter.rs
@@ -47,6 +47,20 @@ impl MapPhraseLinter {
description,
)
}
+
+ pub fn new_closed_compound(phrase: impl AsRef<str>, correct_form: impl ToString) -> Self {
+ let message = format!(
+ "Did you mean the closed compound `{}`?",
+ correct_form.to_string()
+ );
+
+ let description = format!(
+ "Looks for incorrect spacing inside the closed compound `{}`.",
+ correct_form.to_string()
+ );
+
+ Self::new_exact_phrase(phrase, [correct_form], message, description)
+ }
}
impl PatternLinter for MapPhraseLinter {
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 0243503a9..3860a9406 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -65,17 +65,6 @@ pub use avoid_curses::AvoidCurses;
pub use boring_words::BoringWords;
pub use capitalize_personal_pronouns::CapitalizePersonalPronouns;
pub use chock_full::ChockFull;
-pub use closed_compounds::Desktop;
-pub use closed_compounds::Furthermore;
-pub use closed_compounds::Laptop;
-pub use closed_compounds::Overnight;
-pub use closed_compounds::{
- Anybody, Anyhow, Anywhere, Backplane, Devops, Everywhere, Henceforth, However, Insofar,
- Instead, Intact, Into, Itself, Middleware, Misunderstand, Misunderstood, Misuse, Misused,
- Multicore, Multimedia, Multithreading, Myself, Nonetheless, Nothing, Notwithstanding, Overall,
- Overclocking, Overload, Postpone, Proofread, Regardless, Somebody, Somehow, Somewhere,
- Therefore, Thereupon, Underclock, Upset, Upward, Whereupon, Widespread, Worldwide,
-};
pub use compound_nouns::CompoundNouns;
pub use correct_number_suffix::CorrectNumberSuffix;
pub use currency_placement::CurrencyPlacement;