Skip to content

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

feat: (since) indefinite durations (#3283)

Andrew Dunbar54f13f8436ba
diff --git a/harper-core/src/expr/duration_expr.rs b/harper-core/src/expr/duration_expr.rs
index 0659df7b2..d39eaaa81 100644
--- a/harper-core/src/expr/duration_expr.rs
+++ b/harper-core/src/expr/duration_expr.rs
@@ -1,5 +1,7 @@
-use crate::patterns::{IndefiniteArticle, WordSet};
-use crate::{Span, Token};
+use crate::{
+ Span, Token,
+ patterns::{IndefiniteArticle, WordSet},
+};
use super::{Expr, SequenceExpr, SpelledNumberExpr};
@@ -17,10 +19,23 @@ impl Expr for DurationExpr {
"weeks", "month", "months", "year", "years",
]);
+ // standard "a couple of" and colloquial "a couple"
+ let a_couple_of = SequenceExpr::default()
+ .t_aco("a")
+ .t_ws()
+ .t_aco("couple")
+ .then_optional(SequenceExpr::default().t_ws().t_aco("of"));
+
+ // positive "a few" and negative "few"
+ let a_few = SequenceExpr::optional(SequenceExpr::aco("a").t_ws()).t_aco("few");
+
let expr = SequenceExpr::longest_of(vec![
Box::new(SpelledNumberExpr),
Box::new(SequenceExpr::default().then_number()),
Box::new(IndefiniteArticle::default()),
+ Box::new(a_couple_of),
+ Box::new(a_few),
+ Box::new(SequenceExpr::default().then_quantifier()),
])
.then_whitespace()
.then(units);
@@ -49,4 +64,13 @@ pub mod tests {
let matches = DurationExpr.iter_matches_in_doc(&doc).collect::<Vec<_>>();
assert_eq!(matches.to_strings(&doc), vec!["ten days"]);
}
+
+ #[test]
+ fn detect_a_few_months() {
+ let doc = Document::new_markdown_default_curated(
+ "I am struggling since a few months with the rebuild of an old FORTRAN program.",
+ );
+ let matches = DurationExpr.iter_matches_in_doc(&doc).collect::<Vec<_>>();
+ assert_eq!(matches.to_strings(&doc), vec!["a few months"]);
+ }
}
diff --git a/harper-core/src/linting/since_duration.rs b/harper-core/src/linting/since_duration.rs
index 6fc846584..999720e80 100644
--- a/harper-core/src/linting/since_duration.rs
+++ b/harper-core/src/linting/since_duration.rs
@@ -1,8 +1,8 @@
-use crate::expr::{DurationExpr, Expr, SequenceExpr};
-use crate::{CharStringExt, Token, TokenStringExt};
-
-use super::{ExprLinter, Lint, LintKind, Suggestion};
-use crate::linting::expr_linter::Chunk;
+use crate::{
+ CharStringExt, Token, TokenStringExt,
+ expr::{DurationExpr, Expr, SequenceExpr},
+ linting::{ExprLinter, Lint, LintKind, Suggestion, expr_linter::Chunk},
+};
const AGO_VARIANTS: [&[char]; 3] = [&['a', 'g', 'o'], &['A', 'g', 'o'], &['A', 'G', 'O']];
const FOR_VARIANTS: [&[char]; 3] = [&['f', 'o', 'r'], &['F', 'o', 'r'], &['F', 'O', 'R']];
@@ -272,7 +272,6 @@ mod tests {
}
#[test]
- #[ignore = "We can't yet handle indefinite numbers."]
fn not_yet_handled_3() {
assert_suggestion_result(
"I use a Wacom Cintiq 27QHDT since several years on Linux",
@@ -288,4 +287,44 @@ mod tests {
SinceDuration::default(),
);
}
+
+ fn correct_since_a_few_months() {
+ assert_suggestion_result(
+ "I am struggling since a few months with the rebuild of an old FORTRAN program.",
+ SinceDuration::default(),
+ "I am struggling for a few months with the rebuild of an old FORTRAN program.",
+ );
+ }
+
+ // Note: Expects "few" to be corrected to "a few" by a different linter
+ fn correct_since_few_days() {
+ assert_suggestion_result(
+ "I've been struggling since few days with the rebuild of an old FORTRAN program.",
+ SinceDuration::default(),
+ "I've been struggling for few days with the rebuild of an old FORTRAN program.",
+ );
+ }
+
+ fn dont_flag_since_a_couple_hours_ago() {
+ assert_no_lints(
+ "[BUG] since a couple hours ago, the claude code agent often gets stuck while working.",
+ SinceDuration::default(),
+ );
+ }
+
+ fn fix_since_a_couple_of_days() {
+ assert_suggestion_result(
+ "Since a couple of days, I got this error",
+ SinceDuration::default(),
+ "For a couple of days, I got this error",
+ );
+ }
+
+ fn fix_since_a_couple_days() {
+ assert_suggestion_result(
+ "Values at 0 all the time since a couple days · Issue #91551",
+ SinceDuration::default(),
+ "Values at 0 all the time since a couple days ago · Issue #91551",
+ );
+ }
}