Skip to content

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

feat: no longer I→I no longer (#3611)

Andrew Dunbarbed2d57a50fc
diff --git a/harper-core/default_config.json b/harper-core/default_config.json
index 71a2a14c8..bab72398e 100644
--- a/harper-core/default_config.json
+++ b/harper-core/default_config.json
@@ -5215,6 +5215,13 @@
"label": "Incident Report"
}
},
+ {
+ "Bool": {
+ "name": "NoLongerPronoun",
+ "state": true,
+ "label": "No Longer Pronoun"
+ }
+ },
{
"Bool": {
"name": "NeitherHereNorThere",
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rs
index 30da67f11..b4307a3c6 100644
--- a/harper-core/src/expr/sequence_expr.rs
+++ b/harper-core/src/expr/sequence_expr.rs
@@ -3,7 +3,7 @@ use paste::paste;
use crate::{
CharStringExt, Lrc, Span, Token, TokenKind,
expr::{AsBoxedExpr, FirstMatchOf, FixedPhrase, LongestMatchOf},
- patterns::{AnyPattern, IndefiniteArticle, WhitespacePattern, Word, WordSet},
+ patterns::{AnyPattern, IndefiniteArticle, RelativePronoun, WhitespacePattern, Word, WordSet},
};
use super::{Expr, Optional, OwnedExprExt, Repeating, Step, UnlessStep};
@@ -597,6 +597,10 @@ impl SequenceExpr {
gen_then_from_is!(subject_pronoun);
gen_then_from_is!(object_pronoun);
+ pub fn then_relative_pronoun(self) -> Self {
+ self.then(RelativePronoun::default())
+ }
+
// Verbs
gen_then_from_is!(verb);
diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rs
index 49f85d1d6..6ce7ac7ca 100644
--- a/harper-core/src/linting/lint_group/mod.rs
+++ b/harper-core/src/linting/lint_group/mod.rs
@@ -158,6 +158,7 @@ use super::naked_eye::NakedEye;
use super::need_to_noun::NeedToNoun;
use super::no_french_spaces::NoFrenchSpaces;
use super::no_longer::NoLonger;
+use super::no_longer_pronoun::NoLongerPronoun;
use super::no_match_for::NoMatchFor;
use super::no_oxford_comma::NoOxfordComma;
use super::nobody::Nobody;
@@ -728,6 +729,7 @@ impl LintGroup {
insert_expr_rule!(NeedToNoun, true);
insert_struct_rule!(NoFrenchSpaces, true);
insert_expr_rule!(NoLonger, true);
+ insert_expr_rule!(NoLongerPronoun, true);
insert_expr_rule!(NoMatchFor, true);
insert_struct_rule!(NoOxfordComma, false);
insert_expr_rule!(Nobody, true);
diff --git a/harper-core/src/linting/lint_kind.rs b/harper-core/src/linting/lint_kind.rs
index cb5c60186..9bda1852b 100644
--- a/harper-core/src/linting/lint_kind.rs
+++ b/harper-core/src/linting/lint_kind.rs
@@ -52,6 +52,9 @@ pub enum LintKind {
Usage,
/// For choosing between different words or phrases in a given context
WordChoice,
+ /// For errors where words are in an unnatural sequence or incorrect syntactic position
+ /// (e.g., "no longer I" vs. "I no longer")
+ WordOrder,
}
impl LintKind {
@@ -105,6 +108,7 @@ impl LintKind {
LintKind::Typo => "Typo",
LintKind::Usage => "Usage",
LintKind::WordChoice => "WordChoice",
+ LintKind::WordOrder => "WordOrder",
}
.to_owned()
}
@@ -133,6 +137,7 @@ impl Display for LintKind {
LintKind::Typo => "Typo",
LintKind::Usage => "Usage",
LintKind::WordChoice => "Word Choice",
+ LintKind::WordOrder => "Word Order",
};
write!(f, "{s}")
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 1d3b20d0d..91a7bc895 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -167,6 +167,7 @@ mod naked_eye;
mod need_to_noun;
mod no_french_spaces;
mod no_longer;
+mod no_longer_pronoun;
mod no_match_for;
mod no_oxford_comma;
mod nobody;
diff --git a/harper-core/src/linting/no_longer_pronoun.rs b/harper-core/src/linting/no_longer_pronoun.rs
new file mode 100644
index 000000000..a1d57324d
--- /dev/null
+++ b/harper-core/src/linting/no_longer_pronoun.rs
@@ -0,0 +1,142 @@
+use crate::{
+ Lint, Token, TokenStringExt,
+ expr::{Expr, SequenceExpr},
+ linting::{
+ ExprLinter, LintKind, Suggestion,
+ expr_linter::{Chunk, followed_by_word},
+ },
+ patterns::{RelativePronoun, SingleTokenPattern},
+};
+
+pub struct NoLongerPronoun {
+ expr: SequenceExpr,
+}
+
+impl Default for NoLongerPronoun {
+ fn default() -> Self {
+ Self {
+ expr: SequenceExpr::aco("no")
+ .t_ws()
+ .t_aco("longer")
+ .t_ws()
+ .then_subject_pronoun()
+ .then_optional(SequenceExpr::whitespace().t_set(&["am", "are", "is"])),
+ }
+ }
+}
+
+impl ExprLinter for NoLongerPronoun {
+ type Unit = Chunk;
+
+ fn match_to_lint_with_context(
+ &self,
+ toks: &[Token],
+ src: &[char],
+ ctx: Option<(&[Token], &[Token])>,
+ ) -> Option<Lint> {
+ let no_longer = &toks[..3];
+ let sp = &toks[3..=3];
+ let pron_etc = &toks[4..];
+
+ if followed_by_word(ctx, |t| RelativePronoun::default().matches_token(t, src)) {
+ return None;
+ }
+
+ let correction: Vec<char> = pron_etc
+ .iter()
+ .chain(sp.iter())
+ .chain(no_longer.iter())
+ .flat_map(|tok| tok.get_ch(src))
+ .copied()
+ .collect();
+
+ Some(Lint {
+ span: toks.span()?,
+ lint_kind: LintKind::WordOrder,
+ // NOTE: `replace_with_match_case` messes up with `I` due to being index-based
+ suggestions: vec![Suggestion::ReplaceWith(correction)],
+ message: "Consider moving the pronoun to before `no longer`.".to_owned(),
+ ..Default::default()
+ })
+ }
+
+ fn expr(&self) -> &dyn Expr {
+ &self.expr
+ }
+
+ fn description(&self) -> &str {
+ "Detects incorrect word order where `no longer` incorrectly precedes a subject pronoun."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};
+
+ use super::NoLongerPronoun;
+
+ #[test]
+ fn fix_no_longer_you_need_to() {
+ assert_suggestion_result(
+ "It works great for poetry and no longer you need to create keys on pypi",
+ NoLongerPronoun::default(),
+ "It works great for poetry and you no longer need to create keys on pypi",
+ )
+ }
+
+ #[test]
+ fn fix_no_longer_it() {
+ assert_suggestion_result(
+ "does that mean that i have make myself an intranet library and no longer it automatically creates a library with my username?",
+ NoLongerPronoun::default(),
+ "does that mean that i have make myself an intranet library and it no longer automatically creates a library with my username?",
+ )
+ }
+
+ #[test]
+ fn fix_no_longer_i_am_able_verify() {
+ assert_suggestion_result(
+ "the hash value got replaced by the app's name and no longer I am able to verify OTP automatically",
+ NoLongerPronoun::default(),
+ "the hash value got replaced by the app's name and I am no longer able to verify OTP automatically",
+ );
+ }
+
+ #[test]
+ fn fix_no_longer_i_am_able_see() {
+ assert_suggestion_result(
+ "there is also 'effect' to be applied, but no longer I am able to see and control their colors",
+ NoLongerPronoun::default(),
+ "there is also 'effect' to be applied, but I am no longer able to see and control their colors",
+ );
+ }
+
+ #[test]
+ #[ignore = "I think the word 'even' being in the wrong place here is a separate mistake"]
+ fn no_longer_we_are_interested() {
+ assert_suggestion_result(
+ "I do not also care about what you think and even no longer we are interested to demo tamota buggy feature for our class",
+ NoLongerPronoun::default(),
+ "I do not also care about what you think and we are no longer even interested to demo tamota buggy feature for our class",
+ );
+ }
+
+ // Potential false positives
+
+ #[test]
+ fn allow_i_who() {
+ assert_no_lints(
+ "It is no longer I who live, but Christ who lives in me",
+ NoLongerPronoun::default(),
+ );
+ }
+
+ #[test]
+ #[ignore = "If this turns out to be common, checking for a modal verb before or 'to' after might work"]
+ fn different_mistake_missing_word() {
+ assert_no_lints(
+ "If I understand this change correctly this change would no longer you to duplicate materials or make them unique to pass in unique parameters",
+ NoLongerPronoun::default(),
+ )
+ }
+}
diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rs
index 4346c6aba..1d02b9b2f 100644
--- a/harper-core/src/patterns/mod.rs
+++ b/harper-core/src/patterns/mod.rs
@@ -16,6 +16,7 @@ mod invert;
mod modal_verb;
mod nominal_phrase;
mod prepositional_preceder;
+mod relative_pronoun;
mod upos_set;
mod whitespace_pattern;
mod within_edit_distance;
@@ -31,6 +32,7 @@ pub use invert::Invert;
pub use modal_verb::ModalVerb;
pub use nominal_phrase::NominalPhrase;
pub use prepositional_preceder::{PrepositionalPrecederPattern, prepositional_preceder};
+pub use relative_pronoun::RelativePronoun;
pub use upos_set::UPOSSet;
pub use whitespace_pattern::WhitespacePattern;
pub use within_edit_distance::WithinEditDistance;
diff --git a/harper-core/src/patterns/relative_pronoun.rs b/harper-core/src/patterns/relative_pronoun.rs
new file mode 100644
index 000000000..8f885ba85
--- /dev/null
+++ b/harper-core/src/patterns/relative_pronoun.rs
@@ -0,0 +1,21 @@
+use crate::Token;
+
+use super::{SingleTokenPattern, WordSet};
+
+pub struct RelativePronoun {
+ inner: WordSet,
+}
+
+impl Default for RelativePronoun {
+ fn default() -> Self {
+ Self {
+ inner: WordSet::new(&["that", "which", "who", "whom", "whose"]),
+ }
+ }
+}
+
+impl SingleTokenPattern for RelativePronoun {
+ fn matches_token(&self, token: &Token, source: &[char]) -> bool {
+ self.inner.matches_token(token, source)
+ }
+}
diff --git a/harper-desktop/src-tauri/src/highlighter/render_state.rs b/harper-desktop/src-tauri/src/highlighter/render_state.rs
index 737f54650..71cddaf80 100644
--- a/harper-desktop/src-tauri/src/highlighter/render_state.rs
+++ b/harper-desktop/src-tauri/src/highlighter/render_state.rs
@@ -649,7 +649,8 @@ fn is_spelling_kind(lint_kind: LintKind) -> bool {
| LintKind::Repetition
| LintKind::Style
| LintKind::Usage
- | LintKind::WordChoice => false,
+ | LintKind::WordChoice
+ | LintKind::WordOrder => false,
}
}
@@ -689,7 +690,8 @@ fn popup_style_for_lint_kind(lint_kind: LintKind) -> PopupStyle {
| LintKind::Regionalism
| LintKind::Style
| LintKind::Usage
- | LintKind::WordChoice => PopupStyle {
+ | LintKind::WordChoice
+ | LintKind::WordOrder => PopupStyle {
color,
background: hex(0xf3, 0xf4, 0xf6),
foreground: hex(0x37, 0x41, 0x51),
diff --git a/harper-desktop/src-tauri/src/lint_kind_color.rs b/harper-desktop/src-tauri/src/lint_kind_color.rs
index 8dbf1e9ad..9f033d98c 100644
--- a/harper-desktop/src-tauri/src/lint_kind_color.rs
+++ b/harper-desktop/src-tauri/src/lint_kind_color.rs
@@ -53,6 +53,7 @@ pub fn lint_kind_color(lint_kind: LintKind) -> Color {
LintKind::Typo => Color::new(0xFF, 0x6B, 0x35),
LintKind::Usage => Color::new(0x1E, 0x90, 0xFF),
LintKind::WordChoice => Color::new(0x22, 0x8B, 0x22),
+ LintKind::WordOrder => Color::new(0x4D, 0x4D, 0xFF),
}
}
diff --git a/packages/harper.js/src/main.ts b/packages/harper.js/src/main.ts
index 21cf2ff75..b0e364765 100644
--- a/packages/harper.js/src/main.ts
+++ b/packages/harper.js/src/main.ts
@@ -36,7 +36,8 @@ export type LintKind =
| 'Style'
| 'Typo'
| 'Usage'
- | 'WordChoice';
+ | 'WordChoice'
+ | 'WordOrder';
export type StructuredLintSetting =
| StructuredLintBoolSetting
diff --git a/packages/lint-framework/src/lint/lintKindColor.ts b/packages/lint-framework/src/lint/lintKindColor.ts
index d0e8d052d..1f886af35 100644
--- a/packages/lint-framework/src/lint/lintKindColor.ts
+++ b/packages/lint-framework/src/lint/lintKindColor.ts
@@ -25,6 +25,7 @@ const LINT_KIND_COLORS = {
Typo: '#FF6B35', // Vibrant orange-red
Usage: '#1E90FF', // Dodger blue
WordChoice: '#228B22', // Forest green
+ WordOrder: '#4D4DFF', // Royal blue
} as const satisfies Record<LintKind, string>;
// Export the array of all lint kind names
diff --git a/packages/obsidian-plugin/src/lintKindColor.ts b/packages/obsidian-plugin/src/lintKindColor.ts
index 8ca1fe8a9..2d184d601 100644
--- a/packages/obsidian-plugin/src/lintKindColor.ts
+++ b/packages/obsidian-plugin/src/lintKindColor.ts
@@ -21,6 +21,7 @@ export const LINT_KIND_COLORS = {
Typo: '#FF6B35',
Usage: '#1E90FF',
WordChoice: '#228B22',
+ WordOrder: '#4D4DFF',
} as const;
export function lintKindColor(lintKindKey: string): string {