Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: the the→that the (#3549)
Andrew Dunbar6ccccce5c32c
diff --git a/harper-core/default_config.json b/harper-core/default_config.jsonindex 5fb740642..d4eb9d05d 100644--- a/harper-core/default_config.json+++ b/harper-core/default_config.json@@ -2727,6 +2727,13 @@"state": true,"label": "Close Tight Knit"}+ },+ {+ "Bool": {+ "name": "TheTheToThatThe",+ "state": true,+ "label": "The The To That The"+ }}]}diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rsindex 9e4187a28..88418304f 100644--- a/harper-core/src/linting/lint_group/mod.rs+++ b/harper-core/src/linting/lint_group/mod.rs@@ -226,6 +226,7 @@ use super::the_how_why::TheHowWhy;use super::the_my::TheMy;use super::the_point_for::ThePointFor;use super::the_proper_noun_possessive::TheProperNounPossessive;+use super::the_the_to_that_the::TheTheToThatThe;use super::then_than::ThenThan;use super::there_is_agreement::ThereIsAgreement;use super::there_own::ThereOwn;@@ -780,6 +781,7 @@ impl LintGroup {insert_expr_rule!(TheMy, true);insert_expr_rule!(ThePointFor, true);insert_expr_rule!(TheProperNounPossessive, true);+ insert_expr_rule!(TheTheToThatThe, true);insert_expr_rule!(ThenThan, true);insert_expr_rule!(ThereOwn, true);insert_expr_rule!(Theres, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 4e57f8a7f..39a54340b 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -239,6 +239,7 @@ mod the_how_why;mod the_my;mod the_point_for;mod the_proper_noun_possessive;+mod the_the_to_that_the;mod then_than;mod there_is_agreement;mod there_own;diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rsindex 9d89a9765..28025bc26 100644--- a/harper-core/src/linting/repeated_words.rs+++ b/harper-core/src/linting/repeated_words.rs@@ -77,7 +77,7 @@ impl Linter for RepeatedWords {document.get_span_content(&tok_a.span).to_vec(),)],message: "Did you mean to repeat this word?".to_string(),- ..Default::default()+ priority: 128, // Lower priority than `TheTheToThatThe`})}}diff --git a/harper-core/src/linting/the_the_to_that_the.rs b/harper-core/src/linting/the_the_to_that_the.rsnew file mode 100644index 000000000..34caed46d--- /dev/null+++ b/harper-core/src/linting/the_the_to_that_the.rs@@ -0,0 +1,184 @@+use crate::{+ CharStringExt, Lint, Token, TokenStringExt,+ expr::{Expr, SequenceExpr},+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},+};++pub struct TheTheToThatThe {+ expr: SequenceExpr,+}++impl Default for TheTheToThatThe {+ fn default() -> Self {+ Self {+ expr: SequenceExpr::word_set(&[+ "so", "fact", "found", "show", "say", "believe", "think", "said",+ ])+ .t_ws()+ .t_aco("the")+ .t_ws()+ .t_aco("the"),+ }+ }+}++impl ExprLinter for TheTheToThatThe {+ type Unit = Chunk;++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let span = toks[toks+ .iter()+ .position(|t| t.get_ch(src).eq_ch(&['t', 'h', 'e']))?..][..3]+ .span()?;+ let ch = span.get_content(src);++ Some(Lint {+ span,+ lint_kind: LintKind::Typo,+ suggestions: ["that the", "the"]+ .iter()+ .map(|s| Suggestion::replace_with_match_case_str(s, ch))+ .collect(),+ message: "Did you mean `that the` or just `the`?".to_string(),+ priority: 126, // Higher priority than `RepeatedWords`+ })+ }++ fn expr(&self) -> &dyn Expr {+ &self.expr+ }++ fn description(&self) -> &str {+ "Corrects `the the` to `that the` or to a single `the`."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{+ assert_good_and_bad_suggestions, assert_no_lints, assert_suggestion_result,+ };++ use super::TheTheToThatThe;++ // Contrived tests++ #[test]+ fn dont_flag_simple_the_the() {+ assert_no_lints("this is the the simple case", TheTheToThatThe::default());+ }++ #[test]+ fn fix_so_that_the() {+ assert_suggestion_result(+ "this is so the the other case is tested",+ TheTheToThatThe::default(),+ "this is so that the other case is tested",+ );+ }++ // Real world tests++ #[test]+ fn fact_the_the_to_that() {+ assert_suggestion_result(+ "This PR fixes the fact the the / page and some of the other new pages are reverting to the browser font",+ TheTheToThatThe::default(),+ "This PR fixes the fact that the / page and some of the other new pages are reverting to the browser font",+ );+ }++ #[test]+ fn found_the_the_just_double() {+ assert_suggestion_result(+ "and I've never found the the dashboard feed materially useful",+ TheTheToThatThe::default(),+ "and I've never found the dashboard feed materially useful",+ );+ }++ #[test]+ fn found_the_the_could_be_either() {+ assert_good_and_bad_suggestions(+ "I found the the shape of feature from the PillarFeatureNet becomes [64] instead of [N, 64]",+ TheTheToThatThe::default(),+ &[+ "I found that the shape of feature from the PillarFeatureNet becomes [64] instead of [N, 64]",+ "I found the shape of feature from the PillarFeatureNet becomes [64] instead of [N, 64]",+ ],+ &[],+ );+ }++ #[test]+ fn found_the_the_to_that() {+ assert_suggestion_result(+ "I was directed to the MongoDB website and found the the newer versions require you to have the mongosh shell",+ TheTheToThatThe::default(),+ "I was directed to the MongoDB website and found that the newer versions require you to have the mongosh shell",+ );+ }++ #[test]+ fn say_the_the_to_that() {+ assert_suggestion_result(+ "So, I'm going to say the the shown output is a bug in using Avro",+ TheTheToThatThe::default(),+ "So, I'm going to say that the shown output is a bug in using Avro",+ );+ }++ #[test]+ fn show_the_the_just_double() {+ assert_suggestion_result(+ "It would be better to show the the first logical line (i.e. the first paragraph).",+ TheTheToThatThe::default(),+ "It would be better to show the first logical line (i.e. the first paragraph).",+ );+ }++ #[test]+ fn so_the_the_could_be_either() {+ assert_good_and_bad_suggestions(+ "accordion-body doesn't have data- attributes to read from, so the The proper solution isn't quite working",+ TheTheToThatThe::default(),+ &[+ // TODO: replace_with_match_cases goes by index so results in `thaT` instead of `that`+ // "accordion-body doesn't have data- attributes to read from, so that the proper solution isn't quite working",+ "accordion-body doesn't have data- attributes to read from, so the proper solution isn't quite working",+ ],+ &[],+ );+ }++ #[test]+ fn so_the_the_just_double() {+ assert_suggestion_result(+ "So the the choice is really between pursuing a compromise ...",+ TheTheToThatThe::default(),+ "So the choice is really between pursuing a compromise ...",+ );+ }++ #[test]+ fn think_the_the_to_that() {+ assert_suggestion_result(+ "I don't think the the additional flask route was the issue because I got your example to work.",+ TheTheToThatThe::default(),+ "I don't think that the additional flask route was the issue because I got your example to work.",+ );+ }++ #[test]+ fn think_the_the_could_be_either() {+ assert_good_and_bad_suggestions(+ "but I think the the lack of transparency and attribution to the original coder makes this much more an act of opportunism",+ TheTheToThatThe::default(),+ &[+ "but I think that the lack of transparency and attribution to the original coder makes this much more an act of opportunism",+ "but I think the lack of transparency and attribution to the original coder makes this much more an act of opportunism",+ ],+ &[],+ );+ }+}diff --git a/harper-core/tests/text/linters/The Great Gatsby.snap.yml b/harper-core/tests/text/linters/The Great Gatsby.snap.ymlindex 3ced0d842..3c01f4bb3 100644--- a/harper-core/tests/text/linters/The Great Gatsby.snap.yml+++ b/harper-core/tests/text/linters/The Great Gatsby.snap.yml@@ -1199,7 +1199,7 @@ Suggest:-Lint: Repetition (127 priority)+Lint: Repetition (128 priority)Message: |1065 | “The only crazy I was was when I married him. I knew right away I made a| ^~~~~~~ Did you mean to repeat this word?@@ -4500,7 +4500,7 @@ Suggest:-Lint: Repetition (127 priority)+Lint: Repetition (128 priority)Message: |3135 | uneasy anyhow until he had given them something, realizing in a vague way that| ^~~~~@@ -5604,7 +5604,7 @@ Suggest:-Lint: Repetition (127 priority)+Lint: Repetition (128 priority)Message: |4347 | Her frightened eyes told that whatever intentions, whatever courage she had had,| ^~~~~~~ Did you mean to repeat this word?