Skip to content

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

refactor(core): use macros to make `phrase_corrections` easier to grok

Elijah Potter613b85ce5836
diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rs
index 2f968eadb..5693bcf72 100644
--- a/harper-core/src/linting/phrase_corrections.rs
+++ b/harper-core/src/linting/phrase_corrections.rs
@@ -2,170 +2,125 @@ use crate::patterns::{ExactPhrase, OwnedPatternExt};
use super::{LintGroup, MapPhraseLinter};
-/// Produce a [`LintGroup`] that represents all the linters in this module.
+/// Produce a [`LintGroup`] that looks for errors in common phrases.
/// Comes pre-configured with the recommended default settings.
pub fn lint_group() -> LintGroup {
let mut group = LintGroup::default();
- group.add(
- "TurnItOff",
- Box::new(MapPhraseLinter::new_similar_to_phrase("turn it off", 1)),
- );
- group.add(
- "HumanLife",
- Box::new(MapPhraseLinter::new_similar_to_phrase("human life", 1)),
- );
- group.add(
- "ThatChallenged",
- Box::new(MapPhraseLinter::new_similar_to_phrase("that challenged", 2)),
- );
- group.add(
- "NoLonger",
- Box::new(MapPhraseLinter::new_similar_to_phrase("no longer", 1)),
- );
- group.add(
- "NeedHelp",
- Box::new(MapPhraseLinter::new_similar_to_phrase("need help", 1)),
- );
- group.add(
- "OfCourse",
- Box::new(MapPhraseLinter::new_similar_to_phrase("of course", 1)),
- );
- group.add(
- "AndTheLike",
- Box::new(MapPhraseLinter::new_similar_to_phrase("and the like", 1)),
- );
- group.add(
- "BadRap",
- Box::new(MapPhraseLinter::new_similar_to_phrase("bad rap", 1)),
- );
- group.add(
- "BatedBreath",
- Box::new(MapPhraseLinter::new_similar_to_phrase("bated breath", 1)),
- );
- group.add(
- "BeckAndCall",
- Box::new(MapPhraseLinter::new_similar_to_phrase("beck and call", 1)),
- );
- group.add(
- "HungerPang",
- Box::new(MapPhraseLinter::new_similar_to_phrase("hunger pang", 2)),
- );
- group.add(
- "EnMasse",
- Box::new(MapPhraseLinter::new_similar_to_phrase("en masse", 1)),
- );
- group.add(
- "LetAlone",
- Box::new(MapPhraseLinter::new_similar_to_phrase("let alone", 1)),
- );
- group.add(
- "SneakingSuspicion",
- Box::new(MapPhraseLinter::new_similar_to_phrase(
- "sneaking suspicion",
- 3,
- )),
- );
- group.add(
- "SpecialAttention",
- Box::new(MapPhraseLinter::new_similar_to_phrase(
- "special attention",
- 1,
- )),
- );
- group.add(
- "SupposedTo",
- Box::new(MapPhraseLinter::new_similar_to_phrase("supposed to", 1)),
- );
-
- group.add(
- "ChangeTack",
- Box::new(MapPhraseLinter::new_exact_phrase(
+ macro_rules! add_similar_mappings {
+ ($group:expr, { $($name:expr => ($phrase:expr, $threshold:expr)),+ $(,)? }) => {
+ $(
+ $group.add(
+ $name,
+ Box::new(MapPhraseLinter::new_similar_to_phrase($phrase, $threshold)),
+ );
+ )+
+ };
+ }
+
+ // These are rules that look for phrases that are _almost_ but not exactly
+ // equal to the correct form (shown on the right).
+ //
+ // The number is the allowed edit distance per word to search for.
+ add_similar_mappings!(group, {
+ "TurnItOff" => ("turn it off", 1),
+ "HumanLife" => ("human life", 1),
+ "ThatChallenged" => ("that challenged", 2),
+ "NoLonger" => ("no longer", 1),
+ "NeedHelp" => ("need help", 1),
+ "OfCourse" => ("of course", 1),
+ "AndTheLike" => ("and the like", 1),
+ "BadRap" => ("bad rap", 1),
+ "BatedBreath" => ("bated breath", 1),
+ "BeckAndCall" => ("beck and call", 1),
+ "HungerPang" => ("hunger pang", 2),
+ "EnMasse" => ("en masse", 1),
+ "LetAlone" => ("let alone", 1),
+ "SneakingSuspicion" => ("sneaking suspicion", 3),
+ "SpecialAttention" => ("special attention", 1),
+ "SupposedTo" => ("supposed to", 1),
+ });
+
+ macro_rules! add_exact_mappings {
+ ($group:expr, {
+ $($name:expr => ($input:expr, $corrections:expr, $hint:expr, $description:expr)),+ $(,)?
+ }) => {
+ $(
+ $group.add(
+ $name,
+ Box::new(MapPhraseLinter::new_exact_phrase(
+ $input,
+ $corrections,
+ $hint,
+ $description,
+ )),
+ );
+ )+
+ };
+ }
+
+ add_exact_mappings!(group, {
+ // The name of the rule
+ "ChangeTack" => (
+ // The exact phrase to look for.
"change tact",
+ // The corrections to provide.
["change tack"],
+ // The message to be shown with the error.
"Did you mean the sailing idiom?",
- "Locates minor errors in the sailing idiom `change tack`.",
- )),
- );
- group.add(
- "WantBe",
- Box::new(MapPhraseLinter::new_exact_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."
- ))
-);
-
- group.add(
- "StateOfTheArt",
- Box::new(MapPhraseLinter::new_exact_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."
- ))
-);
-
- group.add(
- "FastPaste",
- Box::new(MapPhraseLinter::new(
- Box::new(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."
- ))
-);
-
- group.add(
- "FaceFirst",
- Box::new(MapPhraseLinter::new_exact_phrase(
+ // A description of the rule.
+ "Locates minor errors in the sailing idiom `change tack`."
+ ),
+ "WantBe" => (
+ "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."
+ ),
+ "StateOfTheArt" => (
+ "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."
+ ),
+ "FaceFirst" => (
"face first into",
["face-first into"],
"Should this be `face-first`?",
- "Ensures `face first` is correctly hyphenated as `face-first` when used before `into`.",
- )),
- );
-
- group.add(
- "EludedTo",
- Box::new(MapPhraseLinter::new_exact_phrase(
+ "Ensures `face first` is correctly hyphenated as `face-first` when used before `into`."
+ ),
+ "EludedTo" => (
"eluded to",
["alluded to"],
"Did you mean `alluded to`?",
- "Corrects `eluded to` to `alluded to` in contexts referring to indirect references.",
- )),
- );
-
- group.add(
- "BaitedBreath",
- Box::new(MapPhraseLinter::new_exact_phrase(
+ "Corrects `eluded to` to `alluded to` in contexts referring to indirect references."
+ ),
+ "BaitedBreath" => (
"baited breath",
["bated breath"],
"Did you mean `bated breath`?",
- "Ensures `bated breath` is written correctly, as `baited breath` is incorrect.",
- )),
- );
-
- group.add(
- "BareInMind",
- Box::new(MapPhraseLinter::new_exact_phrase(
+ "Ensures `bated breath` is written correctly, as `baited breath` is incorrect."
+ ),
+ "BareInMind" => (
"bare in mind",
["bear in mind"],
"Did you mean `bear in mind`?",
- "Ensures the phrase `bear in mind` is used correctly instead of `bare in mind`.",
- )),
- );
-
- group.add(
- "MutePoint",
- Box::new(MapPhraseLinter::new_exact_phrase(
- "mute point",
- ["moot point"],
- "Did you mean `moot point`?",
- "Ensures `moot point` is used instead of `mute point`, as `moot` means debatable or irrelevant."
- ))
- );
+ "Ensures the phrase `bear in mind` is used correctly instead of `bare in mind`."
+ ),
+ "MutePoint" => (
+ "mute point",
+ ["moot point"],
+ "Did you mean `moot point`?",
+ "Ensures `moot point` is used instead of `mute point`, as `moot` means debatable or irrelevant."
+ )
+ });
+
+ group.add("FastPaste", Box::new(MapPhraseLinter::new(
+ Box::new(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."
+ )));
group.set_all_rules_to(Some(true));