Skip to content

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

feat: correct "though" when typo for "thought" (#1952)

Andrew Dunbar64bf7e3776a6
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rs
index bca312630..31054f022 100644
--- a/harper-core/src/expr/sequence_expr.rs
+++ b/harper-core/src/expr/sequence_expr.rs
@@ -418,6 +418,7 @@ impl SequenceExpr {
gen_then_from_is!(third_person_pronoun);
gen_then_from_is!(third_person_singular_pronoun);
gen_then_from_is!(third_person_plural_pronoun);
+ gen_then_from_is!(subject_pronoun);
gen_then_from_is!(object_pronoun);
// Verbs
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 9ec731e63..eba823e57 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -119,6 +119,7 @@ use super::the_how_why::TheHowWhy;
use super::the_my::TheMy;
use super::then_than::ThenThan;
use super::thing_think::ThingThink;
+use super::though_thought::ThoughThought;
use super::throw_rubbish::ThrowRubbish;
use super::to_two_too::ToTwoToo;
use super::touristic::Touristic;
@@ -517,6 +518,7 @@ impl LintGroup {
insert_struct_rule!(TheMy, true);
insert_expr_rule!(ThenThan, true);
insert_expr_rule!(ThingThink, true);
+ insert_expr_rule!(ThoughThought, true);
insert_struct_rule!(ThrowRubbish, true);
insert_struct_rule!(ToTwoToo, true);
insert_expr_rule!(Touristic, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 501ce85a3..2c953ec88 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -133,6 +133,7 @@ mod the_how_why;
mod the_my;
mod then_than;
mod thing_think;
+mod though_thought;
mod throw_rubbish;
mod to_two_too;
mod touristic;
@@ -265,6 +266,7 @@ pub use the_how_why::TheHowWhy;
pub use the_my::TheMy;
pub use then_than::ThenThan;
pub use thing_think::ThingThink;
+pub use though_thought::ThoughThought;
pub use throw_rubbish::ThrowRubbish;
pub use to_two_too::ToTwoToo;
pub use touristic::Touristic;
diff --git a/harper-core/src/linting/though_thought.rs b/harper-core/src/linting/though_thought.rs
new file mode 100644
index 000000000..8a4cda418
--- /dev/null
+++ b/harper-core/src/linting/though_thought.rs
@@ -0,0 +1,163 @@
+use crate::expr::{Expr, SequenceExpr};
+use crate::linting::expr_linter::find_the_only_token_matching;
+use crate::linting::{ExprLinter, Lint, LintKind, Suggestion};
+use crate::{CharStringExt, Token, TokenKind};
+
+pub struct ThoughThought {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for ThoughThought {
+ fn default() -> Self {
+ Self {
+ expr: Box::new(
+ SequenceExpr::default()
+ .then_kind_is_but_is_not(
+ TokenKind::is_subject_pronoun,
+ TokenKind::is_object_pronoun,
+ )
+ .t_ws()
+ .t_aco("though")
+ .t_ws()
+ .then_any_of(vec![
+ Box::new(SequenceExpr::default().then_subject_pronoun()),
+ Box::new(SequenceExpr::aco("that")),
+ ]),
+ ),
+ }
+ }
+}
+
+impl ExprLinter for ThoughThought {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let tok = find_the_only_token_matching(toks, src, |tok, src| {
+ tok.span
+ .get_content(src)
+ .eq_ignore_ascii_case_chars(&['t', 'h', 'o', 'u', 'g', 'h'])
+ })?;
+
+ Some(Lint {
+ span: tok.span,
+ lint_kind: LintKind::Typo,
+ suggestions: vec![Suggestion::replace_with_match_case_str(
+ "thought",
+ tok.span.get_content(src),
+ )],
+ message: "Is this a typo for `thought`?".to_string(),
+ ..Default::default()
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Corrects `though` when it's a typo for `thought`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::{
+ ThoughThought,
+ tests::{assert_no_lints, assert_suggestion_result},
+ };
+
+ #[test]
+ fn fix_i_though_i() {
+ assert_suggestion_result(
+ "Looking at those I though I had to draw imgui into separate renderpass",
+ ThoughThought::default(),
+ "Looking at those I thought I had to draw imgui into separate renderpass",
+ );
+ }
+
+ #[test]
+ fn fix_i_though_it() {
+ assert_suggestion_result(
+ "and I though it was a shame because the data it provides can be ...",
+ ThoughThought::default(),
+ "and I thought it was a shame because the data it provides can be ...",
+ );
+ }
+
+ #[test]
+ fn fix_i_though_that() {
+ assert_suggestion_result(
+ "I though that there may be other solutions as in other here",
+ ThoughThought::default(),
+ "I thought that there may be other solutions as in other here",
+ );
+ }
+
+ #[test]
+ fn fix_i_though_they() {
+ assert_suggestion_result(
+ "path parsin error ( i though they were extincted )",
+ ThoughThought::default(),
+ "path parsin error ( i thought they were extincted )",
+ );
+ }
+
+ #[test]
+ fn fix_i_though_we() {
+ assert_suggestion_result(
+ "and that way I though we need something universial",
+ ThoughThought::default(),
+ "and that way I thought we need something universial",
+ );
+ }
+
+ #[test]
+ fn fix_i_though_you() {
+ assert_suggestion_result(
+ "I though you resolved the issue, so I updated my version",
+ ThoughThought::default(),
+ "I thought you resolved the issue, so I updated my version",
+ );
+ }
+
+ #[test]
+ fn dont_flag_it_though_i() {
+ assert_no_lints(
+ "am including it though i believe it's nto the case because before this",
+ ThoughThought::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_it_though_it() {
+ assert_no_lints(
+ "Prisma works with it though it is not officially supported by Prisma yet.",
+ ThoughThought::default(),
+ );
+ }
+
+ #[test]
+ #[ignore = "TODO: Can't check because `it` is both subject and object"]
+ fn fix_you_though_it() {
+ assert_suggestion_result(
+ "it may reveal that the bug is not where you though it was",
+ ThoughThought::default(),
+ "it may reveal that the bug is not where you thought it was",
+ );
+ }
+
+ #[test]
+ fn dont_flag_you_though_that_1() {
+ // Ambiguous: "I can tell you, though, that a project..." vs "I can tell (that) you thought that a project..."
+ assert_no_lints(
+ "I can tell you though that a project not using headers at all will likely be compiling much faster.",
+ ThoughThought::default(),
+ );
+ }
+
+ #[test]
+ fn dont_flag_you_though_that_2() {
+ assert_no_lints(
+ "I agree with you though that 2D lat/lon grids are unnecessarily confusing",
+ ThoughThought::default(),
+ );
+ }
+}
diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rs
index 76615bbf3..04530dd6f 100644
--- a/harper-core/src/token_kind.rs
+++ b/harper-core/src/token_kind.rs
@@ -83,6 +83,7 @@ impl TokenKind {
is_third_person_pronoun,
is_third_person_singular_pronoun,
is_third_person_plural_pronoun,
+ is_subject_pronoun,
is_object_pronoun,
is_possessive_noun,
is_possessive_pronoun,