Skip to content

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

refactor(core): reduce boilerplate

Elijah Potter5c2d2c15e43d
diff --git a/harper-comments/tests/language_support.rs b/harper-comments/tests/language_support.rs
index 11546ad29..fc2d48b88 100644
--- a/harper-comments/tests/language_support.rs
+++ b/harper-comments/tests/language_support.rs
@@ -1,7 +1,7 @@
use std::path::Path;
use harper_comments::CommentParser;
-use harper_core::linting::{LintGroup, LintGroupConfig, Linter};
+use harper_core::linting::{LintGroup, Linter};
use harper_core::parsers::MarkdownOptions;
use harper_core::{Document, FstDictionary};
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index 5661bdb62..82e0b4cd1 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -347,7 +347,7 @@ impl Document {
Lrc::new(EitherPattern::new(vec![
Box::new(
SequencePattern::default()
- .then_word_set(WordSet::all(&["etc", "vs"]))
+ .then(WordSet::new(&["etc", "vs"]))
.then_period(),
),
Box::new(
diff --git a/harper-core/src/linting/back_in_the_day.rs b/harper-core/src/linting/back_in_the_day.rs
index d2f5d64bb..645333d71 100644
--- a/harper-core/src/linting/back_in_the_day.rs
+++ b/harper-core/src/linting/back_in_the_day.rs
@@ -13,13 +13,13 @@ pub struct BackInTheDay {
impl Default for BackInTheDay {
fn default() -> Self {
- let exceptions = Lrc::new(WordSet::all(&["before", "of", "when"]));
+ let exceptions = Lrc::new(WordSet::new(&["before", "of", "when"]));
let phrase = Lrc::new(ExactPhrase::from_phrase("back in the days"));
let pattern = SequencePattern::default()
- .then(Box::new(phrase.clone()))
+ .then(phrase.clone())
.then_whitespace()
- .then(Box::new(exceptions.clone()))
+ .then(exceptions.clone())
.or(Box::new(phrase));
Self {
diff --git a/harper-core/src/linting/chock_full.rs b/harper-core/src/linting/chock_full.rs
index dde777b27..346f92944 100644
--- a/harper-core/src/linting/chock_full.rs
+++ b/harper-core/src/linting/chock_full.rs
@@ -14,11 +14,11 @@ impl Default for ChockFull {
Self {
pattern: Box::new(
SequencePattern::default()
- .then(Box::new(WordSet::all(&["chalk", "choke"])))
- .then(Box::new(EitherPattern::new(vec![
+ .then(WordSet::new(&["chalk", "choke"]))
+ .then(EitherPattern::new(vec![
Box::new(WhitespacePattern),
Box::new(|tok: &Token, _source: &[char]| tok.kind.is_hyphen()),
- ])))
+ ]))
.then_exact_word("full"),
),
}
diff --git a/harper-core/src/linting/compound_nouns/general_compound_nouns.rs b/harper-core/src/linting/compound_nouns/general_compound_nouns.rs
index dcdb4be56..00ea4cd76 100644
--- a/harper-core/src/linting/compound_nouns/general_compound_nouns.rs
+++ b/harper-core/src/linting/compound_nouns/general_compound_nouns.rs
@@ -20,21 +20,21 @@ pub struct GeneralCompoundNouns {
impl Default for GeneralCompoundNouns {
fn default() -> Self {
let exceptions_pattern = SequencePattern::default()
- .then(Box::new(|tok: &Token, _: &[char]| {
+ .then(|tok: &Token, _: &[char]| {
let Some(Some(meta)) = tok.kind.as_word() else {
return false;
};
tok.span.len() > 1 && !meta.article && !meta.preposition
- }))
+ })
.then_whitespace()
- .then(Box::new(|tok: &Token, _: &[char]| {
+ .then(|tok: &Token, _: &[char]| {
let Some(Some(meta)) = tok.kind.as_word() else {
return false;
};
tok.span.len() > 1 && !meta.article && !meta.is_adverb() && !meta.preposition
- }));
+ });
let split_pattern = Lrc::new(SplitCompoundWord::new(|meta| {
meta.is_noun() && !meta.is_adjective()
diff --git a/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs b/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs
index fbf13ac51..5632ca5df 100644
--- a/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs
+++ b/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs
@@ -22,7 +22,7 @@ impl Default for ImpliedOwnershipCompoundNouns {
let pattern = SequencePattern::default()
.then_possessive_noun()
.then_whitespace()
- .then(Box::new(split_pattern.clone()));
+ .then(split_pattern.clone());
Self {
pattern: Box::new(pattern),
diff --git a/harper-core/src/linting/expand_time_shorthands.rs b/harper-core/src/linting/expand_time_shorthands.rs
index 816a77dc0..38645d735 100644
--- a/harper-core/src/linting/expand_time_shorthands.rs
+++ b/harper-core/src/linting/expand_time_shorthands.rs
@@ -11,28 +11,26 @@ pub struct ExpandTimeShorthands {
impl ExpandTimeShorthands {
pub fn new() -> Self {
- let hotwords = Arc::new(WordSet::all(&[
+ let hotwords = Arc::new(WordSet::new(&[
"hr", "hrs", "min", "mins", "sec", "secs", "ms", "msec", "msecs",
]));
Self {
- pattern: Box::new(
- SequencePattern::default()
- .then(Box::new(ImpliesQuantity))
- .then(Box::new(EitherPattern::new(vec![
- Box::new(SequencePattern::default().then(Box::new(hotwords.clone()))),
+ pattern: Box::new(SequencePattern::default().then(ImpliesQuantity).then(
+ EitherPattern::new(vec![
+ Box::new(SequencePattern::default().then(hotwords.clone())),
Box::new(
SequencePattern::default()
.then_whitespace()
- .then(Box::new(hotwords.clone())),
+ .then(hotwords.clone()),
),
Box::new(
SequencePattern::default()
.then_hyphen()
- .then(Box::new(hotwords.clone())),
+ .then(hotwords.clone()),
),
- ]))),
- ),
+ ]),
+ )),
}
}
diff --git a/harper-core/src/linting/hop_hope/to_hop.rs b/harper-core/src/linting/hop_hope/to_hop.rs
index d57e0a478..60c89b697 100644
--- a/harper-core/src/linting/hop_hope/to_hop.rs
+++ b/harper-core/src/linting/hop_hope/to_hop.rs
@@ -11,13 +11,13 @@ pub struct ToHop {
impl Default for ToHop {
fn default() -> Self {
let pattern = SequencePattern::default()
- .then_word_set(WordSet::all(&["hoping", "hoped", "hope"]))
+ .then(WordSet::new(&["hoping", "hoped", "hope"]))
.then_whitespace()
.t_aco("on")
.then_whitespace()
.then_article()
.then_whitespace()
- .then_word_set(WordSet::all(&["airplane", "plane", "bus", "call", "train"]));
+ .then(WordSet::new(&["airplane", "plane", "bus", "call", "train"]));
Self {
pattern: Box::new(pattern),
diff --git a/harper-core/src/linting/hop_hope/to_hope.rs b/harper-core/src/linting/hop_hope/to_hope.rs
index 76eaf2382..ec1d8e1ec 100644
--- a/harper-core/src/linting/hop_hope/to_hope.rs
+++ b/harper-core/src/linting/hop_hope/to_hope.rs
@@ -12,7 +12,7 @@ impl Default for ToHope {
let pattern = SequencePattern::default()
.then_singular_subject()
.then_whitespace()
- .then_word_set(WordSet::all(&["hop", "hopped"]))
+ .then(WordSet::new(&["hop", "hopped"]))
.then_whitespace()
.then_singular_subject();
diff --git a/harper-core/src/linting/hyphenate_number_day.rs b/harper-core/src/linting/hyphenate_number_day.rs
index 79b225b56..513e2037b 100644
--- a/harper-core/src/linting/hyphenate_number_day.rs
+++ b/harper-core/src/linting/hyphenate_number_day.rs
@@ -1,5 +1,5 @@
use crate::{
- patterns::{EitherPattern, Pattern, SequencePattern},
+ patterns::{EitherPattern, NounPhrase, Pattern, SequencePattern},
Token,
};
@@ -15,20 +15,20 @@ impl Default for HyphenateNumberDay {
.then_number()
.then_whitespace()
.t_aco("day")
- .then(Box::new(EitherPattern::new(vec![
+ .then(EitherPattern::new(vec![
Box::new(
SequencePattern::default()
.then_whitespace()
- .then_noun_phrase(),
+ .then(NounPhrase),
),
Box::new(
SequencePattern::default()
.then_hyphen()
.then_adjective()
.then_whitespace()
- .then_noun_phrase(),
+ .then(NounPhrase),
),
- ])));
+ ]));
Self {
pattern: Box::new(pattern),
diff --git a/harper-core/src/linting/left_right_hand.rs b/harper-core/src/linting/left_right_hand.rs
index f504a00d2..3cedc0580 100644
--- a/harper-core/src/linting/left_right_hand.rs
+++ b/harper-core/src/linting/left_right_hand.rs
@@ -12,7 +12,7 @@ pub struct LeftRightHand {
impl Default for LeftRightHand {
fn default() -> Self {
let pattern = SequencePattern::default()
- .then_word_set(WordSet::all(&["left", "right"]))
+ .then(WordSet::new(&["left", "right"]))
.then_whitespace()
.t_aco("hand")
.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 d3bb80cfc..12f898396 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
@@ -13,7 +13,7 @@ pub struct NoContractionWithVerb {
impl Default for NoContractionWithVerb {
fn default() -> Self {
let pattern = SequencePattern::default()
- .then_word_set(WordSet::all(&["lets", "let"]))
+ .then(WordSet::new(&["lets", "let"]))
.then_whitespace()
.then_verb();
diff --git a/harper-core/src/linting/likewise.rs b/harper-core/src/linting/likewise.rs
index ee0c521c4..6af86e396 100644
--- a/harper-core/src/linting/likewise.rs
+++ b/harper-core/src/linting/likewise.rs
@@ -16,14 +16,14 @@ impl Default for Likewise {
SequencePattern::aco("like").then_whitespace().t_aco("wise"),
));
- pattern.add(Box::new(Invert::new(Box::new(
+ pattern.add(Box::new(Invert::new(
SequencePattern::default()
.then_anything()
.then_whitespace()
.then_anything()
.then_whitespace()
.then_noun(),
- ))));
+ )));
Self {
pattern: Box::new(pattern),
diff --git a/harper-core/src/linting/multiple_sequential_pronouns.rs b/harper-core/src/linting/multiple_sequential_pronouns.rs
index 275ee6e9e..cfdb4462e 100644
--- a/harper-core/src/linting/multiple_sequential_pronouns.rs
+++ b/harper-core/src/linting/multiple_sequential_pronouns.rs
@@ -1,9 +1,7 @@
-use hashbrown::HashSet;
-
use super::pattern_linter::PatternLinter;
use super::Suggestion;
use crate::linting::LintKind;
-use crate::patterns::{Pattern, SequencePattern};
+use crate::patterns::{Pattern, SequencePattern, WordSet};
use crate::{Lint, Lrc, Token, TokenStringExt};
/// Linter that checks if multiple pronouns are being used right after each
@@ -14,22 +12,18 @@ pub struct MultipleSequentialPronouns {
impl MultipleSequentialPronouns {
fn new() -> Self {
- let pronouns: HashSet<_> = [
+ let pronouns = Lrc::new(WordSet::new(&[
"me", "my", "I", "we", "you", "he", "him", "her", "she", "it", "they",
- ]
- .into_iter()
- .collect();
-
- let pronouns = Lrc::new(pronouns);
+ ]));
Self {
pattern: Box::new(
SequencePattern::default()
- .then_any_word_in(pronouns.clone())
+ .then(pronouns.clone())
.then_one_or_more(Box::new(
SequencePattern::default()
.then_whitespace()
- .then_any_word_in(pronouns.clone()),
+ .then(pronouns.clone()),
)),
),
}
diff --git a/harper-core/src/linting/no_oxford_comma.rs b/harper-core/src/linting/no_oxford_comma.rs
index 9c33533a3..34759ae23 100644
--- a/harper-core/src/linting/no_oxford_comma.rs
+++ b/harper-core/src/linting/no_oxford_comma.rs
@@ -1,5 +1,5 @@
use crate::{
- patterns::{Pattern, SequencePattern, WordSet},
+ patterns::{NounPhrase, Pattern, SequencePattern, WordSet},
Document, Token, TokenStringExt,
};
@@ -12,14 +12,18 @@ pub struct NoOxfordComma {
impl NoOxfordComma {
pub fn new() -> Self {
Self {
- pattern: SequencePattern::default()
- .then_noun_phrase()
- .then_comma()
- .then_whitespace()
- .then_noun_phrase()
+ pattern: {
+ let this = {
+ let this = SequencePattern::default();
+ this.then(NounPhrase)
+ }
.then_comma()
- .then_whitespace()
- .then(Box::new(WordSet::all(&["and", "or", "nor"]))),
+ .then_whitespace();
+ this.then(NounPhrase)
+ }
+ .then_comma()
+ .then_whitespace()
+ .then(WordSet::new(&["and", "or", "nor"])),
}
}
diff --git a/harper-core/src/linting/oxford_comma.rs b/harper-core/src/linting/oxford_comma.rs
index cbbdeb2a4..1ff655c97 100644
--- a/harper-core/src/linting/oxford_comma.rs
+++ b/harper-core/src/linting/oxford_comma.rs
@@ -1,5 +1,5 @@
use crate::{
- patterns::{Pattern, SequencePattern, WordSet},
+ patterns::{NounPhrase, Pattern, SequencePattern, WordSet},
Document, Token, TokenStringExt,
};
@@ -12,18 +12,21 @@ pub struct OxfordComma {
impl OxfordComma {
pub fn new() -> Self {
Self {
- pattern: SequencePattern::default()
- .then_one_or_more(Box::new(
- SequencePattern::default()
- .then_noun_phrase()
- .then_comma()
- .then_whitespace(),
- ))
- .then_noun_phrase()
- .then_whitespace()
- .then(Box::new(WordSet::all(&["and", "or", "nor"])))
+ pattern: {
+ let this = {
+ let this = SequencePattern::default().then_one_or_more(Box::new(
+ SequencePattern::default()
+ .then(NounPhrase)
+ .then_comma()
+ .then_whitespace(),
+ ));
+ this.then(NounPhrase)
+ }
.then_whitespace()
- .then_noun_phrase(),
+ .then(WordSet::new(&["and", "or", "nor"]))
+ .then_whitespace();
+ this.then(NounPhrase)
+ },
}
}
diff --git a/harper-core/src/linting/pique_interest.rs b/harper-core/src/linting/pique_interest.rs
index 78e86d37f..c72e4e81f 100644
--- a/harper-core/src/linting/pique_interest.rs
+++ b/harper-core/src/linting/pique_interest.rs
@@ -13,7 +13,7 @@ pub struct PiqueInterest {
impl Default for PiqueInterest {
fn default() -> Self {
let pattern = SequencePattern::default()
- .then_word_set(WordSet::all(&[
+ .then(WordSet::new(&[
"peak", "peaked", "peek", "peeked", "peeking", "peaking",
]))
.then_whitespace()
diff --git a/harper-core/src/linting/plural_conjugate.rs b/harper-core/src/linting/plural_conjugate.rs
index 65d1c1fba..912c67026 100644
--- a/harper-core/src/linting/plural_conjugate.rs
+++ b/harper-core/src/linting/plural_conjugate.rs
@@ -17,9 +17,9 @@ impl Default for PluralConjugate {
.then_exact_word("is");
let non_plural_case = SequencePattern::default()
- .then(Box::new(|tok: &Token, _source: &[char]| {
+ .then(|tok: &Token, _source: &[char]| {
tok.kind.is_not_plural_noun() && tok.kind.is_noun()
- }))
+ })
.then_whitespace()
.then_exact_word("are");
diff --git a/harper-core/src/linting/possessive_your.rs b/harper-core/src/linting/possessive_your.rs
index 8341c971f..59e6aa95b 100644
--- a/harper-core/src/linting/possessive_your.rs
+++ b/harper-core/src/linting/possessive_your.rs
@@ -11,11 +11,12 @@ pub struct PossessiveYour {
impl Default for PossessiveYour {
fn default() -> Self {
- let pattern = SequencePattern::aco("you").then_whitespace().then(Box::new(
- |tok: &Token, _source: &[char]| {
- tok.kind.is_noun() && !tok.kind.is_verb() && !tok.kind.is_adverb()
- },
- ));
+ let pattern =
+ SequencePattern::aco("you")
+ .then_whitespace()
+ .then(|tok: &Token, _source: &[char]| {
+ tok.kind.is_noun() && !tok.kind.is_verb() && !tok.kind.is_adverb()
+ });
Self {
pattern: Box::new(pattern),
diff --git a/harper-core/src/linting/pronoun_contraction/avoid_contraction.rs b/harper-core/src/linting/pronoun_contraction/avoid_contraction.rs
index 29a16aa43..686a364ce 100644
--- a/harper-core/src/linting/pronoun_contraction/avoid_contraction.rs
+++ b/harper-core/src/linting/pronoun_contraction/avoid_contraction.rs
@@ -13,9 +13,7 @@ impl Default for AvoidContraction {
fn default() -> Self {
let pattern = SequencePattern::aco("you're")
.then_whitespace()
- .then(Box::new(|tok: &Token, _source: &[char]| {
- tok.kind.is_noun() && !tok.kind.is_adjective()
- }));
+ .then(|tok: &Token, _source: &[char]| tok.kind.is_noun() && !tok.kind.is_adjective());
Self {
pattern: Box::new(pattern),
diff --git a/harper-core/src/linting/pronoun_contraction/should_contract.rs b/harper-core/src/linting/pronoun_contraction/should_contract.rs
index 76c757976..1fef75c02 100644
--- a/harper-core/src/linting/pronoun_contraction/should_contract.rs
+++ b/harper-core/src/linting/pronoun_contraction/should_contract.rs
@@ -15,7 +15,7 @@ impl Default for ShouldContract {
Self {
pattern: Box::new(
SequencePattern::default()
- .then_word_set(WordSet::all(&["your", "were"]))
+ .then(WordSet::new(&["your", "were"]))
.then_whitespace()
.t_aco("the")
.then_whitespace()
diff --git a/harper-core/src/linting/proper_noun_capitalization_linters.rs b/harper-core/src/linting/proper_noun_capitalization_linters.rs
index 8ac1cd12d..d03c2c2fb 100644
--- a/harper-core/src/linting/proper_noun_capitalization_linters.rs
+++ b/harper-core/src/linting/proper_noun_capitalization_linters.rs
@@ -59,7 +59,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
"Americas",
Box::new(ProperNounCapitalizationLinter::new(
SequencePattern::default()
- .then(Box::new(WordSet::all(&["South", "North", "Central"])))
+ .then(WordSet::new(&["South", "North", "Central"]))
.then_whitespace()
.t_aco("America"),
"When referring to North, Central, and South America, make sure to treat them as a proper noun.",
@@ -136,7 +136,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
EitherPattern::new(vec![
Box::new(
SequencePattern::default()
- .then_word_set(WordSet::all(&[
+ .then(WordSet::new(&[
"Atlantic",
"Pacific",
"Indian",
@@ -148,7 +148,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
),
Box::new(
SequencePattern::default()
- .then_word_set(WordSet::all(&[
+ .then(WordSet::new(&[
"Mediterranean",
"Caribbean",
"Baltic",
@@ -230,7 +230,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
"Koreas",
Box::new(ProperNounCapitalizationLinter::new(
SequencePattern::default()
- .then(Box::new(WordSet::all(&["South", "North"])))
+ .then(WordSet::new(&["South", "North"]))
.then_whitespace()
.t_aco("Korea"),
"When referring to the nations, make sure to treat them as a proper noun.",
@@ -260,12 +260,12 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
.t_aco("Town")
),
Box::new(SequencePattern::default()
- .then(Box::new(EitherPattern::new(vec![
- Box::new(WordSet::all(&[
+ .then(EitherPattern::new(vec![
+ Box::new(WordSet::new(&[
"Johor",
"Kota"
])),
- ])))
+ ]))
.then_whitespace()
.t_aco("Bahru")
),
@@ -277,12 +277,12 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
Box::new(SequencePattern::default()
.t_aco("Kuala")
.then_whitespace()
- .then(Box::new(EitherPattern::new(vec![
- Box::new(WordSet::all(&[
+ .then(EitherPattern::new(vec![
+ Box::new(WordSet::new(&[
"Lumpur",
"Terengganu"
])),
- ])))
+ ]))
),
Box::new(SequencePattern::default()
.t_aco("Shah")
@@ -313,7 +313,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
SequencePattern::default()
.t_aco("United")
.then_whitespace()
- .then(Box::new(EitherPattern::new(vec![
+ .then(EitherPattern::new(vec![
Box::new(SequencePattern::aco("Nations")),
Box::new(SequencePattern::aco("States")),
Box::new(SequencePattern::aco("Kingdom")),
@@ -324,7 +324,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
.then_whitespace()
.t_aco("Emirates")
)
- ]))),
+ ])),
"When referring to national or international organizations, make sure to treat them as a proper noun.",
dictionary.clone()))
);
@@ -335,8 +335,8 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
EitherPattern::new(vec![
Box::new(
SequencePattern::default()
- .then(Box::new(EitherPattern::new(vec![
- Box::new(WordSet::all(&[
+ .then(EitherPattern::new(vec![
+ Box::new(WordSet::new(&[
"Absolution",
"Admission",
"Alaska",
@@ -457,7 +457,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
.then_whitespace()
.t_aco("Souls"),
),
- ])))
+ ]))
.then_whitespace()
.t_aco("Day"),
),
@@ -485,7 +485,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
SequencePattern::default()
.t_aco("Amazon")
.then_whitespace()
- .then(Box::new(EitherPattern::new(vec![
+ .then(EitherPattern::new(vec![
Box::new(
SequencePattern::default()
.t_aco("Shopping")
@@ -544,7 +544,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
SequencePattern::default()
.t_aco("Kindle")
)
- ]))),
+ ])),
"When referring to the various products of Amazon.com, make sure to treat them as a proper noun.",
dictionary.clone()))
);
@@ -555,7 +555,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
SequencePattern::default()
.t_aco("Google")
.then_whitespace()
- .then(Box::new(WordSet::all(&[
+ .then(WordSet::new(&[
"Search",
"Cloud",
"Maps",
@@ -579,7 +579,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
"Pixel",
"Nest",
"Workspace",
- ]))),
+ ])),
"When referring to Google products and services, make sure to treat them as proper nouns."
,dictionary.clone()))
);
@@ -590,7 +590,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
SequencePattern::default()
.t_aco("Azure")
.then_whitespace()
- .then(Box::new(EitherPattern::new(vec![
+ .then(EitherPattern::new(vec![
Box::new(SequencePattern::aco("DevOps")),
Box::new(SequencePattern::aco("Functions")),
Box::new(
@@ -655,7 +655,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
.then_whitespace()
.t_aco("Hub"),
),
- ]))),
+ ])),
"When referring to Azure cloud services, make sure to treat them as proper nouns.",
dictionary.clone(),
)),
@@ -667,8 +667,8 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
SequencePattern::default()
.t_aco("Microsoft")
.then_whitespace()
- .then(Box::new(EitherPattern::new(vec![
- Box::new(WordSet::all(&[
+ .then(EitherPattern::new(vec![
+ Box::new(WordSet::new(&[
"Windows",
"Office",
"Teams",
@@ -690,7 +690,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
.then_whitespace()
.t_aco("Studio")
)
- ]))),
+ ])),
"When referring to Microsoft products and services, make sure to treat them as proper nouns.",
dictionary.clone()))
);
@@ -701,8 +701,8 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
SequencePattern::default()
.t_aco("Apple")
.then_whitespace()
- .then(Box::new(EitherPattern::new(vec![
- Box::new(WordSet::all(&[
+ .then(EitherPattern::new(vec![
+ Box::new(WordSet::new(&[
"iPhone", "iPad", "iMac", "MacBook", "Watch", "TV", "Music", "Arcade",
"iCloud", "Safari", "HomeKit", "CarPlay",
])),
@@ -735,7 +735,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
.then_whitespace()
.t_aco("Pro")
)
- ]))),
+ ])),
"When referring to Apple products and services, make sure to treat them as proper nouns.",
dictionary.clone()))
@@ -745,8 +745,8 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
"MetaNames",
Box::new(ProperNounCapitalizationLinter::new(SequencePattern::aco("Meta")
.then_whitespace()
- .then(Box::new(EitherPattern::new(vec![
- Box::new(WordSet::all(&[
+ .then(EitherPattern::new(vec![
+ Box::new(WordSet::new(&[
"Oculus", "Portals", "Quest", "Gaming", "Horizon",
])),
Box::new(
@@ -755,7 +755,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
.then_whitespace()
.t_aco("Labs")
),
- ]))),
+ ])),
"When referring to Meta products and services, make sure to treat them as proper nouns."
, dictionary.clone()
))
@@ -767,7 +767,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
SequencePattern::default()
.t_aco("Jetpack")
.then_whitespace()
- .then(Box::new(EitherPattern::new(vec![
+ .then(EitherPattern::new(vec![
Box::new(
SequencePattern::default()
.t_aco("VaultPress")
@@ -806,7 +806,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
.t_aco("Agencies"),
),
Box::new(SequencePattern::default().t_aco("CRM")),
- ]))),
+ ])),
"Ensure proper capitalization of Jetpack-related terms.",
dictionary.clone(),
)),
@@ -818,7 +818,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
SequencePattern::default()
.t_aco("Tumblr")
.then_whitespace()
- .then(Box::new(EitherPattern::new(vec![
+ .then(EitherPattern::new(vec![
Box::new(SequencePattern::default().t_aco("Blaze")),
Box::new(SequencePattern::default().t_aco("Pro")),
Box::new(SequencePattern::default().t_aco("Live")),
@@ -826,7 +826,7 @@ pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {
Box::new(SequencePattern::default().t_aco("Communities")),
Box::new(SequencePattern::default().t_aco("Shop")),
Box::new(SequencePattern::default().t_aco("Dashboard")),
- ]))),
+ ])),
"Ensure proper capitalization of Tumblr-related terms.",
dictionary.clone(),
)),
diff --git a/harper-core/src/linting/terminating_conjunctions.rs b/harper-core/src/linting/terminating_conjunctions.rs
index 4588f3ec3..b8f7fc65d 100644
--- a/harper-core/src/linting/terminating_conjunctions.rs
+++ b/harper-core/src/linting/terminating_conjunctions.rs
@@ -11,7 +11,7 @@ impl Default for TerminatingConjunctions {
pattern: Box::new(ConsumesRemainingPattern::new(Box::new(
SequencePattern::default()
.then_anything_but_hyphen()
- .then_word_set(WordSet::all(&[
+ .then(WordSet::new(&[
"although",
"as",
"because",
diff --git a/harper-core/src/linting/then_than.rs b/harper-core/src/linting/then_than.rs
index 62d069042..1c1217798 100644
--- a/harper-core/src/linting/then_than.rs
+++ b/harper-core/src/linting/then_than.rs
@@ -14,14 +14,14 @@ impl ThenThan {
pattern: Box::new(All::new(vec![
Box::new(
SequencePattern::default()
- .then(Box::new(WordSet::all(&["better", "other"]).or(Box::new(
+ .then(WordSet::new(&["better", "other"]).or(Box::new(
|tok: &Token, _source: &[char]| tok.kind.is_adjective(),
- ))))
+ )))
.then_whitespace()
.then_any_capitalization_of("then"),
),
// Denotes exceptions to the rule.
- Box::new(Invert::new(Box::new(WordSet::all(&["back"])))),
+ Box::new(Invert::new(WordSet::new(&["back"]))),
])),
}
}
diff --git a/harper-core/src/linting/use_genitive.rs b/harper-core/src/linting/use_genitive.rs
index bbb7c97e8..b33842104 100644
--- a/harper-core/src/linting/use_genitive.rs
+++ b/harper-core/src/linting/use_genitive.rs
@@ -10,7 +10,7 @@ pub struct UseGenitive {
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(Box::new(
+ let environment = Lrc::new(SequencePattern::default().then_whitespace().then(
EitherPattern::new(vec![
Box::new(
SequencePattern::default()
@@ -20,7 +20,7 @@ impl UseGenitive {
),
Box::new(SequencePattern::default().then_noun()),
]),
- )));
+ ));
let trigger_words = ["there", "they're"];
@@ -32,20 +32,20 @@ impl UseGenitive {
Box::new(
SequencePattern::default()
.then_exact_word(word)
- .then(Box::new(environment.clone())),
+ .then(environment.clone()),
),
)
}
// Add a prelude to remove false-positives.
let full_pattern = SequencePattern::default()
- .then(Box::new(Invert::new(Box::new(EitherPattern::new(vec![
+ .then(Invert::new(EitherPattern::new(vec![
Box::new(SequencePattern::default().then_exact_word_or_lowercase("Is")),
Box::new(SequencePattern::default().then_exact_word_or_lowercase("Were")),
Box::new(SequencePattern::default().then_adjective()),
- ])))))
+ ])))
.then_whitespace()
- .then(Box::new(primary_pattern));
+ .then(primary_pattern);
Self {
pattern: Box::new(full_pattern),
diff --git a/harper-core/src/linting/was_aloud.rs b/harper-core/src/linting/was_aloud.rs
index aa422006f..dab7148e8 100644
--- a/harper-core/src/linting/was_aloud.rs
+++ b/harper-core/src/linting/was_aloud.rs
@@ -11,7 +11,7 @@ pub struct WasAloud {
impl Default for WasAloud {
fn default() -> Self {
let pattern = SequencePattern::default()
- .then_word_set(WordSet::all(&["was", "were", "be", "been"]))
+ .then(WordSet::new(&["was", "were", "be", "been"]))
.then_whitespace()
.then_exact_word("aloud");
diff --git a/harper-core/src/patterns/any_capitalization.rs b/harper-core/src/patterns/any_capitalization.rs
index eab611fbd..282b33748 100644
--- a/harper-core/src/patterns/any_capitalization.rs
+++ b/harper-core/src/patterns/any_capitalization.rs
@@ -12,7 +12,7 @@ impl AnyCapitalization {
Self { word }
}
- pub fn from_string(word: &str) -> Self {
+ pub fn of(word: &str) -> Self {
let chars = word.chars().collect();
Self::new(chars)
diff --git a/harper-core/src/patterns/exact_phrase.rs b/harper-core/src/patterns/exact_phrase.rs
index 4cece0cad..093f81ab1 100644
--- a/harper-core/src/patterns/exact_phrase.rs
+++ b/harper-core/src/patterns/exact_phrase.rs
@@ -18,17 +18,15 @@ impl ExactPhrase {
for token in doc.fat_tokens() {
match token.kind {
TokenKind::Word(_word_metadata) => {
- phrase = phrase.then(Box::new(AnyCapitalization::new(
- token.content.as_slice().into(),
- )));
+ phrase = phrase.then(AnyCapitalization::new(token.content.as_slice().into()));
}
TokenKind::Space(_) => {
phrase = phrase.then_whitespace();
}
TokenKind::Punctuation(p) => {
- phrase = phrase.then(Box::new(move |t: &Token, _source: &[char]| {
+ phrase = phrase.then(move |t: &Token, _source: &[char]| {
t.kind.as_punctuation().cloned() == Some(p)
- }))
+ })
}
TokenKind::ParagraphBreak => {
phrase = phrase.then_whitespace();
diff --git a/harper-core/src/patterns/indefinite_article.rs b/harper-core/src/patterns/indefinite_article.rs
index c1908e317..bb6cfbd21 100644
--- a/harper-core/src/patterns/indefinite_article.rs
+++ b/harper-core/src/patterns/indefinite_article.rs
@@ -9,7 +9,7 @@ pub struct IndefiniteArticle {
impl Default for IndefiniteArticle {
fn default() -> Self {
Self {
- inner: SequencePattern::default().then_word_set(WordSet::all(&["a", "an"])),
+ inner: SequencePattern::default().then(WordSet::new(&["a", "an"])),
}
}
}
diff --git a/harper-core/src/patterns/invert.rs b/harper-core/src/patterns/invert.rs
index 7a1878032..870bbef7d 100644
--- a/harper-core/src/patterns/invert.rs
+++ b/harper-core/src/patterns/invert.rs
@@ -8,8 +8,10 @@ pub struct Invert {
}
impl Invert {
- pub fn new(inner: Box<dyn Pattern>) -> Self {
- Self { inner }
+ pub fn new(inner: impl Pattern + 'static) -> Self {
+ Self {
+ inner: Box::new(inner),
+ }
}
}
diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rs
index 3c51b474e..c2fc8c2bb 100644
--- a/harper-core/src/patterns/sequence_pattern.rs
+++ b/harper-core/src/patterns/sequence_pattern.rs
@@ -1,12 +1,9 @@
-use hashbrown::HashSet;
use paste::paste;
use super::whitespace_pattern::WhitespacePattern;
use super::{
- AnyCapitalization, AnyPattern, IndefiniteArticle, NounPhrase, Pattern, RepeatingPattern,
- SingularSubject, WordSet,
+ AnyCapitalization, AnyPattern, IndefiniteArticle, Pattern, RepeatingPattern, SingularSubject,
};
-use crate::Lrc;
use crate::{CharStringExt, Token, TokenKind};
/// A pattern that checks that a sequence of other patterns match.
@@ -88,17 +85,8 @@ impl SequencePattern {
gen_then_from_is!(article);
gen_then_from_is!(proper_noun);
- pub fn then_word_set(self, set: WordSet) -> Self {
- self.then(Box::new(set))
- }
-
pub fn then_indefinite_article(self) -> Self {
- self.then(Box::new(IndefiniteArticle::default()))
- }
-
- /// Add a pattern that looks for more complex ideas, like nouns with adjectives attached.
- pub fn then_noun_phrase(self) -> Self {
- self.then(Box::new(NounPhrase))
+ self.then(IndefiniteArticle::default())
}
pub fn then_exact_word(mut self, word: &'static str) -> Self {
@@ -125,7 +113,7 @@ impl SequencePattern {
}
pub fn then_singular_subject(self) -> Self {
- self.then(Box::new(SingularSubject::default()))
+ self.then(SingularSubject::default())
}
/// Shorthand for [`Self::any_capitalization_of`].
@@ -145,7 +133,7 @@ impl SequencePattern {
/// Match examples of `word` that have any capitalization.
pub fn then_any_capitalization_of(mut self, word: &'static str) -> Self {
self.token_patterns
- .push(Box::new(AnyCapitalization::from_string(word)));
+ .push(Box::new(AnyCapitalization::of(word)));
self
}
@@ -177,15 +165,6 @@ impl SequencePattern {
self
}
- pub fn then_loose(mut self, kind: TokenKind) -> Self {
- self.token_patterns
- .push(Box::new(move |tok: &Token, _source: &[char]| {
- kind.with_default_data() == tok.kind.with_default_data()
- }));
-
- self
- }
-
pub fn then_any_word(mut self) -> Self {
self.token_patterns
.push(Box::new(|tok: &Token, _source: &[char]| tok.kind.is_word()));
@@ -200,34 +179,27 @@ impl SequencePattern {
self
}
+ /// Match against one or more whitespace tokens.
pub fn then_whitespace(mut self) -> Self {
self.token_patterns.push(Box::new(WhitespacePattern));
self
}
- pub fn then_any_word_in(mut self, word_set: Lrc<HashSet<&'static str>>) -> Self {
- self.token_patterns
- .push(Box::new(move |tok: &Token, source: &[char]| {
- let tok_chars = tok.span.get_content(source);
- let word: String = tok_chars.iter().collect();
- word_set.contains(word.as_str())
- }));
- self
- }
-
pub fn then_one_or_more(mut self, pat: Box<dyn Pattern>) -> Self {
self.token_patterns
.push(Box::new(RepeatingPattern::new(pat, 0)));
self
}
+ /// Match against any single token.
+ /// More of a filler then anything else.
pub fn then_anything(mut self) -> Self {
self.token_patterns.push(Box::new(AnyPattern));
self
}
- pub fn then(mut self, pat: Box<dyn Pattern>) -> Self {
- self.token_patterns.push(pat);
+ pub fn then(mut self, pat: impl Pattern + 'static) -> Self {
+ self.token_patterns.push(Box::new(pat));
self
}
}
@@ -252,11 +224,10 @@ impl Pattern for SequencePattern {
#[cfg(test)]
mod tests {
- use hashbrown::HashSet;
use super::SequencePattern;
use crate::patterns::Pattern;
- use crate::{Document, Lrc};
+ use crate::Document;
#[test]
fn matches_n_whitespace_tokens() {
@@ -285,25 +256,4 @@ mod tests {
doc.get_tokens().len()
);
}
-
- #[test]
- fn matches_sets() {
- let mut pronouns = HashSet::new();
- pronouns.insert("his");
- pronouns.insert("hers");
- let pronouns = Lrc::new(pronouns);
-
- let pat = SequencePattern::default()
- .then_exact_word("it")
- .then_whitespace()
- .then_exact_word("was")
- .then_whitespace()
- .then_any_word_in(pronouns);
- let doc = Document::new_plain_english_curated("it was hers");
-
- assert_eq!(
- pat.matches(doc.get_tokens(), doc.get_source()),
- doc.get_tokens().len()
- );
- }
}
diff --git a/harper-core/src/patterns/similar_to_phrase.rs b/harper-core/src/patterns/similar_to_phrase.rs
index 333d80969..190ad454c 100644
--- a/harper-core/src/patterns/similar_to_phrase.rs
+++ b/harper-core/src/patterns/similar_to_phrase.rs
@@ -35,13 +35,9 @@ impl SimilarToPhrase {
for token in document.fat_tokens() {
match token.kind {
TokenKind::Word(_word_metadata) => {
- phrase = phrase.then(Box::new(AnyCapitalization::new(
- token.content.as_slice().into(),
- )));
- fuzzy_phrase = fuzzy_phrase.then(Box::new(WithinEditDistance::new(
- token.content.into(),
- max_edit_dist,
- )));
+ phrase = phrase.then(AnyCapitalization::new(token.content.as_slice().into()));
+ fuzzy_phrase = fuzzy_phrase
+ .then(WithinEditDistance::new(token.content.into(), max_edit_dist));
}
TokenKind::Space(_) => {
fuzzy_phrase = fuzzy_phrase.then_whitespace();
diff --git a/harper-core/src/patterns/word_set.rs b/harper-core/src/patterns/word_set.rs
index 4372f8f7d..32d79e861 100644
--- a/harper-core/src/patterns/word_set.rs
+++ b/harper-core/src/patterns/word_set.rs
@@ -21,7 +21,8 @@ impl WordSet {
}
}
- pub fn all(words: &[&'static str]) -> Self {
+ /// Create a new word set that matches against any word in the provided list.
+ pub fn new(words: &[&'static str]) -> Self {
let mut set = Self::default();
for str in words {
@@ -71,7 +72,7 @@ mod tests {
#[test]
fn fruit() {
- let set = WordSet::all(&["banana", "apple", "orange"]);
+ let set = WordSet::new(&["banana", "apple", "orange"]);
let doc = Document::new_markdown_default_curated("I ate a banana and an apple today.");
@@ -82,7 +83,7 @@ mod tests {
#[test]
fn fruit_whack_capitalization() {
- let set = WordSet::all(&["banana", "apple", "orange"]);
+ let set = WordSet::new(&["banana", "apple", "orange"]);
let doc = Document::new_markdown_default_curated("I Ate A bAnaNa And aN apPlE today.");
diff --git a/packages/web/src/routes/docs/contributors/author-a-rule/+page.md b/packages/web/src/routes/docs/contributors/author-a-rule/+page.md
index e8e9d9230..843fdb4d9 100644
--- a/packages/web/src/routes/docs/contributors/author-a-rule/+page.md
+++ b/packages/web/src/routes/docs/contributors/author-a-rule/+page.md
@@ -62,7 +62,7 @@ group.add(
SequencePattern::default()
.t_aco("Tumblr")
.then_whitespace()
- .then(Box::new(EitherPattern::new(vec![
+ .then((EitherPattern::new(vec![
Box::new(SequencePattern::default().t_aco("Blaze")),
Box::new(SequencePattern::default().t_aco("Pro")),
Box::new(SequencePattern::default().t_aco("Live")),