Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: fix run into problem/a trouble/troubles (#3685)
Andrew Dunbar7d287b0c9abd
diff --git a/harper-core/default_config.json b/harper-core/default_config.jsonindex 6a1657308..10ff88039 100644--- a/harper-core/default_config.json+++ b/harper-core/default_config.json@@ -5165,6 +5165,13 @@"state": true,"label": "Leaving In Droves"}+ },+ {+ "Bool": {+ "name": "RunIntoProblemsOrTrouble",+ "state": true,+ "label": "Run Into Problems / Trouble"+ }}]}diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex e2e610568..fcaeff999 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -209,6 +209,7 @@ use super::respond::Respond;use super::right_click::RightClick;use super::rise_the_ranks::RiseTheRanks;use super::roller_skated::RollerSkated;+use super::run_into_problems_or_trouble::RunIntoProblemsOrTrouble;use super::safe_to_save::SafeToSave;use super::save_to_safe::SaveToSafe;use super::sentence_capitalization::SentenceCapitalization;@@ -774,6 +775,7 @@ impl LintGroup {insert_expr_rule!(RightClick, true);insert_expr_rule!(RiseTheRanks, true);insert_expr_rule!(RollerSkated, true);+ insert_expr_rule!(RunIntoProblemsOrTrouble, true);insert_expr_rule!(SafeToSave, true);insert_expr_rule!(SaveToSafe, true);insert_struct_rule_with_dict!(SentenceCapitalization, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 77adb9347..996fd2f5c 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -220,6 +220,7 @@ mod respond;mod right_click;mod rise_the_ranks;mod roller_skated;+mod run_into_problems_or_trouble;mod safe_to_save;mod save_to_safe;mod sentence_capitalization;diff --git a/harper-core/src/linting/run_into_problems_or_trouble.rs b/harper-core/src/linting/run_into_problems_or_trouble.rsnew file mode 100644index 000000000..d3be69c05--- /dev/null+++ b/harper-core/src/linting/run_into_problems_or_trouble.rs@@ -0,0 +1,199 @@+use crate::{+ Lint, Token, TokenStringExt,+ expr::{Expr, SequenceExpr},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+ patterns::WordSet,+};++pub struct RunIntoProblemsOrTrouble {+ expr: SequenceExpr,+}++impl Default for RunIntoProblemsOrTrouble {+ fn default() -> Self {+ Self {+ expr: SequenceExpr::word_set(&["run", "ran", "running", "runs"])+ .t_ws()+ .t_aco("into")+ .t_ws()+ .then_any_of(vec![+ Box::new(WordSet::new(&["problem", "troubles"])),+ Box::new(SequenceExpr::word_seq(&["a", "trouble"])),+ ]),+ }+ }+}++impl ExprLinter for RunIntoProblemsOrTrouble {+ type Unit = Chunk;++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let a = match toks.len() {+ 5 => false,+ 7 => true,+ _ => return None,+ };++ let nptoks = &toks[4..];+ let npspan = nptoks.span()?;+ let noun = nptoks.last()?;+ let first = noun.get_ch(src).iter().next()?;+ let last = noun.get_ch(src).iter().last()?;++ // into problem -> problems / a problem+ // into a trouble -> trouble+ // into troubles -> trouble+ let (replacements, msg): (&[&str], &str) = match (a, first, last) {+ (false, &'p' | &'P', &'m' | &'M') => (+ &["problems", "a problem"],+ "The noun `problem` is countable. Use the plural `problems` or add the article `a`.",+ ),+ (true, &'t' | &'T', &'e' | &'E') => (+ &["trouble"],+ "The noun `trouble` is uncountable. Drop the article `a`.",+ ),+ (false, &'t' | &'T', &'s' | &'S') => (+ &["trouble"],+ "The idiom is `run into trouble`. Use the singular form.",+ ),+ _ => return None,+ };++ let suggestions = replacements+ .iter()+ .map(|&replacement| {+ let correction_vec_of_char = replacement.chars().collect::<Vec<char>>();+ Suggestion::replace_with_match_case(correction_vec_of_char, npspan.get_content(src))+ })+ .collect();++ Some(Lint {+ span: npspan,+ lint_kind: LintKind::Usage,+ suggestions,+ message: msg.to_owned(),+ ..Default::default()+ })+ }++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Corrects `running into` `problems` or `trouble` with wrong article, singular, or plural forms."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{assert_good_and_bad_suggestions, assert_suggestion_result};++ use super::RunIntoProblemsOrTrouble;++ #[test]+ fn ran_into_a_trouble() {+ assert_suggestion_result(+ "but recently I ran into a trouble that the release button is not shown",+ RunIntoProblemsOrTrouble::default(),+ "but recently I ran into trouble that the release button is not shown",+ );+ }++ #[test]+ fn ran_into_problem() {+ assert_good_and_bad_suggestions(+ "Ran into problem when trying to install the upgrade assistant",+ RunIntoProblemsOrTrouble::default(),+ &[+ "Ran into problems when trying to install the upgrade assistant",+ "Ran into a problem when trying to install the upgrade assistant",+ ],+ &[],+ );+ }++ #[test]+ fn ran_into_troubles() {+ assert_suggestion_result(+ "I ran into troubles when executing contrib/utilities/update_all_files.sh",+ RunIntoProblemsOrTrouble::default(),+ "I ran into trouble when executing contrib/utilities/update_all_files.sh",+ );+ }++ #[test]+ fn run_into_a_trouble() {+ assert_suggestion_result(+ "I'm have run into a trouble when writing a lexer with below tokens",+ RunIntoProblemsOrTrouble::default(),+ "I'm have run into trouble when writing a lexer with below tokens",+ );+ }++ #[test]+ fn run_into_problem() {+ assert_good_and_bad_suggestions(+ "Running into problem when I add it into chatbox.",+ RunIntoProblemsOrTrouble::default(),+ &[+ "Running into problems when I add it into chatbox.",+ "Running into a problem when I add it into chatbox.",+ ],+ &[],+ );+ }++ #[test]+ fn run_into_troubles() {+ assert_suggestion_result(+ "[post here if you run into troubles]",+ RunIntoProblemsOrTrouble::default(),+ "[post here if you run into trouble]",+ );+ }++ #[test]+ fn running_into_a_trouble() {+ assert_suggestion_result(+ "We are spawning built app without this argument, thus running into a trouble in v3.5.0",+ RunIntoProblemsOrTrouble::default(),+ "We are spawning built app without this argument, thus running into trouble in v3.5.0",+ );+ }++ #[test]+ fn running_into_problem() {+ assert_good_and_bad_suggestions(+ "Running into problem with Prisma when using Git submodules.",+ RunIntoProblemsOrTrouble::default(),+ &[+ "Running into problems with Prisma when using Git submodules.",+ "Running into a problem with Prisma when using Git submodules.",+ ],+ &[],+ );+ }++ #[test]+ fn running_into_troubles() {+ assert_suggestion_result(+ "Running into troubles with 0.6.2",+ RunIntoProblemsOrTrouble::default(),+ "Running into trouble with 0.6.2",+ );+ }++ #[test]+ fn runs_into_problem() {+ assert_good_and_bad_suggestions(+ "Serving FaceNet with Tensorflow Serving runs into problem with PyFunc",+ RunIntoProblemsOrTrouble::default(),+ &[+ "Serving FaceNet with Tensorflow Serving runs into problems with PyFunc",+ "Serving FaceNet with Tensorflow Serving runs into a problem with PyFunc",+ ],+ &[],+ );+ }+}