Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
Merge branch 'master' into rid-off-phrase-corrections
Elijah Potter96f3ac012356
diff --cc harper-core/src/linting/phrase_corrections.rsindex 3430cd66c,124cd021c..73e0dbf87--- a/harper-core/src/linting/phrase_corrections.rs+++ b/harper-core/src/linting/phrase_corrections.rs@@@ -1,213 -1,356 +1,411 @@@- use super::{Lint, LintKind, PatternLinter};- use crate::linting::Suggestion;- use crate::patterns::{ExactPhrase, OwnedPatternExt, Pattern, SimilarToPhrase};- use crate::{Token, TokenStringExt};-- macro_rules! create_linter_map_phrase {- ($name:ident, $pattern:expr, $($correct_form:literal).*, $message:expr, $description:expr) => {- #[doc = $description]- pub struct $name {- pattern: Box<dyn Pattern>,- }-- impl $name {- pub fn new() -> Self {- Self {- pattern: Box::new($pattern),- }- }- }-- impl Default for $name {- fn default() -> Self {- Self::new()- }- }-- impl PatternLinter for $name {- fn pattern(&self) -> &dyn Pattern {- self.pattern.as_ref()- }-- fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {- let span = matched_tokens.span()?;- let matched_text = span.get_content(source);-- Some(Lint {- span,- lint_kind: LintKind::Miscellaneous,- suggestions: vec![$(- Suggestion::replace_with_match_case(- $correct_form.chars().collect(),- matched_text,- ),- )*],- message: $message.to_string(),- priority: 31,- })- }-- fn description(&self) -> &'static str {- $description- }- }- };- }+ use super::{LintGroup, MapPhraseLinter};++ /// 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();++ 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_phrases(+ $input,+ $corrections,+ $hint,+ $description,+ )),+ );+ )++ };+ }- /// Generate a linter that will look for a common phrase and correct mild errors that- /// are still composed of real words.- macro_rules! create_linter_for_phrase {- ($name:ident, $correct_form:literal, $dist:literal) => {- create_linter_map_phrase!(- $name,- SimilarToPhrase::from_phrase($correct_form, $dist),- $correct_form,- concat!("Did you mean the phrase `", $correct_form, "`?"),- concat!(- "Looks for slight improper modifications to the phrase `",- $correct_form,- "`."- )- );- };+ 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?",+ // 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`."+ ),+ "EludedTo" => (+ ["eluded to"],+ ["alluded to"],+ "Did you mean `alluded to`?",+ "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."+ ),+ "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`."+ ),+ "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."+ ),+ "RoadMap" => (+ ["roadmap"],+ ["road map"],+ "Did you mean `road map`?",+ "Detects when `roadmap` is used instead of `road map`, prompting the correct spacing."+ ),+ "SameAs" => (+ ["same then"],+ ["same as"],+ "Did you mean `same as`?",+ "Corrects the incorrect phrase `same then` to the standard `same as`."+ ),+ "SoonerOrLater" => (+ ["sooner than later"],+ ["sooner rather than later", "sooner or later"],+ "Did you mean `sooner rather than later` or `sooner or later`?",+ "Fixes the improper phrase `sooner than later` by suggesting standard alternatives."+ ),+ "HadOf" => (+ ["had of"],+ ["had have", "had've"],+ "Did you mean `had have` or `had've`?",+ "Flags the unnecessary use of `of` after `had` and suggests the correct forms."+ ),+ "FatalOutcome" => (+ ["fatal outcome"],+ ["death"],+ "Consider using `death` for clarity.",+ "Replaces `fatal outcome` with the more direct term `death` for conciseness."+ ),+ "NotTo" => (+ ["no to"],+ ["not to"],+ "Did you mean `not to`?",+ "Corrects `no to` to `not to`, ensuring proper negation."+ ),+ "ThatThis" => (+ ["the this"],+ ["that this"],+ "Did you mean `that this`?",+ "Fixes `the this` to the correct phrase `that this`."+ ),+ "CondenseAllThe" => (+ ["all of the"],+ ["all the"],+ "Consider simplifying to `all the`.",+ "Suggests removing `of` in `all of the` for a more concise phrase."+ ),+ "AvoidAndAlso" => (+ ["and also"],+ ["and"],+ "Consider using just `and`.",+ "Reduces redundancy by replacing `and also` with `and`."+ ),+ "AndIn" => (+ ["an in"],+ ["and in"],+ "Did you mean `and in`?",+ "Fixes the incorrect phrase `an in` to `and in` for proper conjunction usage."+ ),+ "BeenThere" => (+ ["bee there"],+ ["been there"],+ "Did you mean `been there`?",+ "Corrects the misspelling `bee there` to the proper phrase `been there`."+ ),+ "CanBeSeen" => (+ ["can be seem"],+ ["can be seen"],+ "Did you mean `can be seen`?",+ "Corrects `can be seem` to the proper phrase `can be seen`."+ ),+ "GoingTo" => (+ ["gong to"],+ ["going to"],+ "Did you mean `going to`?",+ "Corrects `gong to` to the intended phrase `going to`."+ ),+ "IAm" => (+ ["I a m"],+ ["I am"],+ "Did you mean `I am`?",+ "Fixes the incorrect spacing in `I a m` to properly form `I am`."+ ),+ "ItCan" => (+ ["It cam"],+ ["It can"],+ "Did you mean `It can`?",+ "Corrects the misspelling `It cam` to the proper phrase `It can`."+ ),+ "MyHouse" => (+ ["mu house"],+ ["my house"],+ "Did you mean `my house`?",+ "Fixes the typo `mu house` to `my house`."+ ),+ "OperativeSystem" => (+ ["operative system"],+ ["operating system"],+ "Did you mean `operating system`?",+ "Ensures `operating system` is used correctly instead of `operative system`."+ ),+ "OperativeSystems" => (+ ["operative systems"],+ ["operating systems"],+ "Did you mean `operating systems`?",+ "Ensures `operating systems` is used correctly instead of `operative systems`."+ ),+ "BanTogether" => (+ ["ban together"],+ ["band together"],+ "Did you mean `band together`?",+ "Detects and corrects the common error of using `ban together` instead of the idiom `band together`, which means to unite or join forces."+ ),+ "WaveFunction" => (+ ["wavefunction"],+ ["wave function"],+ "Did you mean `wave function`?",+ "Identifies the mistake of merging `wave` and `function` into one word. In quantum mechanics, a `wave function` (written as two words) describes the mathematical function that represents the quantum state of a particle or system. Correct usage is crucial for clear and accurate scientific communication."+ ),+ "InThe" => (+ ["int he"],+ ["in the"],+ "Did you mean `in the`?",+ "Detects and corrects a spacing error where `in the` is mistakenly written as `int he`. Proper spacing is essential for readability and grammatical correctness in common phrases."+ ),+ "WillContain" => (+ ["will contains"],+ ["will contain"],+ "Did you mean `will contain`?",+ "Incorrect verb form: `will` should be followed by the base form `contain`."+ ),+ "IsKnownFor" => (+ ["is know for"],+ ["is known for"],+ "Did you mean `is known for`?",+ "Typo: `known` is the correct past participle."+ ),+ "PointIsMoot" => (+ ["your point is mute"],+ ["your point is moot"],+ "Did you mean `your point is moot`?",+ "Typo: `moot` (meaning debatable) is correct rather than `mute`."+ ),+ "ByAccident" => (+ ["on accident"],+ ["by accident"],+ "Did you mean `by accident`?",+ "Incorrect preposition: `by accident` is the idiomatic expression."+ ),+ "ThatChallenged" => (+ ["the challenged"],+ ["that challenged"],+ "Did you mean `that challenged`?",+ "Changes `the challenged` to `that challenged` to fix the misspelling."+ ),+ "TurnItOff" => (+ ["turn it of", "turn i of"],+ ["turn it off"],+ "Did you mean `turn it off`?",+ "Fixes the mistake in the phrase `turn it off`."+ ),+ "HumanLife" => (+ ["human live"],+ ["human life"],+ "Did you mean `human life`?",+ "Changes `human live` to `human life`."+ ),+ "NeedHelp" => (+ ["ned help"],+ ["need help"],+ "Did you mean `need help`?",+ "Changes `ned help` to the correct `need help`."+ ),+ "AndTheLike" => (+ ["an the like"],+ ["and the like"],+ "Did you mean `and the like`?",+ "Fixes the typo in `and the like`."+ ),+ "BatedBreath" => (+ ["baited breath"],+ ["bated breath"],+ "Did you mean `bated breath`?",+ "Changes `baited breath` to the correct `bated breath`."+ ),+ "BeckAndCall" => (+ ["back and call"],+ ["beck and call"],+ "Did you mean `beck and call`?",+ "Fixes `back and call` to `beck and call`."+ ),+ "LetAlone" => (+ ["let along"],+ ["let alone"],+ "Did you mean `let alone`?",+ "Changes `let along` to `let alone`."+ ),+ "SneakingSuspicion" => (+ ["sneaky suspicion"],+ ["sneaking suspicion"],+ "Did you mean `sneaking suspicion`?",+ "Changes `sneaky suspicion` to `sneaking suspicion`."+ ),+ "SpecialAttention" => (+ ["spacial attention"],+ ["special attention"],+ "Did you mean `special attention`?",+ "Changes `spacial attention` to `special attention`."+ ),+ "SupposedTo" => (+ ["suppose to"],+ ["supposed to"],+ "Did you mean `supposed to`?",+ "Fixes `suppose to` to the correct `supposed to`."+ ),+ "KindRegards" => (+ ["kid regards"],+ ["kind regards"],+ "Did you mean `kind regards`?",+ "Changes `kid regards` to `kind regards`."+ ),+ "ThoughtProcess" => (+ ["though process"],+ ["thought process"],+ "Did you mean `thought process`?",+ "Changes `though process` to `thought process`."+ ),+ "BadRap" => (+ ["bed rap", "bad rep"],+ ["bad rap"],+ "Did you mean `bad rap`?",+ "Changes `bed rap` to the proper idiom `bad rap`."+ ),+ "OfCourse" => (+ ["off course", "o course"],+ ["Of course"],+ "Did you mean `of course`?",+ "Detects the non‐idiomatic phrase `off course` and suggests the correct form `of course`."+ ),+ "FastPaste" => (+ ["fast paste", "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."+ ),+ "EnMasse" => (+ ["on mass", "on masse", "in mass"],+ ["en masse"],+ "Did you mean `en masse`?",+ "Detects variants like `on mass` or `in mass` and suggests `en masse`."+ ),+ "HungerPang" => (+ ["hunger pain"],+ ["hunger pang"],+ "Did you mean `hunger pang`?",+ "Corrects `hunger pain` to `hunger pang`."- )++ ),++ "GetRidOff" => (++ ["get rid off"],++ ["get rid of"],++ "Did you mean `get rid of`?",++ "Ensures `get rid of` is used instead of `get rid off`."++ ),++ "GetsRidOff" => (++ ["gets rid off"],++ ["gets rid of"],++ "Did you mean `gets rid of`?",++ "Ensures `gets rid of` is used instead of `gets rid off`."++ ),++ "GettingRidOff" => (++ ["getting rid off"],++ ["getting rid of"],++ "Did you mean `getting rid of`?",++ "Ensures `getting rid of` is used instead of `getting rid off`."++ ),++ "GotRidOff" => (++ ["got rid off"],++ ["got rid of"],++ "Did you mean `got rid of`?",++ "Ensures `got rid of` is used instead of `got rid off`."++ ),+ });++ group.set_all_rules_to(Some(true));++ group}- create_linter_for_phrase!(TurnItOff, "turn it off", 1);- create_linter_for_phrase!(HumanLife, "human life", 1);- create_linter_for_phrase!(ThatChallenged, "that challenged", 2);- create_linter_for_phrase!(NoLonger, "no longer", 1);- create_linter_for_phrase!(NeedHelp, "need help", 1);- create_linter_for_phrase!(OfCourse, "of course", 1);- create_linter_for_phrase!(AndTheLike, "and the like", 1);- create_linter_for_phrase!(BadRap, "bad rap", 1);- create_linter_for_phrase!(BatedBreath, "bated breath", 1);- create_linter_for_phrase!(BeckAndCall, "beck and call", 1);- create_linter_for_phrase!(HungerPang, "hunger pang", 2);- create_linter_for_phrase!(EnMasse, "en masse", 1);- create_linter_for_phrase!(LetAlone, "let alone", 1);- create_linter_for_phrase!(SneakingSuspicion, "sneaking suspicion", 3);- create_linter_for_phrase!(SpecialAttention, "special attention", 1);- create_linter_for_phrase!(ThanOthers, "than others", 1);- 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!(- ChangeTack,- ExactPhrase::from_phrase("change tact"),- "change tack",- "Did you mean the sailing idiom?",- "Locates minor errors in the sailing idiom `change tack`."- );- 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.");-- create_linter_map_phrase!(- FaceFirst,- ExactPhrase::from_phrase("face first into"),- "Should this be `face-first`?",- "face-first into",- "Ensures `face first` is correctly hyphenated as `face-first` when used before `into`."- );-- create_linter_map_phrase!(- EludedTo,- ExactPhrase::from_phrase("eluded to"),- "alluded to",- "Did you mean `alluded to`?",- "Corrects `eluded to` to `alluded to` in contexts referring to indirect references."- );-- create_linter_map_phrase!(- BaitedBreath,- ExactPhrase::from_phrase("baited breath"),- "bated breath",- "Did you mean `bated breath`?",- "Ensures `bated breath` is written correctly, as `baited breath` is incorrect."- );-- create_linter_map_phrase!(- BareInMind,- ExactPhrase::from_phrase("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`."- );-- create_linter_map_phrase!(MutePoint, ExactPhrase::from_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.");-- create_linter_map_phrase!(- GetRidOff,- ExactPhrase::from_phrase("get rid off"),- "get rid of",- "Did you mean `get rid of`?",- "Ensures `get rid of` is used instead of `get rid off`."- );- create_linter_map_phrase!(- GetsRidOff,- ExactPhrase::from_phrase("gets rid off"),- "gets rid of",- "Did you mean `gets rid of`?",- "Ensures `gets rid of` is used instead of `gets rid off`."- );- create_linter_map_phrase!(- GettingRidOff,- ExactPhrase::from_phrase("getting rid off"),- "getting rid of",- "Did you mean `getting rid of`?",- "Ensures `getting rid of` is used instead of `getting rid off`."- );- create_linter_map_phrase!(- GotRidOff,- ExactPhrase::from_phrase("got rid off"),- "got rid of",- "Did you mean `got rid of`?",- "Ensures `got rid of` is used instead of `got rid off`."- );-#[cfg(test)]mod tests {use crate::linting::tests::{assert_lint_count, assert_suggestion_result};- use super::{- BadRap, BatedBreath, ChangeTack, EnMasse, GetRidOff, GetsRidOff, GettingRidOff, GotRidOff,- HungerPang, LetAlone, LoAndBehold, OfCourse, SneakingSuspicion, SpecialAttention,- SupposedTo, ThanOthers, TurnItOff,- };+ use super::lint_group;+ // todo: 4 tests: get/gets/getting rid off+ #[test]+ fn get_rid_off() {+ assert_suggestion_result(+ "Please bump axios version to get rid off npm warning #624",- GetRidOff::default(),++ lint_group(),+ "Please bump axios version to get rid of npm warning #624",+ );+ }+ fn gets_rid_off() {+ assert_suggestion_result(+ "Adding at as a runtime dependency gets rid off that error",- GetsRidOff::default(),++ lint_group(),+ "Adding at as a runtime dependency gets rid of that error",+ );+ }+ fn getting_rid_off() {- assert_suggestion_result("getting rid off of all the complexity of the different accesses method of API service providers", GettingRidOff::default(), "getting rid of of all the complexity of the different accesses method of API service providers");++ assert_suggestion_result(++ "getting rid off of all the complexity of the different accesses method of API service providers",++ lint_group(),++ "getting rid of of all the complexity of the different accesses method of API service providers",++ );+ }+ fn got_rid_off() {+ assert_suggestion_result(+ "For now we got rid off circular deps in model tree structure and it's API.",- GotRidOff::default(),++ lint_group(),+ "For now we got rid of circular deps in model tree structure and it's API.",+ );+ }+#[test]fn issue_574() {- assert_lint_count("run by one", TurnItOff::default(), 0);+ assert_lint_count("run by one", lint_group(), 0);}#[test]