Skip to content

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

Merge branch 'master' into operative-system

Elijah Potterfc8b69162b97
diff --cc harper-core/src/linting/phrase_corrections.rs
index e5398b4b3,671b70954..fb3a89d42
--- a/harper-core/src/linting/phrase_corrections.rs
+++ b/harper-core/src/linting/phrase_corrections.rs
@@@ -1,159 -1,230 +1,242 @@@
- 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,
- })
- }
+ use crate::patterns::{ExactPhrase, OwnedPatternExt};
+
+ 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_similar_mappings {
+ ($group:expr, { $($name:expr => ($phrase:expr, $threshold:expr)),+ $(,)? }) => {
+ $(
+ $group.add(
+ $name,
+ Box::new(MapPhraseLinter::new_similar_to_phrase($phrase, $threshold)),
+ );
+ )+
+ };
+ }
- fn description(&self) -> &'static str {
- $description
- }
- }
- };
- }
+ // 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),
+ "KindRegards" => ("kind regards", 1),
+ "ThoughtProcess" => ("thought process", 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,
+ )),
+ );
+ )+
+ };
+ }
- /// 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`."
++ ),
+ });
+
+ 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));
+
+ 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!(
- OperativeSystem,
- ExactPhrase::from_phrase("operative system"),
- "operating system",
- "Did you mean `operating system`?",
- "Ensures `operating system` is used correctly instead of `operative system`."
- );
- create_linter_map_phrase!(
- OperativeSystems,
- ExactPhrase::from_phrase("operative systems"),
- "operating systems",
- "Did you mean `operating systems`?",
- "Ensures `operating systems` is used correctly instead of `operative systems`."
- );
-
#[cfg(test)]
mod tests {
use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
@@@ -272,27 -321,6 +333,24 @@@
#[test]
fn now_on_hold() {
- assert_lint_count(
- "Those are now on hold for month.",
- LoAndBehold::default(),
- 0,
- );
+ assert_lint_count("Those are now on hold for month.", lint_group(), 0);
}
+
+ #[test]
+ fn operative_system() {
+ assert_suggestion_result(
+ "COS is a operative system made with the COSMOS Kernel and written in C#, COS its literally the same than MS-DOS but written in C# and open-source.",
- OperativeSystem::default(),
++ lint_group(),
+ "COS is a operating system made with the COSMOS Kernel and written in C#, COS its literally the same than MS-DOS but written in C# and open-source.",
+ );
+ }
++
+ #[test]
+ fn operative_systems() {
+ assert_suggestion_result(
+ "My dotfiles for my operative systems and other configurations.",
- OperativeSystems::default(),
++ lint_group(),
+ "My dotfiles for my operating systems and other configurations.",
+ );
+ }
}