Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: used pattern linter for `RepeatedWords`
Elijah Pottere3bf6beaa7a2
diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rsindex 6c98add40..8e58c171d 100644--- a/harper-core/src/linting/repeated_words.rs+++ b/harper-core/src/linting/repeated_words.rs@@ -1,31 +1,20 @@-use hashbrown::HashSet;+use super::{Lint, LintKind, Linter, PatternLinter, Suggestion};+use crate::patterns::{Pattern, SequencePattern, WordPatternGroup};+use crate::token::{Token, TokenStringExt};-use super::{Lint, LintKind, Linter, Suggestion};-use crate::token::{Token, TokenKind, TokenStringExt};-use crate::{CharString, Document, Span};--#[derive(Debug, Clone)]pub struct RepeatedWords {- /// The set of words that can be considered for repetition checking.- set: HashSet<CharString>+ pattern: Box<dyn Pattern>}impl RepeatedWords {pub fn new() -> Self {- let mut set = HashSet::new();-- macro_rules! add_set {- ($lit:literal) => {- set.insert($lit.chars().collect());- };- ($($lit:literal),*) => {- $(- add_set!($lit);- )*- }- }+ Self::default()+ }+}- add_set!(+impl Default for RepeatedWords {+ fn default() -> Self {+ let words = ["the", "be", "to", "of", "and", "a", "in", "that", "have", "I", "it", "for", "not","on", "with", "he", "as", "you", "do", "at", "this", "is", "but", "his", "by", "from","they", "we", "say", "her", "she", "or", "an", "will", "my", "one", "all", "would",@@ -35,61 +24,38 @@ impl RepeatedWords {"than", "then", "now", "look", "only", "come", "its", "over", "think", "also", "back","after", "use", "two", "how", "our", "work", "first", "well", "way", "even", "new","want", "because", "any", "these", "give", "day", "most", "us", "are"- );+ ];- Self { set }- }-}+ let mut pattern = WordPatternGroup::default();-impl Default for RepeatedWords {- fn default() -> Self {- Self::new()- }-}--impl Linter for RepeatedWords {- fn lint(&mut self, document: &Document) -> Vec<Lint> {- let mut lints = Vec::new();-- for chunk in document.chunks() {- let mut iter = chunk.iter_word_indices().zip(chunk.iter_words()).peekable();+ for word in words {+ let mut rep = SequencePattern::default();+ rep.then_exact_word(word)+ .then_whitespace()+ .then_exact_word(word);- while let (Some((idx_a, tok_a)), Some((idx_b, tok_b))) = (iter.next(), iter.peek()) {- let word_a = document.get_span_content(tok_a.span);- let word_b = document.get_span_content(tok_b.span);-- if self.set.contains(word_a) && word_a == word_b {- let intervening_tokens = &chunk[idx_a + 1..*idx_b];-- if intervening_tokens.iter().any(|t| !t.kind.is_whitespace()) {- continue;- }+ pattern.add(word, Box::new(rep));+ }- // Detect and remove the whitespace between the repetitions.- let remove_end = tok_b.span.end;+ Self {+ pattern: Box::new(pattern)+ }+ }+}- let remove_start = if let Some(Token {- span,- kind: TokenKind::Space(_)- }) = intervening_tokens.last()- {- span.start- } else {- tok_b.span.start- };+impl PatternLinter for RepeatedWords {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }- lints.push(Lint {- span: Span::new(remove_start, remove_end),- lint_kind: LintKind::Repetition,- suggestions: vec![Suggestion::Remove],- message: "Did you mean to repeat this word?".to_string(),- ..Default::default()- })- }- }+ fn match_to_lint(&self, matched_tokens: &[Token], _source: &[char]) -> Lint {+ Lint {+ span: matched_tokens[1..].span().unwrap(),+ lint_kind: LintKind::Repetition,+ suggestions: vec![Suggestion::Remove],+ message: "Did you mean to repeat this word?".to_string(),+ ..Default::default()}-- lints}}diff --git a/harper-core/src/patterns/word_pattern_group.rs b/harper-core/src/patterns/word_pattern_group.rsindex fd4e00e5a..2c941d011 100644--- a/harper-core/src/patterns/word_pattern_group.rs+++ b/harper-core/src/patterns/word_pattern_group.rs@@ -6,6 +6,7 @@ use crate::CharString;/// A pattern collection to look for patterns that start with a specific/// word.+#[derive(Default)]pub struct WordPatternGroup<P>whereP: Pattern@@ -14,13 +15,15 @@ where}impl WordPatternGroup<NaivePatternGroup> {- pub fn add(&mut self, word: &[char], pat: Box<dyn Pattern>) {- if let Some(group) = self.patterns.get_mut(word) {+ 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();group.push(pat);- self.patterns.insert(word.into(), group);+ self.patterns.insert(chars, group);}}}