Skip to content

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

feat: implements #1838 (#1840)

Andrew Dunbar885e5f80decd
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rs
index 2bc57acd4..c38b5c6ce 100644
--- a/harper-core/src/expr/sequence_expr.rs
+++ b/harper-core/src/expr/sequence_expr.rs
@@ -324,6 +324,7 @@ impl SequenceExpr {
// Pronouns
gen_then_from_is!(pronoun);
+ gen_then_from_is!(personal_pronoun);
gen_then_from_is!(first_person_singular_pronoun);
gen_then_from_is!(first_person_plural_pronoun);
gen_then_from_is!(second_person_pronoun);
diff --git a/harper-core/src/linting/cant.rs b/harper-core/src/linting/cant.rs
new file mode 100644
index 000000000..3f140e199
--- /dev/null
+++ b/harper-core/src/linting/cant.rs
@@ -0,0 +1,153 @@
+use super::{ExprLinter, Suggestion};
+use crate::Lint;
+use crate::expr::{Expr, LongestMatchOf, SequenceExpr};
+use crate::linting::LintKind;
+use crate::linting::expr_linter::find_the_only_token_matching;
+use crate::{CharStringExt, Token};
+
+pub struct Cant {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for Cant {
+ fn default() -> Self {
+ let nom_cant = SequenceExpr::default()
+ .then_kind_except(|kind| kind.is_nominal(), &["or"])
+ .t_ws()
+ .t_aco("cant");
+ let cant_pron = SequenceExpr::aco("cant").t_ws().then_personal_pronoun();
+ let cant_verb = SequenceExpr::aco("cant")
+ .t_ws()
+ .then_kind_is_but_is_not(|kind| kind.is_verb(), |kind| kind.is_noun());
+
+ Self {
+ expr: Box::new(LongestMatchOf::new(vec![
+ Box::new(nom_cant),
+ Box::new(cant_pron),
+ Box::new(cant_verb),
+ ])),
+ }
+ }
+}
+
+// TODO: This can be removed once #1730 is merged
+fn is_verb_lemma(tok: &Token, src: &[char]) -> bool {
+ tok.kind.is_verb()
+ && !tok
+ .span
+ .get_content(src)
+ .ends_with_ignore_ascii_case_chars(&['s'])
+ && !tok
+ .span
+ .get_content(src)
+ .ends_with_ignore_ascii_case_chars(&['e', 'd'])
+ && !tok
+ .span
+ .get_content(src)
+ .ends_with_ignore_ascii_case_chars(&['i', 'n', 'g'])
+}
+
+impl ExprLinter for Cant {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let token = find_the_only_token_matching(toks, src, |tok, src| {
+ tok.span
+ .get_content(src)
+ .eq_ignore_ascii_case_chars(&['c', 'a', 'n', 't'])
+ })?;
+
+ let jargon = token.span.get_content(src);
+ let cannot = "can't";
+
+ Some(Lint {
+ span: token.span,
+ lint_kind: LintKind::Enhancement,
+ suggestions: vec![Suggestion::replace_with_match_case_str(cannot, jargon)],
+ message: "`Cant` is secret language or jargon. If that's not what you mean you should use `can't` here.".to_string(),
+ priority: 127,
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Suggests correcting `cant` to `can't`."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::Cant;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn corrects_pronoun_cant() {
+ assert_suggestion_result(
+ "I cant go to the store.",
+ Cant::default(),
+ "I can't go to the store.",
+ );
+ }
+
+ #[test]
+ fn corrects_proper_noun_cant() {
+ assert_suggestion_result(
+ "Bob cant go to the store.",
+ Cant::default(),
+ "Bob can't go to the store.",
+ );
+ }
+
+ #[test]
+ fn corrects_common_noun_cant() {
+ // "dog" and "cat" are
+ assert_suggestion_result(
+ "A horse cant drink bottled water.",
+ Cant::default(),
+ "A horse can't drink bottled water.",
+ );
+ }
+
+ #[test]
+ fn corrects_cant_pronoun() {
+ assert_suggestion_result(
+ "Cant you go to the store?",
+ Cant::default(),
+ "Can't you go to the store?",
+ );
+ }
+
+ #[test]
+ fn dont_flag_if_cant_is_part_of_noun_phrase() {
+ assert_lint_count("Cant cant be the same as jargon.", Cant::default(), 0);
+ }
+
+ #[test]
+ fn dont_flag_cant_project() {
+ assert_lint_count(
+ "The CANT project is designed to allow people to screw around with CAN easily at layers 1/2.",
+ Cant::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ #[ignore = "'Convert' is also a noun, so a 'cant convert' could be a person who switched to speaking jargon"]
+ fn corrects_cant_verb() {
+ assert_suggestion_result(
+ "Cant convert widget to input",
+ Cant::default(),
+ "Can't convert widget to input",
+ );
+ }
+
+ #[test]
+ fn dont_flag_legit_noun_sense() {
+ assert_lint_count(
+ "CB Slang Dictionary is the distinctive anti-language, argot or cant which developed amongst users of citizens' band radio (CB), especially truck drivers",
+ Cant::default(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/expr_linter.rs b/harper-core/src/linting/expr_linter.rs
index b2533476b..0352daa2a 100644
--- a/harper-core/src/linting/expr_linter.rs
+++ b/harper-core/src/linting/expr_linter.rs
@@ -23,6 +23,25 @@ pub trait ExprLinter: LSend {
fn description(&self) -> &str;
}
+/// Helper function to find the only occurrence of a token matching a predicate
+///
+/// Returns `Some(token)` if exactly one token matches the predicate, `None` otherwise.
+/// TODO: This can be used in the [`ThenThan`] linter when #1819 is merged.
+pub fn find_the_only_token_matching<'a, F>(
+ tokens: &'a [Token],
+ source: &[char],
+ predicate: F,
+) -> Option<&'a Token>
+where
+ F: Fn(&Token, &[char]) -> bool,
+{
+ let mut matches = tokens.iter().filter(|&tok| predicate(tok, source));
+ match (matches.next(), matches.next()) {
+ (Some(tok), None) => Some(tok),
+ _ => None,
+ }
+}
+
impl<L> Linter for L
where
L: ExprLinter,
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index e109ff5e5..6bbbc47ef 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -24,6 +24,7 @@ use super::avoid_curses::AvoidCurses;
use super::back_in_the_day::BackInTheDay;
use super::best_of_all_time::BestOfAllTime;
use super::boring_words::BoringWords;
+use super::cant::Cant;
use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;
use super::chock_full::ChockFull;
use super::comma_fixes::CommaFixes;
@@ -413,6 +414,7 @@ impl LintGroup {
insert_expr_rule!(BackInTheDay, true);
insert_expr_rule!(BestOfAllTime, true);
insert_expr_rule!(BoringWords, false);
+ insert_expr_rule!(Cant, true);
insert_struct_rule!(CapitalizePersonalPronouns, true);
insert_expr_rule!(ChockFull, true);
insert_struct_rule!(CommaFixes, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 16622efaf..6b2c7bbd5 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -15,6 +15,7 @@ mod avoid_curses;
mod back_in_the_day;
mod best_of_all_time;
mod boring_words;
+mod cant;
mod capitalize_personal_pronouns;
mod chock_full;
mod closed_compounds;
@@ -149,6 +150,7 @@ pub use avoid_curses::AvoidCurses;
pub use back_in_the_day::BackInTheDay;
pub use best_of_all_time::BestOfAllTime;
pub use boring_words::BoringWords;
+pub use cant::Cant;
pub use capitalize_personal_pronouns::CapitalizePersonalPronouns;
pub use chock_full::ChockFull;
pub use comma_fixes::CommaFixes;
diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rs
index c163130ff..8a02a6cab 100644
--- a/harper-core/src/token_kind.rs
+++ b/harper-core/src/token_kind.rs
@@ -76,6 +76,7 @@ impl TokenKind {
is_plural_pronoun,
is_non_plural_pronoun,
is_reflexive_pronoun,
+ is_personal_pronoun,
is_first_person_singular_pronoun,
is_first_person_plural_pronoun,
is_second_person_pronoun,