Skip to content

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

feat: add single token pattern trait (#1209)

Michael Schmidt43fdd39c7d4b
diff --git a/harper-core/src/linting/expand_time_shorthands.rs b/harper-core/src/linting/expand_time_shorthands.rs
index 32976303b..f5262f0d9 100644
--- a/harper-core/src/linting/expand_time_shorthands.rs
+++ b/harper-core/src/linting/expand_time_shorthands.rs
@@ -63,7 +63,7 @@ impl PatternLinter for ExpandTimeShorthands {
fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
let offending_span = matched_tokens.last()?.span;
- let implies_plural = ImpliesQuantity::implies_plurality(matched_tokens, source);
+ let implies_plural = ImpliesQuantity::implies_plurality(matched_tokens.first()?, source);
let offending_text = offending_span.get_content(source);
diff --git a/harper-core/src/patterns/any_pattern.rs b/harper-core/src/patterns/any_pattern.rs
index 44063572a..b5e616e0f 100644
--- a/harper-core/src/patterns/any_pattern.rs
+++ b/harper-core/src/patterns/any_pattern.rs
@@ -1,12 +1,12 @@
use crate::Token;
-use super::Pattern;
+use super::SingleTokenPattern;
-/// A [`Pattern`] that will match any single token.
+/// Matches any single token.
pub struct AnyPattern;
-impl Pattern for AnyPattern {
- fn matches(&self, tokens: &[Token], _source: &[char]) -> Option<usize> {
- if tokens.is_empty() { None } else { Some(1) }
+impl SingleTokenPattern for AnyPattern {
+ fn matches_token(&self, _token: &Token, _source: &[char]) -> bool {
+ true
}
}
diff --git a/harper-core/src/patterns/implies_quantity.rs b/harper-core/src/patterns/implies_quantity.rs
index 2ba59f550..6b0a5bdc0 100644
--- a/harper-core/src/patterns/implies_quantity.rs
+++ b/harper-core/src/patterns/implies_quantity.rs
@@ -1,6 +1,6 @@
use crate::{Token, TokenKind};
-use super::Pattern;
+use super::SingleTokenPattern;
/// This struct does two things.
///
@@ -11,9 +11,7 @@ use super::Pattern;
pub struct ImpliesQuantity;
impl ImpliesQuantity {
- pub fn implies_plurality(tokens: &[Token], source: &[char]) -> Option<bool> {
- let token = tokens.first()?;
-
+ pub fn implies_plurality(token: &Token, source: &[char]) -> Option<bool> {
match &token.kind {
TokenKind::Word(Some(word_metadata)) => {
if word_metadata.determiner {
@@ -35,13 +33,9 @@ impl ImpliesQuantity {
}
}
-impl Pattern for ImpliesQuantity {
- fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
- if Self::implies_plurality(tokens, source).is_some() {
- Some(1)
- } else {
- None
- }
+impl SingleTokenPattern for ImpliesQuantity {
+ fn matches_token(&self, token: &Token, source: &[char]) -> bool {
+ Self::implies_plurality(token, source).is_some()
}
}
diff --git a/harper-core/src/patterns/indefinite_article.rs b/harper-core/src/patterns/indefinite_article.rs
index 498311d30..6aab5b996 100644
--- a/harper-core/src/patterns/indefinite_article.rs
+++ b/harper-core/src/patterns/indefinite_article.rs
@@ -1,6 +1,6 @@
use crate::Token;
-use super::{Pattern, WordSet};
+use super::{SingleTokenPattern, WordSet};
pub struct IndefiniteArticle {
inner: WordSet,
@@ -14,8 +14,8 @@ impl Default for IndefiniteArticle {
}
}
-impl Pattern for IndefiniteArticle {
- fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
- self.inner.matches(tokens, source)
+impl SingleTokenPattern for IndefiniteArticle {
+ fn matches_token(&self, token: &Token, source: &[char]) -> bool {
+ self.inner.matches_token(token, source)
}
}
diff --git a/harper-core/src/patterns/inflection_of_be.rs b/harper-core/src/patterns/inflection_of_be.rs
index f3ae13c2a..364828b28 100644
--- a/harper-core/src/patterns/inflection_of_be.rs
+++ b/harper-core/src/patterns/inflection_of_be.rs
@@ -1,4 +1,4 @@
-use super::Pattern;
+use super::SingleTokenPattern;
use crate::Token;
use crate::patterns::WordSet;
@@ -23,8 +23,8 @@ impl InflectionOfBe {
}
}
-impl Pattern for InflectionOfBe {
- fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
- self.inner.matches(tokens, source)
+impl SingleTokenPattern for InflectionOfBe {
+ fn matches_token(&self, token: &Token, source: &[char]) -> bool {
+ self.inner.matches_token(token, source)
}
}
diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rs
index 3de3cb2ae..40c858d98 100644
--- a/harper-core/src/patterns/mod.rs
+++ b/harper-core/src/patterns/mod.rs
@@ -132,12 +132,15 @@ where
}
}
-impl<F> Pattern for F
-where
- F: LSend + Fn(&Token, &[char]) -> bool,
-{
+/// A simpler version of the [`Pattern`] trait that only matches a single
+/// token.
+pub trait SingleTokenPattern: LSend {
+ fn matches_token(&self, token: &Token, source: &[char]) -> bool;
+}
+
+impl<S: SingleTokenPattern> Pattern for S {
fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
- if self(tokens.first()?, source) {
+ if self.matches_token(tokens.first()?, source) {
Some(1)
} else {
None
@@ -145,6 +148,12 @@ where
}
}
+impl<F: LSend + Fn(&Token, &[char]) -> bool> SingleTokenPattern for F {
+ fn matches_token(&self, token: &Token, source: &[char]) -> bool {
+ self(token, source)
+ }
+}
+
pub trait DocPattern {
fn find_all_matches_in_doc(&self, document: &Document) -> Vec<Span>;
}
diff --git a/harper-core/src/patterns/within_edit_distance.rs b/harper-core/src/patterns/within_edit_distance.rs
index 897d7fe6e..967af45fa 100644
--- a/harper-core/src/patterns/within_edit_distance.rs
+++ b/harper-core/src/patterns/within_edit_distance.rs
@@ -1,11 +1,11 @@
use std::cell::RefCell;
-use super::Pattern;
+use super::SingleTokenPattern;
use crate::{CharString, CharStringExt, Token};
use crate::edit_distance::edit_distance_min_alloc;
-/// A [`Pattern`] that matches single words within a certain edit distance of a given word.
+/// Matches single words within a certain edit distance of a given word.
pub struct WithinEditDistance {
word: CharString,
max_edit_dist: u8,
@@ -31,14 +31,13 @@ thread_local! {
static BUFFERS: RefCell<(Vec<u8>, Vec<u8>)> = const { RefCell::new((Vec::new(), Vec::new())) };
}
-impl Pattern for WithinEditDistance {
- fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
- let first = tokens.first()?;
- if !first.kind.is_word() {
- return None;
+impl SingleTokenPattern for WithinEditDistance {
+ fn matches_token(&self, token: &Token, source: &[char]) -> bool {
+ if !token.kind.is_word() {
+ return false;
}
- let content = first.span.get_content(source);
+ let content = token.span.get_content(source);
BUFFERS.with_borrow_mut(|(buffer_a, buffer_b)| {
let distance = edit_distance_min_alloc(
@@ -47,11 +46,7 @@ impl Pattern for WithinEditDistance {
buffer_a,
buffer_b,
);
- if distance <= self.max_edit_dist {
- Some(1)
- } else {
- None
- }
+ distance <= self.max_edit_dist
})
}
}
diff --git a/harper-core/src/patterns/word.rs b/harper-core/src/patterns/word.rs
index 6ae5b5145..989b4d71c 100644
--- a/harper-core/src/patterns/word.rs
+++ b/harper-core/src/patterns/word.rs
@@ -1,4 +1,4 @@
-use super::Pattern;
+use super::SingleTokenPattern;
use crate::{CharString, Token};
@@ -36,17 +36,16 @@ impl Word {
}
}
-impl Pattern for Word {
- fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
- let tok = tokens.first()?;
- if !tok.kind.is_word() {
- return None;
+impl SingleTokenPattern for Word {
+ fn matches_token(&self, token: &Token, source: &[char]) -> bool {
+ if !token.kind.is_word() {
+ return false;
}
- if tok.span.len() != self.word.len() {
- return None;
+ if token.span.len() != self.word.len() {
+ return false;
}
- let chars = tok.span.get_content(source);
+ let chars = token.span.get_content(source);
let eq = if self.exact {
chars == self.word.as_slice()
} else {
@@ -56,7 +55,7 @@ impl Pattern for Word {
.all(|(a, b)| a.eq_ignore_ascii_case(b))
};
- if eq { Some(1) } else { None }
+ eq
}
}
diff --git a/harper-core/src/patterns/word_set.rs b/harper-core/src/patterns/word_set.rs
index 47034224d..03496ac7a 100644
--- a/harper-core/src/patterns/word_set.rs
+++ b/harper-core/src/patterns/word_set.rs
@@ -1,9 +1,9 @@
-use super::Pattern;
+use super::SingleTokenPattern;
use smallvec::SmallVec;
use crate::{CharString, Token};
-/// A [`Pattern`] that matches against any of a set of provided words.
+/// A [`super::Pattern`] that matches against any of a set of provided words.
/// For small sets of short words, it doesn't allocate.
///
/// Note that any capitalization of the contained words will result in a match.
@@ -37,14 +37,13 @@ impl WordSet {
}
}
-impl Pattern for WordSet {
- fn matches(&self, tokens: &[Token], source: &[char]) -> Option<usize> {
- let tok = tokens.first()?;
- if !tok.kind.is_word() {
- return None;
+impl SingleTokenPattern for WordSet {
+ fn matches_token(&self, token: &Token, source: &[char]) -> bool {
+ if !token.kind.is_word() {
+ return false;
}
- let tok_chars = tok.span.get_content(source);
+ let tok_chars = token.span.get_content(source);
for word in &self.words {
if tok_chars.len() != word.len() {
@@ -57,11 +56,11 @@ impl Pattern for WordSet {
.all(|(a, b)| a.eq_ignore_ascii_case(b));
if partial_match {
- return Some(1);
+ return true;
}
}
- None
+ false
}
}