Skip to content

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

feat: few time-units ago→a few time-units ago (#1343)

Andrew Dunbar0f9221badebe
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index 639906fc4..519650a5a 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -6,7 +6,7 @@ use paste::paste;
use crate::parsers::{Markdown, MarkdownOptions, Parser, PlainEnglish};
use crate::patterns::{
- DocPattern, EitherPattern, Pattern, RepeatingPattern, SequencePattern, WordSet,
+ DocPattern, LongestMatchOf, Pattern, RepeatingPattern, SequencePattern, WordSet,
};
use crate::punctuation::Punctuation;
use crate::vec_ext::VecExt;
@@ -478,11 +478,11 @@ impl Document {
}
thread_local! {
- static LATIN_PATTERN: Lrc<EitherPattern> = Document::uncached_latin_pattern();
+ static LATIN_PATTERN: Lrc<LongestMatchOf> = Document::uncached_latin_pattern();
}
- fn uncached_latin_pattern() -> Lrc<EitherPattern> {
- Lrc::new(EitherPattern::new(vec![
+ fn uncached_latin_pattern() -> Lrc<LongestMatchOf> {
+ Lrc::new(LongestMatchOf::new(vec![
Box::new(
SequencePattern::default()
.then(WordSet::new(&["etc", "vs"]))
diff --git a/harper-core/src/linting/a_part.rs b/harper-core/src/linting/a_part.rs
index 0215f45d8..6bc065b4e 100644
--- a/harper-core/src/linting/a_part.rs
+++ b/harper-core/src/linting/a_part.rs
@@ -1,7 +1,7 @@
use crate::{
Token, TokenStringExt,
linting::{Lint, LintKind, PatternLinter, Suggestion},
- patterns::{EitherPattern, FixedPhrase, Pattern},
+ patterns::{FixedPhrase, LongestMatchOf, Pattern},
};
pub struct APart {
@@ -10,7 +10,7 @@ pub struct APart {
impl Default for APart {
fn default() -> Self {
- let pattern = EitherPattern::new(vec![
+ let pattern = LongestMatchOf::new(vec![
Box::new(FixedPhrase::from_phrase("a part from")),
Box::new(FixedPhrase::from_phrase("apart of")),
Box::new(FixedPhrase::from_phrase("fall a part")),
diff --git a/harper-core/src/linting/amounts_for.rs b/harper-core/src/linting/amounts_for.rs
index 0130ff666..2bb0d34b7 100644
--- a/harper-core/src/linting/amounts_for.rs
+++ b/harper-core/src/linting/amounts_for.rs
@@ -1,6 +1,6 @@
use crate::{
Lrc, Token, TokenStringExt,
- patterns::{EitherPattern, FixedPhrase, Pattern, SequencePattern, WordSet},
+ patterns::{FixedPhrase, LongestMatchOf, Pattern, SequencePattern, WordSet},
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -28,7 +28,7 @@ impl Default for AmountsFor {
.then(Lrc::new(FixedPhrase::from_phrase("amount for")));
Self {
- pattern: Box::new(EitherPattern::new(vec![
+ pattern: Box::new(LongestMatchOf::new(vec![
Box::new(singular_pattern),
Box::new(plural_pattern),
])),
diff --git a/harper-core/src/linting/chock_full.rs b/harper-core/src/linting/chock_full.rs
index 950b651c9..c6ac54cc4 100644
--- a/harper-core/src/linting/chock_full.rs
+++ b/harper-core/src/linting/chock_full.rs
@@ -1,6 +1,6 @@
use crate::{
Token, TokenStringExt,
- patterns::{EitherPattern, Pattern, SequencePattern, WhitespacePattern, WordSet},
+ patterns::{LongestMatchOf, Pattern, SequencePattern, WhitespacePattern, WordSet},
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -15,7 +15,7 @@ impl Default for ChockFull {
pattern: Box::new(
SequencePattern::default()
.then(WordSet::new(&["chalk", "choke"]))
- .then(EitherPattern::new(vec![
+ .then(LongestMatchOf::new(vec![
Box::new(WhitespacePattern),
Box::new(|tok: &Token, _source: &[char]| tok.kind.is_hyphen()),
]))
diff --git a/harper-core/src/linting/dashes.rs b/harper-core/src/linting/dashes.rs
index 9086135f3..9d10ad122 100644
--- a/harper-core/src/linting/dashes.rs
+++ b/harper-core/src/linting/dashes.rs
@@ -1,6 +1,6 @@
use crate::{
Token, TokenStringExt,
- patterns::{EitherPattern, Pattern, SequencePattern},
+ patterns::{LongestMatchOf, Pattern, SequencePattern},
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -20,7 +20,7 @@ impl Default for Dashes {
.then_hyphen()
.then_one_or_more_hyphens();
- let pattern = EitherPattern::new(vec![Box::new(em_dash_or_longer), Box::new(en_dash)]);
+ let pattern = LongestMatchOf::new(vec![Box::new(em_dash_or_longer), Box::new(en_dash)]);
Self {
pattern: Box::new(pattern),
diff --git a/harper-core/src/linting/everyday.rs b/harper-core/src/linting/everyday.rs
index 1d52ed1e1..392be7a29 100644
--- a/harper-core/src/linting/everyday.rs
+++ b/harper-core/src/linting/everyday.rs
@@ -1,7 +1,7 @@
use super::{Lint, LintKind, PatternLinter, Suggestion};
use crate::{
Lrc, Punctuation, Token, TokenKind, TokenStringExt,
- patterns::{All, EitherPattern, Pattern, SequencePattern, Word},
+ patterns::{All, LongestMatchOf, Pattern, SequencePattern, Word},
};
pub struct Everyday {
@@ -156,7 +156,7 @@ impl Default for Everyday {
// verb, past form: "I coded every day" / "I learned everyday phrases"
Self {
- pattern: Box::new(EitherPattern::new(vec![
+ pattern: Box::new(LongestMatchOf::new(vec![
Box::new(everyday_bad_after),
Box::new(bad_before_every_day),
Box::new(everyday_ambiverb_after_then_noun),
diff --git a/harper-core/src/linting/expand_time_shorthands.rs b/harper-core/src/linting/expand_time_shorthands.rs
index f5262f0d9..638668601 100644
--- a/harper-core/src/linting/expand_time_shorthands.rs
+++ b/harper-core/src/linting/expand_time_shorthands.rs
@@ -3,7 +3,7 @@ use std::sync::Arc;
use super::{Lint, LintKind, PatternLinter};
use crate::Token;
use crate::linting::Suggestion;
-use crate::patterns::{EitherPattern, ImpliesQuantity, Pattern, SequencePattern, WordSet};
+use crate::patterns::{ImpliesQuantity, LongestMatchOf, Pattern, SequencePattern, WordSet};
pub struct ExpandTimeShorthands {
pattern: Box<dyn Pattern>,
@@ -17,7 +17,7 @@ impl ExpandTimeShorthands {
Self {
pattern: Box::new(SequencePattern::default().then(ImpliesQuantity).then(
- EitherPattern::new(vec![
+ LongestMatchOf::new(vec![
Box::new(SequencePattern::default().then(hotwords.clone())),
Box::new(
SequencePattern::default()
diff --git a/harper-core/src/linting/few_units_of_time_ago.rs b/harper-core/src/linting/few_units_of_time_ago.rs
new file mode 100644
index 000000000..b1908044c
--- /dev/null
+++ b/harper-core/src/linting/few_units_of_time_ago.rs
@@ -0,0 +1,504 @@
+use crate::{
+ Lrc, Token,
+ linting::{Lint, PatternLinter, Suggestion},
+ patterns::{FirstMatchOf, Pattern, SequencePattern, TimeUnitPattern},
+};
+
+pub struct FewUnitsOfTimeAgo {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Default for FewUnitsOfTimeAgo {
+ fn default() -> Self {
+ let units = TimeUnitPattern;
+
+ let mut start = FirstMatchOf::default();
+ // start.push(Box::new(
+ // SequencePattern::default()
+ // .then_chunk_start()
+ // .t_ws(),
+ // ));
+ // start.push(Box::new(SequencePattern::default().then_chunk_start()));
+ start.push(Box::new(
+ SequencePattern::default()
+ .then(|tok: &Token, src: &[char]| {
+ if tok.kind.is_word() {
+ let chars = tok.span.get_content(src);
+ chars != ['a'] && chars != ['A']
+ } else {
+ true
+ }
+ })
+ .t_ws(),
+ ));
+
+ let pattern = Lrc::new(
+ SequencePattern::default()
+ .then(start)
+ .t_aco("few")
+ .then_whitespace()
+ .then(units)
+ .then_whitespace()
+ .t_aco("ago"),
+ );
+
+ Self {
+ pattern: Box::new(pattern),
+ }
+ }
+}
+
+impl PatternLinter for FewUnitsOfTimeAgo {
+ fn pattern(&self) -> &dyn crate::patterns::Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let mut span = None;
+
+ for tok in toks.iter().take(3) {
+ if tok.span.get_content_string(src).eq_ignore_ascii_case("few") {
+ span = Some(tok.span);
+ break;
+ }
+ }
+
+ span?;
+
+ Some(Lint {
+ span: span.unwrap(),
+ message: "In this construction you need to use `a few` instead of just `few`."
+ .to_string(),
+ suggestions: vec![Suggestion::replace_with_match_case_str(
+ "a few",
+ span.unwrap().get_content(src),
+ )],
+ ..Default::default()
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Corrects some expressions using `few` where `a few` is correct."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::linting::tests::{
+ assert_lint_count, assert_suggestion_result, assert_top3_suggestion_result,
+ };
+
+ // Basic unit tests
+
+ #[test]
+ #[ignore = "Needs ^ zero-width anchor that matches the start of a chunk"]
+ fn fix_few_minutes_ago() {
+ assert_suggestion_result(
+ "Few minutes ago",
+ FewUnitsOfTimeAgo::default(),
+ "A few minutes ago",
+ );
+ }
+
+ #[test]
+ fn dont_flag_a_few_minutes_ago() {
+ assert_lint_count("A few minutes ago", FewUnitsOfTimeAgo::default(), 0);
+ }
+
+ #[test]
+ fn fix_done_few_minutes_ago() {
+ assert_top3_suggestion_result(
+ "Done few minutes ago",
+ FewUnitsOfTimeAgo::default(),
+ "Done a few minutes ago",
+ );
+ }
+
+ #[test]
+ fn dont_flag_done_a_few_minutes_ago() {
+ assert_lint_count("Done a few minutes ago", FewUnitsOfTimeAgo::default(), 0);
+ }
+
+ #[test]
+ #[ignore = "Needs ^ zero-width anchor that matches the start of a chunk"]
+ fn fix_after_space() {
+ assert_suggestion_result(
+ " Few minutes ago.",
+ FewUnitsOfTimeAgo::default(),
+ " A few minutes ago.",
+ );
+ }
+
+ #[test]
+ #[ignore = "Needs ^ zero-width anchor that matches the start of a chunk"]
+ fn fix_2nd_sentence() {
+ assert_suggestion_result(
+ "Hello World. Few minutes ago I bought your planet.",
+ FewUnitsOfTimeAgo::default(),
+ "Hello World. A few minutes ago I bought your planet.",
+ );
+ }
+
+ // Real world examples from GitHub
+
+ #[test]
+ fn fix_days() {
+ assert_suggestion_result(
+ "My jupyter kernel always says restarting and never ever runs i ran into the problem few days ago before it was fine dont know what happened",
+ FewUnitsOfTimeAgo::default(),
+ "My jupyter kernel always says restarting and never ever runs i ran into the problem a few days ago before it was fine dont know what happened",
+ );
+ }
+
+ #[test]
+ fn fix_decades() {
+ assert_suggestion_result(
+ "This is very old piece of software I wrote few decades ago.",
+ FewUnitsOfTimeAgo::default(),
+ "This is very old piece of software I wrote a few decades ago.",
+ );
+ }
+
+ #[test]
+ fn fix_hours() {
+ assert_suggestion_result(
+ "I just updated my index file few hours ago and there's this error.",
+ FewUnitsOfTimeAgo::default(),
+ "I just updated my index file a few hours ago and there's this error.",
+ );
+ }
+
+ #[test]
+ fn fix_minutes() {
+ assert_suggestion_result(
+ "mysql installed few minutes ago somehow , ubuntu bash thinks its not installed.",
+ FewUnitsOfTimeAgo::default(),
+ "mysql installed a few minutes ago somehow , ubuntu bash thinks its not installed.",
+ );
+ }
+
+ #[test]
+ fn fix_months() {
+ assert_suggestion_result(
+ "Hello, I was working with D455 few months ago, and everything was working fine.",
+ FewUnitsOfTimeAgo::default(),
+ "Hello, I was working with D455 a few months ago, and everything was working fine.",
+ );
+ }
+
+ #[test]
+ fn fix_ms() {
+ assert_suggestion_result(
+ "So I not sure, by getting old signal (get from few ms ago), will it affected my result badly?",
+ FewUnitsOfTimeAgo::default(),
+ "So I not sure, by getting old signal (get from a few ms ago), will it affected my result badly?",
+ );
+ }
+
+ #[test]
+ fn fix_seconds() {
+ assert_suggestion_result(
+ "I have submitted the same issue few seconds ago.",
+ FewUnitsOfTimeAgo::default(),
+ "I have submitted the same issue a few seconds ago.",
+ );
+ }
+
+ #[test]
+ fn fix_weekends() {
+ assert_suggestion_result(
+ "This challenge is a Python jail escape and lucky for me our team had just done one few weekends ago so I was fairly familiar with the tricks to break out.",
+ FewUnitsOfTimeAgo::default(),
+ "This challenge is a Python jail escape and lucky for me our team had just done one a few weekends ago so I was fairly familiar with the tricks to break out.",
+ );
+ }
+
+ #[test]
+ fn fix_weeks() {
+ assert_suggestion_result(
+ "Terraform cloud crashes on plan (same configuration worked few weeks ago)",
+ FewUnitsOfTimeAgo::default(),
+ "Terraform cloud crashes on plan (same configuration worked a few weeks ago)",
+ );
+ }
+
+ #[test]
+ fn fix_years() {
+ assert_suggestion_result(
+ "sandbox-exec was deprecated on MacOS few years ago",
+ FewUnitsOfTimeAgo::default(),
+ "sandbox-exec was deprecated on MacOS a few years ago",
+ );
+ }
+
+ // Real world non-errors from GitHub
+
+ #[test]
+ fn dont_flag_centuries() {
+ assert_lint_count(
+ "Would have been useful a few centuries ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_days() {
+ assert_lint_count(
+ "A few days ago, I upgraded ComfyUI to the latest version, then the prompt node can't upload prompt list text file in Ubuntu",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_decades() {
+ assert_lint_count(
+ "With your QA background you may have heard of the IBM black team of testers back a few decades ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_hours() {
+ assert_lint_count(
+ "It was working well and we could see the installation page a few hours ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_milliseconds() {
+ assert_lint_count(
+ "It is actually the true motor angle observed a few milliseconds ago (pd latency).",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_minutes() {
+ assert_lint_count(
+ "Example from DoD The following was circulated a few minutes ago on an IDESG/NSTIC list",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_moments() {
+ assert_lint_count(
+ "Our microservices started failing a few moments ago when creating new...",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_months() {
+ assert_lint_count(
+ "A few months ago there was an mixed reality project.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_nights() {
+ assert_lint_count(
+ "As an example, a few nights ago I was working on my laptop and stuff that had been working stopped working.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_seconds() {
+ assert_lint_count(
+ "0 - 45 seconds, a few seconds ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_weeks() {
+ assert_lint_count(
+ "It was all working perfectly till a few weeks ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_years() {
+ assert_lint_count(
+ "Hello, I've been an intensive user of your dada2 pipeline until a few years ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ // Real world non-errors from GitHub (but using singular forms)
+
+ #[test]
+ fn dont_flag_decade() {
+ assert_lint_count(
+ "With your QA background you may have heard of the IBM black team of testers back a few decade ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_hour() {
+ assert_lint_count(
+ "It was working well and we could see the installation page a few hour ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_millennia() {
+ assert_lint_count(
+ "A few millennia ago, there was a civilization here",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_minute() {
+ assert_lint_count(
+ "Example from DoD The following was circulated a few minute ago on an IDESG/NSTIC list",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_moment() {
+ assert_lint_count(
+ "No problem should be in the updated version pushed a few moment ago will be live in beta in about 10 min.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_month() {
+ assert_lint_count(
+ "I noticed the same thing a few month ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_second() {
+ assert_lint_count(
+ "Bug it doesnt even answer me rn, like a few second ago he did",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_week() {
+ assert_lint_count(
+ "A few week ago, when logging in the usual way",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_year() {
+ assert_lint_count(
+ "Hello, I've been an intensive user of your dada2 pipeline until a few year ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ // Real world non-errors from GitHub using apostrophes
+
+ #[test]
+ fn dont_flag_days_apos() {
+ assert_lint_count(
+ "And finally it got released a few day's ago.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_months_apos() {
+ assert_lint_count(
+ "I had thought that since I had done this process a few month's ago the database could just be updated.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_weeks_apos() {
+ assert_lint_count(
+ "A few week's ago, I was alerted in Webmin that Webmin was eligible to upgrade to 1.880 which I did through Webmin.",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn dont_flag_years_apos() {
+ assert_lint_count(
+ "A few year's ago a spammer registered an unused base url",
+ FewUnitsOfTimeAgo::default(),
+ 0,
+ );
+ }
+
+ // Real world mistakes from GitHub using singular forms
+
+ #[test]
+ fn fix_day() {
+ assert_suggestion_result(
+ "That worked few day ago with the same setting.",
+ FewUnitsOfTimeAgo::default(),
+ "That worked a few day ago with the same setting.",
+ );
+ }
+
+ #[test]
+ #[ignore = "Needs ^ zero-width anchor that matches the start of a chunk"]
+ fn fix_decade() {
+ assert_suggestion_result(
+ "few decade ago, African Americans weren't allowed to swim in public",
+ FewUnitsOfTimeAgo::default(),
+ "a few decade ago, African Americans weren't allowed to swim in public",
+ );
+ }
+
+ #[test]
+ fn fix_minute() {
+ assert_suggestion_result(
+ "All works fine, but few minute ago the device stop responding from web",
+ FewUnitsOfTimeAgo::default(),
+ "All works fine, but a few minute ago the device stop responding from web",
+ );
+ }
+
+ #[test]
+ fn fix_weekend() {
+ assert_suggestion_result(
+ "I have done this few weekend ago.",
+ FewUnitsOfTimeAgo::default(),
+ "I have done this a few weekend ago.",
+ );
+ }
+}
diff --git a/harper-core/src/linting/hedging.rs b/harper-core/src/linting/hedging.rs
index dd8867137..6283147e4 100644
--- a/harper-core/src/linting/hedging.rs
+++ b/harper-core/src/linting/hedging.rs
@@ -1,5 +1,5 @@
use crate::linting::{Lint, LintKind, PatternLinter};
-use crate::patterns::{EitherPattern, FixedPhrase, Pattern};
+use crate::patterns::{FixedPhrase, LongestMatchOf, Pattern};
use crate::{Token, TokenStringExt};
/// A linter that detects hedging language.
@@ -16,7 +16,7 @@ impl Default for Hedging {
.map(|s| Box::new(FixedPhrase::from_phrase(s)) as Box<dyn Pattern>)
.collect();
- let pattern = Box::new(EitherPattern::new(patterns));
+ let pattern = Box::new(LongestMatchOf::new(patterns));
Self { pattern }
}
}
diff --git a/harper-core/src/linting/hyphenate_number_day.rs b/harper-core/src/linting/hyphenate_number_day.rs
index 7a17331df..47338c4a5 100644
--- a/harper-core/src/linting/hyphenate_number_day.rs
+++ b/harper-core/src/linting/hyphenate_number_day.rs
@@ -1,6 +1,6 @@
use crate::{
Token,
- patterns::{EitherPattern, NominalPhrase, Pattern, SequencePattern},
+ patterns::{LongestMatchOf, NominalPhrase, Pattern, SequencePattern},
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -15,7 +15,7 @@ impl Default for HyphenateNumberDay {
.then_number()
.then_whitespace()
.t_aco("day")
- .then(EitherPattern::new(vec![
+ .then(LongestMatchOf::new(vec![
Box::new(
SequencePattern::default()
.then_whitespace()
diff --git a/harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs b/harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs
index ef4e977d3..bc12fcd33 100644
--- a/harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs
+++ b/harper-core/src/linting/lets_confusion/no_contraction_with_verb.rs
@@ -1,7 +1,7 @@
use crate::{
Token,
linting::{Lint, LintKind, Suggestion},
- patterns::{EitherPattern, Pattern, SequencePattern, WordSet},
+ patterns::{LongestMatchOf, Pattern, SequencePattern, WordSet},
};
use crate::linting::PatternLinter;
@@ -43,7 +43,7 @@ impl Default for NoContractionWithVerb {
tok.kind.is_determiner() || tok.kind.is_pronoun() || tok.kind.is_conjunction()
});
- let let_then_verb = let_ws.then(EitherPattern::new(vec![
+ let let_then_verb = let_ws.then(LongestMatchOf::new(vec![
Box::new(non_ing_verb),
Box::new(verb_due_to_following_pos),
]));
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 1ddd99d89..95ec829ea 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -31,6 +31,7 @@ use super::ellipsis_length::EllipsisLength;
use super::else_possessive::ElsePossessive;
use super::everyday::Everyday;
use super::expand_time_shorthands::ExpandTimeShorthands;
+use super::few_units_of_time_ago::FewUnitsOfTimeAgo;
use super::first_aid_kit::FirstAidKit;
use super::for_noun::ForNoun;
use super::hedging::Hedging;
@@ -360,6 +361,7 @@ impl LintGroup {
insert_struct_rule!(ElsePossessive, true);
insert_struct_rule!(Everyday, true);
insert_pattern_rule!(ExpandTimeShorthands, true);
+ insert_pattern_rule!(FewUnitsOfTimeAgo, true);
insert_struct_rule!(FirstAidKit, true);
insert_struct_rule!(ForNoun, true);
insert_pattern_rule!(Hedging, true);
diff --git a/harper-core/src/linting/map_phrase_linter.rs b/harper-core/src/linting/map_phrase_linter.rs
index 74d122213..3690753c3 100644
--- a/harper-core/src/linting/map_phrase_linter.rs
+++ b/harper-core/src/linting/map_phrase_linter.rs
@@ -1,6 +1,6 @@
use super::{Lint, LintKind, PatternLinter};
use crate::linting::Suggestion;
-use crate::patterns::{EitherPattern, FixedPhrase, Pattern, SimilarToPhrase};
+use crate::patterns::{FixedPhrase, LongestMatchOf, Pattern, SimilarToPhrase};
use crate::{Token, TokenStringExt};
pub struct MapPhraseLinter {
@@ -40,7 +40,7 @@ impl MapPhraseLinter {
message: impl ToString,
description: impl ToString,
) -> Self {
- let patterns = EitherPattern::new(
+ let patterns = LongestMatchOf::new(
phrase
.into_iter()
.map(|p| {
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index ea12d2aed..69c9dc628 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -25,6 +25,7 @@ mod ellipsis_length;
mod else_possessive;
mod everyday;
mod expand_time_shorthands;
+mod few_units_of_time_ago;
mod first_aid_kit;
mod for_noun;
mod hedging;
@@ -114,6 +115,7 @@ pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
pub use everyday::Everyday;
pub use expand_time_shorthands::ExpandTimeShorthands;
+pub use few_units_of_time_ago::FewUnitsOfTimeAgo;
pub use for_noun::ForNoun;
pub use hedging::Hedging;
pub use hereby::Hereby;
diff --git a/harper-core/src/linting/modal_of.rs b/harper-core/src/linting/modal_of.rs
index e5b1291e6..a1f450a21 100644
--- a/harper-core/src/linting/modal_of.rs
+++ b/harper-core/src/linting/modal_of.rs
@@ -1,6 +1,6 @@
use crate::{
Lrc, Token, TokenStringExt,
- patterns::{EitherPattern, Pattern, SequencePattern, WordSet},
+ patterns::{LongestMatchOf, Pattern, SequencePattern, WordSet},
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -52,7 +52,7 @@ impl Default for ModalOf {
);
Self {
- pattern: Box::new(EitherPattern::new(vec![
+ pattern: Box::new(LongestMatchOf::new(vec![
Box::new(anyword_might_of_course),
Box::new(modal_of_course),
Box::new(anyword_might_of),
diff --git a/harper-core/src/linting/noun_instead_of_verb.rs b/harper-core/src/linting/noun_instead_of_verb.rs
index b7e6f03f7..c938f350a 100644
--- a/harper-core/src/linting/noun_instead_of_verb.rs
+++ b/harper-core/src/linting/noun_instead_of_verb.rs
@@ -1,6 +1,6 @@
use crate::{
Lrc, Token, TokenStringExt,
- patterns::{EitherPattern, Pattern, SequencePattern, WordSet},
+ patterns::{LongestMatchOf, Pattern, SequencePattern, WordSet},
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -55,7 +55,7 @@ pub struct NounInsteadOfVerb {
impl Default for NounInsteadOfVerb {
fn default() -> Self {
- let pre_context = EitherPattern::new(vec![
+ let pre_context = LongestMatchOf::new(vec![
Box::new(WordSet::new(PRONOUNS)),
Box::new(WordSet::new(MODAL_VERBS_ETC)),
Box::new(WordSet::new(ADVERBS)),
@@ -85,7 +85,7 @@ impl Default for NounInsteadOfVerb {
.then_any_word();
Self {
- pattern: Box::new(EitherPattern::new(vec![
+ pattern: Box::new(LongestMatchOf::new(vec![
Box::new(pattern_followed_by_punctuation),
Box::new(pattern_followed_by_word),
Box::new(basic_pattern),
diff --git a/harper-core/src/linting/one_and_the_same.rs b/harper-core/src/linting/one_and_the_same.rs
index a1061929d..6488500aa 100644
--- a/harper-core/src/linting/one_and_the_same.rs
+++ b/harper-core/src/linting/one_and_the_same.rs
@@ -1,6 +1,6 @@
use crate::{
Lrc, Token, TokenStringExt,
- patterns::{EitherPattern, FixedPhrase, Pattern, SequencePattern, WordSet},
+ patterns::{FixedPhrase, LongestMatchOf, Pattern, SequencePattern, WordSet},
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -14,7 +14,7 @@ impl Default for OneAndTheSame {
let one_in_the_same = Lrc::new(FixedPhrase::from_phrase("one in the same"));
Self {
- pattern: Box::new(EitherPattern::new(vec![
+ pattern: Box::new(LongestMatchOf::new(vec![
Box::new(
SequencePattern::default()
.then(WordSet::new(&["are", "were"]))
diff --git a/harper-core/src/linting/open_compounds.rs b/harper-core/src/linting/open_compounds.rs
index 96c30278c..174059ebc 100644
--- a/harper-core/src/linting/open_compounds.rs
+++ b/harper-core/src/linting/open_compounds.rs
@@ -1,6 +1,6 @@
use crate::{
Lrc, Token,
- patterns::{EitherPattern, Pattern, SequencePattern, WordSet},
+ patterns::{LongestMatchOf, Pattern, SequencePattern, WordSet},
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -53,7 +53,7 @@ impl Default for OpenCompounds {
.then_anything();
Self {
- pattern: Box::new(EitherPattern::new(vec![
+ pattern: Box::new(LongestMatchOf::new(vec![
Box::new(with_prev_and_next),
Box::new(with_prev),
Box::new(with_next),
diff --git a/harper-core/src/linting/open_the_light.rs b/harper-core/src/linting/open_the_light.rs
index 6fd829087..fb9d8c984 100644
--- a/harper-core/src/linting/open_the_light.rs
+++ b/harper-core/src/linting/open_the_light.rs
@@ -1,7 +1,7 @@
use crate::{
Lrc, Token, TokenStringExt,
linting::{LintKind, Suggestion},
- patterns::{EitherPattern, Pattern, SequencePattern, WordSet},
+ patterns::{LongestMatchOf, Pattern, SequencePattern, WordSet},
};
use super::{Lint, PatternLinter};
@@ -45,7 +45,7 @@ impl Default for OpenTheLight {
.t_ws()
.then_noun();
- let pattern = Box::new(EitherPattern::new(vec![
+ let pattern = Box::new(LongestMatchOf::new(vec![
Box::new(open_the_device),
Box::new(open_the_device_then_noun),
]));
diff --git a/harper-core/src/linting/out_of_date.rs b/harper-core/src/linting/out_of_date.rs
index 2cbc412cd..b5e5c7d23 100644
--- a/harper-core/src/linting/out_of_date.rs
+++ b/harper-core/src/linting/out_of_date.rs
@@ -1,6 +1,6 @@
use crate::{
Token, TokenStringExt,
- patterns::{EitherPattern, FixedPhrase, Pattern},
+ patterns::{FixedPhrase, LongestMatchOf, Pattern},
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -11,7 +11,7 @@ pub struct OutOfDate {
impl Default for OutOfDate {
fn default() -> Self {
- let pattern = EitherPattern::new(vec![
+ let pattern = LongestMatchOf::new(vec![
Box::new(FixedPhrase::from_phrase("out of date")),
Box::new(FixedPhrase::from_phrase("out-of date")),
Box::new(FixedPhrase::from_phrase("out of-date")),
diff --git a/harper-core/src/linting/oxymorons.rs b/harper-core/src/linting/oxymorons.rs
index 46dc8f130..9a5e71c42 100644
--- a/harper-core/src/linting/oxymorons.rs
+++ b/harper-core/src/linting/oxymorons.rs
@@ -1,5 +1,5 @@
use crate::linting::{Lint, LintKind, PatternLinter};
-use crate::patterns::{EitherPattern, FixedPhrase, Pattern};
+use crate::patterns::{FixedPhrase, LongestMatchOf, Pattern};
use crate::{Token, TokenStringExt};
/// A linter that flags oxymoronic phrases.
@@ -36,7 +36,7 @@ impl Oxymorons {
.map(|s| Box::new(FixedPhrase::from_phrase(s)) as Box<dyn Pattern>)
.collect();
- let pattern = Box::new(EitherPattern::new(patterns));
+ let pattern = Box::new(LongestMatchOf::new(patterns));
Self { pattern }
}
}
diff --git a/harper-core/src/linting/pronoun_knew.rs b/harper-core/src/linting/pronoun_knew.rs
index dc2f81049..34109d82a 100644
--- a/harper-core/src/linting/pronoun_knew.rs
+++ b/harper-core/src/linting/pronoun_knew.rs
@@ -1,7 +1,7 @@
use crate::{
Token,
linting::{Lint, LintKind, PatternLinter, Suggestion},
- patterns::{EitherPattern, SequencePattern, WordSet},
+ patterns::{LongestMatchOf, SequencePattern, WordSet},
};
pub struct PronounKnew {
@@ -39,7 +39,7 @@ impl Default for PronounKnew {
.then_whitespace()
.then_any_capitalization_of("new");
- let combined_pattern = EitherPattern::new(vec![
+ let combined_pattern = LongestMatchOf::new(vec![
Box::new(pronoun_then_new),
Box::new(pronoun_adverb_then_new),
]);
diff --git a/harper-core/src/linting/since_duration.rs b/harper-core/src/linting/since_duration.rs
index f7e8f7bca..6c55d62fb 100644
--- a/harper-core/src/linting/since_duration.rs
+++ b/harper-core/src/linting/since_duration.rs
@@ -1,6 +1,6 @@
use crate::{
Lrc, Token, TokenStringExt,
- patterns::{EitherPattern, Pattern, SequencePattern, SpelledNumberPattern, WordSet},
+ patterns::{LongestMatchOf, Pattern, SequencePattern, SpelledNumberPattern, WordSet},
};
use super::{Lint, LintKind, PatternLinter, Suggestion};
@@ -35,7 +35,7 @@ impl Default for SinceDuration {
SequencePattern::default()
.then_any_capitalization_of("since")
.then_whitespace()
- .then(EitherPattern::new(vec![
+ .then(LongestMatchOf::new(vec![
Box::new(SpelledNumberPattern),
Box::new(SequencePattern::default().then_number()),
]))
@@ -49,7 +49,7 @@ impl Default for SinceDuration {
.then_any_capitalization_of("ago");
Self {
- pattern: Box::new(EitherPattern::new(vec![
+ pattern: Box::new(LongestMatchOf::new(vec![
Box::new(pattern_without_ago),
Box::new(pattern_with_ago),
])),
diff --git a/harper-core/src/linting/the_how_why.rs b/harper-core/src/linting/the_how_why.rs
index dc558aa76..03839d7e9 100644
--- a/harper-core/src/linting/the_how_why.rs
+++ b/harper-core/src/linting/the_how_why.rs
@@ -1,13 +1,13 @@
use crate::{
Token, TokenStringExt,
linting::{Lint, LintKind, PatternLinter, Suggestion},
- patterns::{EitherPattern, Invert, Pattern, SequencePattern},
+ patterns::{Invert, LongestMatchOf, Pattern, SequencePattern},
};
/// Suggests removing `the` when followed by how/why/who/when/what,
/// skipping cases like `how to` and `who's who`.
pub struct TheHowWhy {
- pattern: EitherPattern,
+ pattern: LongestMatchOf,
}
impl Default for TheHowWhy {
@@ -47,7 +47,7 @@ impl Default for TheHowWhy {
.then_whitespace()
.t_aco("what");
- let pattern = EitherPattern::new(vec![
+ let pattern = LongestMatchOf::new(vec![
Box::new(the_how),
Box::new(the_who),
Box::new(the_why),
diff --git a/harper-core/src/linting/the_my.rs b/harper-core/src/linting/the_my.rs
index f1253e746..6788b5a4e 100644
--- a/harper-core/src/linting/the_my.rs
+++ b/harper-core/src/linting/the_my.rs
@@ -1,7 +1,7 @@
use super::{Lint, LintKind, PatternLinter, Suggestion};
use crate::{
CharStringExt, Token, TokenStringExt,
- patterns::{EitherPattern, Pattern, SequencePattern, Word, WordSet},
+ patterns::{LongestMatchOf, Pattern, SequencePattern, Word, WordSet},
};
pub struct TheMy {
@@ -24,7 +24,7 @@ impl Default for TheMy {
.then(the);
Self {
- pattern: Box::new(EitherPattern::new(vec![
+ pattern: Box::new(LongestMatchOf::new(vec![
Box::new(the_poss),
Box::new(poss_the),
])),
diff --git a/harper-core/src/linting/then_than.rs b/harper-core/src/linting/then_than.rs
index d831890bd..5859f4007 100644
--- a/harper-core/src/linting/then_than.rs
+++ b/harper-core/src/linting/then_than.rs
@@ -2,7 +2,7 @@ use super::{Lint, LintKind, PatternLinter};
use crate::Token;
use crate::linting::Suggestion;
use crate::patterns::{
- All, EitherPattern, Invert, OwnedPatternExt, Pattern, SequencePattern, Word, WordSet,
+ All, Invert, LongestMatchOf, OwnedPatternExt, Pattern, SequencePattern, Word, WordSet,
};
#[doc = "Corrects the misuse of `then` to `than`."]
@@ -14,7 +14,7 @@ impl ThenThan {
pub fn new() -> Self {
Self {
pattern: Box::new(All::new(vec![
- Box::new(EitherPattern::new(vec![
+ Box::new(LongestMatchOf::new(vec![
// Comparative form of adjective
Box::new(
SequencePattern::default()
diff --git a/harper-core/src/linting/use_genitive.rs b/harper-core/src/linting/use_genitive.rs
index 7ae300a3d..fc5d82184 100644
--- a/harper-core/src/linting/use_genitive.rs
+++ b/harper-core/src/linting/use_genitive.rs
@@ -1,5 +1,5 @@
use crate::linting::{LintKind, PatternLinter, Suggestion};
-use crate::patterns::{EitherPattern, Invert, Pattern, SequencePattern, WordPatternGroup};
+use crate::patterns::{Invert, LongestMatchOf, Pattern, SequencePattern, WordPatternGroup};
use crate::{Lint, Lrc, Token};
// Looks for places where the genitive case _isn't_ being used, and should be.
@@ -11,7 +11,7 @@ impl UseGenitive {
fn new() -> Self {
// Define the environment in which the genitive case __should__ be used.
let environment = Lrc::new(SequencePattern::default().then_whitespace().then(
- EitherPattern::new(vec![
+ LongestMatchOf::new(vec![
Box::new(
SequencePattern::default()
.then_one_or_more_adjectives()
@@ -39,7 +39,7 @@ impl UseGenitive {
// Add a prelude to remove false-positives.
let full_pattern = SequencePattern::default()
- .then(Invert::new(EitherPattern::new(vec![
+ .then(Invert::new(LongestMatchOf::new(vec![
Box::new(SequencePattern::default().t_aco("is")),
Box::new(SequencePattern::default().t_aco("were")),
Box::new(SequencePattern::default().then_adjective()),
diff --git a/harper-core/src/patterns/naive_pattern_group.rs b/harper-core/src/patterns/first_match_of.rs
similarity index 75%
rename from harper-core/src/patterns/naive_pattern_group.rs
rename to harper-core/src/patterns/first_match_of.rs
index 4520834bb..5f5fdea07 100644
--- a/harper-core/src/patterns/naive_pattern_group.rs
+++ b/harper-core/src/patterns/first_match_of.rs
@@ -3,18 +3,20 @@ use crate::Token;
/// A naive pattern collection that naively iterates through a list of patterns,
/// returning the first one that matches.
+///
+/// Compare to [`LongestMatchOf`](super::LongestMatchOf), which returns the longest match.
#[derive(Default)]
-pub struct NaivePatternGroup {
+pub struct FirstMatchOf {
patterns: Vec<Box<dyn Pattern>>,
}
-impl NaivePatternGroup {
+impl FirstMatchOf {
pub fn push(&mut self, pattern: Box<dyn Pattern>) {
self.patterns.push(pattern);
}
}
-impl Pattern for NaivePatternGroup {
+impl Pattern for FirstMatchOf {
fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
self.patterns
.iter()
diff --git a/harper-core/src/patterns/either_pattern.rs b/harper-core/src/patterns/longest_match_of.rs
similarity index 91%
rename from harper-core/src/patterns/either_pattern.rs
rename to harper-core/src/patterns/longest_match_of.rs
index d4ad6ec02..d5fc7896e 100644
--- a/harper-core/src/patterns/either_pattern.rs
+++ b/harper-core/src/patterns/longest_match_of.rs
@@ -4,11 +4,11 @@ use super::Pattern;
/// A pattern that returns the value of the longest match in a list.
#[derive(Default)]
-pub struct EitherPattern {
+pub struct LongestMatchOf {
patterns: Vec<Box<dyn Pattern>>,
}
-impl EitherPattern {
+impl LongestMatchOf {
pub fn new(patterns: Vec<Box<dyn Pattern>>) -> Self {
Self { patterns }
}
@@ -18,7 +18,7 @@ impl EitherPattern {
}
}
-impl Pattern for EitherPattern {
+impl Pattern for LongestMatchOf {
fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
let mut longest = None;
diff --git a/harper-core/src/patterns/mergeable_words.rs b/harper-core/src/patterns/mergeable_words.rs
index 9171c6bbb..cb91f5e90 100644
--- a/harper-core/src/patterns/mergeable_words.rs
+++ b/harper-core/src/patterns/mergeable_words.rs
@@ -2,7 +2,7 @@ use std::sync::Arc;
use crate::{CharString, Dictionary, FstDictionary, Token, WordMetadata};
-use super::{EitherPattern, Pattern, SequencePattern, WhitespacePattern};
+use super::{LongestMatchOf, Pattern, SequencePattern, WhitespacePattern};
type PredicateFn = dyn Fn(Option<&WordMetadata>, Option<&WordMetadata>) -> bool + Send + Sync;
@@ -23,7 +23,7 @@ impl MergeableWords {
Self {
inner: SequencePattern::default()
.then_any_word()
- .then(EitherPattern::new(vec![
+ .then(LongestMatchOf::new(vec![
Box::new(WhitespacePattern),
Box::new(|tok: &Token, _source: &[char]| tok.kind.is_hyphen()),
]))
diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rs
index fcdf69756..9183a286a 100644
--- a/harper-core/src/patterns/mod.rs
+++ b/harper-core/src/patterns/mod.rs
@@ -9,20 +9,21 @@ use crate::{Document, LSend, Span, Token};
mod all;
mod any_pattern;
-mod either_pattern;
+mod first_match_of;
mod fixed_phrase;
mod implies_quantity;
mod indefinite_article;
mod inflection_of_be;
mod invert;
+mod longest_match_of;
mod mergeable_words;
-mod naive_pattern_group;
mod nominal_phrase;
mod pattern_map;
mod repeating_pattern;
mod sequence_pattern;
mod similar_to_phrase;
mod spelled_number_pattern;
+mod time_unit_pattern;
mod whitespace_pattern;
mod within_edit_distance;
mod word;
@@ -32,20 +33,21 @@ mod word_set;
pub use all::All;
pub use any_pattern::AnyPattern;
use blanket::blanket;
-pub use either_pattern::EitherPattern;
+pub use first_match_of::FirstMatchOf;
pub use fixed_phrase::FixedPhrase;
pub use implies_quantity::ImpliesQuantity;
pub use indefinite_article::IndefiniteArticle;
pub use inflection_of_be::InflectionOfBe;
pub use invert::Invert;
+pub use longest_match_of::LongestMatchOf;
pub use mergeable_words::MergeableWords;
-pub use naive_pattern_group::NaivePatternGroup;
pub use nominal_phrase::NominalPhrase;
pub use pattern_map::PatternMap;
pub use repeating_pattern::RepeatingPattern;
pub use sequence_pattern::SequencePattern;
pub use similar_to_phrase::SimilarToPhrase;
pub use spelled_number_pattern::SpelledNumberPattern;
+pub use time_unit_pattern::TimeUnitPattern;
pub use whitespace_pattern::WhitespacePattern;
pub use word::Word;
pub use word_pattern_group::WordPatternGroup;
@@ -122,15 +124,15 @@ where
}
pub trait OwnedPatternExt {
- fn or(self, other: impl Pattern + 'static) -> EitherPattern;
+ fn or(self, other: impl Pattern + 'static) -> LongestMatchOf;
}
impl<P> OwnedPatternExt for P
where
P: Pattern + 'static,
{
- fn or(self, other: impl Pattern + 'static) -> EitherPattern {
- EitherPattern::new(vec![Box::new(self), Box::new(other)])
+ fn or(self, other: impl Pattern + 'static) -> LongestMatchOf {
+ LongestMatchOf::new(vec![Box::new(self), Box::new(other)])
}
}
diff --git a/harper-core/src/patterns/spelled_number_pattern.rs b/harper-core/src/patterns/spelled_number_pattern.rs
index 89117ee2c..aa2861df3 100644
--- a/harper-core/src/patterns/spelled_number_pattern.rs
+++ b/harper-core/src/patterns/spelled_number_pattern.rs
@@ -1,4 +1,4 @@
-use super::{EitherPattern, Pattern, SequencePattern, WhitespacePattern, WordSet};
+use super::{LongestMatchOf, Pattern, SequencePattern, WhitespacePattern, WordSet};
use crate::Token;
/// Matches spelled-out numbers from one to ninety-nine
@@ -50,13 +50,13 @@ impl Pattern for SpelledNumberPattern {
let tens_units_compounds = SequencePattern::default()
.then(WordSet::new(tens))
- .then(EitherPattern::new(vec![
+ .then(LongestMatchOf::new(vec![
Box::new(|t: &Token, _s: &[char]| t.kind.is_hyphen()),
Box::new(WhitespacePattern),
]))
.then(WordSet::new(units));
- let pat = EitherPattern::new(vec![Box::new(single_words), Box::new(tens_units_compounds)]);
+ let pat = LongestMatchOf::new(vec![Box::new(single_words), Box::new(tens_units_compounds)]);
pat.matches(tokens, source)
}
diff --git a/harper-core/src/patterns/time_unit_pattern.rs b/harper-core/src/patterns/time_unit_pattern.rs
new file mode 100644
index 000000000..aaeb2c62d
--- /dev/null
+++ b/harper-core/src/patterns/time_unit_pattern.rs
@@ -0,0 +1,79 @@
+use crate::Token;
+
+use super::{LongestMatchOf, Pattern, WordSet};
+
+/// Matches a time unit.
+///
+/// Matches standard units from microsecond to decade.
+/// Matches other 'units' such as moment, night, weekend.
+/// Matches singular and plural forms.
+/// Matches possessive forms (which are also common misspellings for the plurals).
+/// Matches abbreviations.
+#[derive(Default)]
+pub struct TimeUnitPattern;
+
+impl Pattern for TimeUnitPattern {
+ fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
+ if tokens.is_empty() {
+ return None;
+ }
+
+ let units_definite_singular = WordSet::new(&[
+ "microsecond",
+ "millisecond",
+ "second",
+ "minute",
+ "hour",
+ "day",
+ "week",
+ "month",
+ "year",
+ "decade",
+ ]);
+
+ let units_definite_plural = WordSet::new(&[
+ "microseconds",
+ "milliseconds",
+ "seconds",
+ "minutes",
+ "hours",
+ "days",
+ "weeks",
+ "months",
+ "years",
+ "decades",
+ ]);
+
+ let units_definite_apos = WordSet::new(&[
+ "microsecond's",
+ "millisecond's",
+ "second's",
+ "minute's",
+ "hour's",
+ "day's",
+ "week's",
+ "month's",
+ "year's",
+ "decade's",
+ ]);
+
+ // ms
+ let units_definite_abbrev = WordSet::new(&["ms"]);
+
+ let units_other_singular = WordSet::new(&["moment", "night", "weekend"]);
+ let units_other_plural = WordSet::new(&["moments", "nights", "weekends"]);
+ let units_other_apos = WordSet::new(&["moment's", "night's", "weekend's"]);
+
+ let units = LongestMatchOf::new(vec![
+ Box::new(units_definite_singular),
+ Box::new(units_definite_plural),
+ Box::new(units_other_singular),
+ Box::new(units_other_plural),
+ Box::new(units_definite_abbrev),
+ Box::new(units_definite_apos),
+ Box::new(units_other_apos),
+ ]);
+
+ units.matches(tokens, source)
+ }
+}
diff --git a/harper-core/src/patterns/word_pattern_group.rs b/harper-core/src/patterns/word_pattern_group.rs
index 5028b592e..54c4aa7c5 100644
--- a/harper-core/src/patterns/word_pattern_group.rs
+++ b/harper-core/src/patterns/word_pattern_group.rs
@@ -1,6 +1,6 @@
use hashbrown::HashMap;
-use super::naive_pattern_group::NaivePatternGroup;
+use super::first_match_of::FirstMatchOf;
use super::{Pattern, SequencePattern};
use crate::CharString;
@@ -14,14 +14,14 @@ where
patterns: HashMap<CharString, P>,
}
-impl WordPatternGroup<NaivePatternGroup> {
+impl WordPatternGroup<FirstMatchOf> {
pub fn add(&mut self, word: &str, pat: Box<dyn Pattern>) {
let chars = word.chars().collect();
if let Some(group) = self.patterns.get_mut(&chars) {
group.push(pat);
} else {
- let mut group = NaivePatternGroup::default();
+ let mut group = FirstMatchOf::default();
group.push(pat);
self.patterns.insert(chars, group);
}
diff --git a/packages/obsidian-plugin/data.json b/packages/obsidian-plugin/data.json
new file mode 100644
index 000000000..f8fc3a3b6
--- /dev/null
+++ b/packages/obsidian-plugin/data.json
@@ -0,0 +1,368 @@
+{
+ "ignoredLints": "{\"context_hashes\":[11327540533206285101]}",
+ "useWebWorker": true,
+ "lintSettings": {
+ "ACoupleMore": null,
+ "ALongTime": null,
+ "ALotWorst": null,
+ "APart": null,
+ "AWholeEntire": null,
+ "AdjectiveOfA": null,
+ "AfterAWhile": null,
+ "AlzheimersDisease": null,
+ "AmazonNames": null,
+ "Americas": null,
+ "AmountsFor": null,
+ "AnA": null,
+ "AnAnother": null,
+ "AndIn": null,
+ "AndTheLike": null,
+ "AnotherAn": null,
+ "AnotherOnes": null,
+ "AnotherThings": null,
+ "Anybody": null,
+ "Anyhow": null,
+ "Anyone": null,
+ "Anywhere": null,
+ "AppleNames": null,
+ "AsFarBackAs": null,
+ "AsOfLate": null,
+ "AsWell": null,
+ "AskNoPreposition": null,
+ "AtFaceValue": null,
+ "Australia": null,
+ "AvoidAndAlso": null,
+ "AvoidCurses": null,
+ "AzureNames": null,
+ "BackInTheDay": null,
+ "Backplane": null,
+ "BadRap": null,
+ "BaitedBreath": null,
+ "BanTogether": null,
+ "BareInMind": null,
+ "BatedBreath": null,
+ "BeckAndCall": null,
+ "BeenThere": null,
+ "BestRegards": null,
+ "BlanketStatement": null,
+ "BoringWords": null,
+ "Brutality": null,
+ "ByAccident": null,
+ "CanBeSeen": null,
+ "Canada": null,
+ "CapitalizePersonalPronouns": null,
+ "CaseInPoint": null,
+ "CaseSensitive": null,
+ "ChangeOfTack": null,
+ "ChangeTack": null,
+ "ChangedTack": null,
+ "ChangesOfTack": null,
+ "ChangesTack": null,
+ "ChangingOfTack": null,
+ "ChangingTack": null,
+ "ChineseCommunistParty": null,
+ "ChockFull": null,
+ "ClientSide": null,
+ "CommaFixes": null,
+ "CompaniesProductsAndTrademarks": null,
+ "CompoundNouns": null,
+ "CondenseAllThe": null,
+ "Confident": null,
+ "CorrectNumberSuffix": null,
+ "Countries": null,
+ "CoursingThroughVeins": null,
+ "CurrencyPlacement": null,
+ "DampSquib": null,
+ "Dashes": null,
+ "DayAndAge": null,
+ "DayOneNames": null,
+ "DefiniteArticle": null,
+ "DefiniteArticles": null,
+ "Desktop": null,
+ "DespiteOf": null,
+ "Devops": null,
+ "Discuss": null,
+ "Discussed": null,
+ "Discusses": null,
+ "Discussing": null,
+ "DoNotWant": null,
+ "DotInitialisms": null,
+ "EachAndEveryOne": null,
+ "EllipsisLength": null,
+ "ElsePossessive": null,
+ "EludedTo": null,
+ "EnMasse": null,
+ "EverPresent": null,
+ "Everybody": null,
+ "Everyday": null,
+ "Everyone": null,
+ "Everywhere": null,
+ "Excellent": null,
+ "ExpandBecause": null,
+ "ExpandDependencies": null,
+ "ExpandDependency": null,
+ "ExpandMinimum": null,
+ "ExpandStandardInput": null,
+ "ExpandStandardOutput": null,
+ "ExpandTimeShorthands": null,
+ "ExpandWith": null,
+ "ExpandWithout": null,
+ "Expatriate": null,
+ "ExplanationMark": null,
+ "ExplanationMarks": null,
+ "ExplanationPoint": null,
+ "FaceFirst": null,
+ "FairBit": null,
+ "FarWorse": null,
+ "FastPaste": null,
+ "FatalOutcome": null,
+ "FetalPosition": null,
+ "FirstAidKit": null,
+ "ForALongTime": null,
+ "ForAWhile": null,
+ "ForAllIntentsAndPurposes": null,
+ "ForNoun": null,
+ "FreeRein": null,
+ "Freezing": null,
+ "FurtherAdo": null,
+ "Furthermore": null,
+ "GetRidOff": null,
+ "GetsRidOff": null,
+ "GettingRidOff": null,
+ "GildedAge": null,
+ "GoingTo": null,
+ "GoogleNames": null,
+ "GotRidOff": null,
+ "GottenRidOff": null,
+ "GuineaBissau": null,
+ "HadGone": null,
+ "HadOf": null,
+ "HadPassed": null,
+ "HalfAnHour": null,
+ "Haphazard": null,
+ "HasGone": null,
+ "HasPassed": null,
+ "HaveGone": null,
+ "HavePassed": null,
+ "HavingGone": null,
+ "HavingPassed": null,
+ "Hedging": null,
+ "Henceforth": null,
+ "Hereby": null,
+ "Holidays": null,
+ "HomeInOn": null,
+ "HomedInOn": null,
+ "HomesInOn": null,
+ "HomingInOn": null,
+ "HopHope": null,
+ "HowTo": null,
+ "However": null,
+ "HumanBeings": null,
+ "HumanLife": null,
+ "HungerPang": null,
+ "HyphenateNumberDay": null,
+ "IAm": null,
+ "InAWhile": null,
+ "InAndOfItself": null,
+ "InAnyWay": null,
+ "InCase": null,
+ "InDetail": null,
+ "InMoreDetail": null,
+ "InNeedOf": null,
+ "InOneFellSwoop": null,
+ "InThe": null,
+ "InflectedVerbAfterTo": null,
+ "Insofar": null,
+ "Instead": null,
+ "InsteadOf": null,
+ "Insurmountable": null,
+ "Intact": null,
+ "Into": null,
+ "InvestIn": null,
+ "InvestedIn": null,
+ "InvestingIn": null,
+ "InvestsIn": null,
+ "IsKnownFor": null,
+ "ItCan": null,
+ "ItsContraction": null,
+ "Itself": null,
+ "IveGotTo": null,
+ "JawDropping": null,
+ "JetpackNames": null,
+ "JustDeserts": null,
+ "KindOf": null,
+ "KindRegards": null,
+ "Koreas": null,
+ "Laptop": null,
+ "LastButNotLeast": null,
+ "LastDitch": null,
+ "LeftRightHand": null,
+ "LetAlone": null,
+ "LetsConfusion": null,
+ "LikeThePlague": null,
+ "Likewise": null,
+ "LinkingVerbs": null,
+ "LongSentences": null,
+ "Malaysia": null,
+ "MergeWords": null,
+ "MetaNames": null,
+ "MicrosoftNames": null,
+ "Middleware": null,
+ "Misunderstand": null,
+ "Misunderstood": null,
+ "Misuse": null,
+ "Misused": null,
+ "ModalOf": null,
+ "Monumentous": null,
+ "MostNumber": null,
+ "MuchAdo": null,
+ "MuchWorse": null,
+ "Multicore": null,
+ "Multimedia": null,
+ "MultipleSequentialPronouns": null,
+ "Multithreading": null,
+ "MutePoint": null,
+ "MyHouse": null,
+ "Myself": null,
+ "NailOnTheHead": null,
+ "NationalCapitals": null,
+ "NeedHelp": null,
+ "NerveRacking": null,
+ "NoOxfordComma": null,
+ "Nobody": null,
+ "NominalWants": null,
+ "Nonetheless": null,
+ "NotIn": null,
+ "NotTo": null,
+ "NotablePlaces": null,
+ "Nothing": null,
+ "Notwithstanding": null,
+ "NounInsteadOfVerb": null,
+ "Nowhere": null,
+ "NumberSuffixCapitalization": null,
+ "OceansAndSeas": null,
+ "OfCourse": null,
+ "OffTheCuff": null,
+ "OldWivesTale": null,
+ "OnSecondThought": null,
+ "OnTheSpurOfTheMoment": null,
+ "OnceInAWhile": null,
+ "OneAndTheSame": null,
+ "OpenCompounds": null,
+ "OpenTheLight": null,
+ "OperativeSystem": null,
+ "OperativeSystems": null,
+ "OutOfDate": null,
+ "Overall": null,
+ "Overclocking": null,
+ "Overload": null,
+ "Overnight": null,
+ "OxfordComma": null,
+ "Oxymorons": null,
+ "PeaceOfMind": null,
+ "PhrasalVerbAsCompoundNoun": null,
+ "PiggyBag": null,
+ "PiggyBagged": null,
+ "PiggyBagging": null,
+ "PiqueInterest": null,
+ "PocketCastsNames": null,
+ "PointIsMoot": null,
+ "PointsOfView": null,
+ "PortAuPrince": null,
+ "PortoNovo": null,
+ "PossessiveYour": null,
+ "Postpone": null,
+ "PrayingMantis": null,
+ "PronounContraction": null,
+ "PronounKnew": null,
+ "Proofread": null,
+ "ProperNouns": null,
+ "RapidFire": null,
+ "RealTrouper": null,
+ "Regardless": null,
+ "RepeatedWords": null,
+ "RifeWith": null,
+ "RoadMap": null,
+ "SameAs": null,
+ "SaveToSafe": null,
+ "ScantilyClad": null,
+ "SentenceCapitalization": null,
+ "ServerSide": null,
+ "SimpleGrammatical": null,
+ "SinceDuration": null,
+ "SneakingSuspicion": null,
+ "Somebody": null,
+ "Somehow": null,
+ "Someone": null,
+ "SomewhatSomething": null,
+ "Somewhere": null,
+ "SoonerOrLater": null,
+ "Spaces": null,
+ "SpecialAttention": null,
+ "SpellCheck": null,
+ "SpelledNumbers": null,
+ "SpokeTooSoon": null,
+ "Starving": null,
+ "StateOfTheArt": null,
+ "SufficeItToSay": null,
+ "SupposedTo": null,
+ "TakeItPersonally": null,
+ "TakeItSeriously": null,
+ "ThatChallenged": null,
+ "ThatThis": null,
+ "ThatWhich": null,
+ "TheAnother": null,
+ "TheHowWhy": null,
+ "TheMy": null,
+ "ThenThan": null,
+ "ThereIsAny": null,
+ "Therefore": null,
+ "Thereupon": null,
+ "ThoughtProcess": null,
+ "ThrowRubbish": null,
+ "TickingTimeClock": null,
+ "ToDoHyphen": null,
+ "ToTheMannerBorn": null,
+ "Towards": null,
+ "TrialAndError": null,
+ "TumblrNames": null,
+ "TurnForTheWorse": null,
+ "TurnItOff": null,
+ "USUniversities": null,
+ "UnclosedQuotes": null,
+ "Underclock": null,
+ "UnitedOrganizations": null,
+ "Unless": null,
+ "Upset": null,
+ "Upward": null,
+ "UseGenitive": null,
+ "WantBe": null,
+ "WasAloud": null,
+ "WaveFunction": null,
+ "WellBeing": null,
+ "WellKept": null,
+ "WhatHeLooksLike": null,
+ "WhatItLooksLike": null,
+ "WhatSheLooksLike": null,
+ "WhatTheyLookLike": null,
+ "Whereas": null,
+ "Whereupon": null,
+ "WhetYourAppetite": null,
+ "WholeEntire": null,
+ "WidelyAccepted": null,
+ "Widespread": null,
+ "WillContain": null,
+ "WinPrize": null,
+ "WordPressDotcom": null,
+ "WorldWarII": null,
+ "Worldwide": null,
+ "WorseAndWorse": null,
+ "WorseCaseScenario": null,
+ "WorseThan": null,
+ "WorstCaseScenario": null,
+ "WorstEver": null
+ },
+ "userDictionary": [],
+ "dialect": 0,
+ "delay": -1
+}