Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix: refactored `TokenStringExt` to work via macros
Elijah Potter6f9903ccbc23
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rsindex 8bf33a498..998f0bb68 100644--- a/harper-core/src/document.rs+++ b/harper-core/src/document.rs@@ -3,6 +3,7 @@ use std::collections::VecDeque;use std::fmt::Display;use itertools::Itertools;+use paste::paste;use crate::linting::Suggestion;use crate::parsers::{Markdown, Parser, PlainEnglish};@@ -443,20 +444,31 @@ impl Document {}}-impl Display for Document {- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {- for token in &self.tokens {- write!(f, "{}", self.get_span_content_str(token.span))?;- }+macro_rules! create_fns_on_doc {+ ($thing:ident) => {+ paste! {+ fn [< first_ $thing >](&self) -> Option<Token> {+ self.tokens.[< first_ $thing >]()+ }- Ok(())- }+ fn [<iter_ $thing _indices>](&self) -> impl Iterator<Item = usize> + '_ {+ self.tokens.[< iter_ $thing _indices >]()+ }++ fn [<iter_ $thing s>](&self) -> impl Iterator<Item = Token> + '_ {+ self.tokens.[< iter_ $thing s >]()+ }+ }+ };}impl TokenStringExt for Document {- fn first_word(&self) -> Option<Token> {- self.tokens.first_word()- }+ create_fns_on_doc!(word);+ create_fns_on_doc!(space);+ create_fns_on_doc!(apostrophe);+ create_fns_on_doc!(quote);+ create_fns_on_doc!(number);+ create_fns_on_doc!(at);fn first_sentence_word(&self) -> Option<Token> {self.tokens.first_sentence_word()@@ -466,56 +478,18 @@ impl TokenStringExt for Document {self.tokens.first_non_whitespace()}- fn iter_word_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.tokens.iter_word_indices()- }-- fn iter_words(&self) -> impl Iterator<Item = &Token> + '_ {- self.tokens.iter_words()- }-- fn iter_space_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.tokens.iter_space_indices()- }-- fn iter_spaces(&self) -> impl Iterator<Item = &Token> + '_ {- self.tokens.iter_spaces()- }-- fn iter_apostrophe_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.tokens.iter_apostrophe_indices()- }-- fn iter_apostrophes(&self) -> impl Iterator<Item = &Token> + '_ {- self.tokens.iter_apostrophes()- }-fn span(&self) -> Option<Span> {self.tokens.span()}+}- fn iter_quote_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.tokens.iter_quote_indices()- }-- fn iter_quotes(&self) -> impl Iterator<Item = Token> + '_ {- self.tokens.iter_quotes()- }-- fn iter_number_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.tokens.iter_number_indices()- }-- fn iter_numbers(&self) -> impl Iterator<Item = Token> + '_ {- self.tokens.iter_numbers()- }-- fn iter_at_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.tokens.iter_at_indices()- }+impl Display for Document {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ for token in &self.tokens {+ write!(f, "{}", self.get_span_content_str(token.span))?;+ }- fn iter_at(&self) -> impl Iterator<Item = Token> + '_ {- self.tokens.iter_at()+ Ok(())}}diff --git a/harper-core/src/linting/multiple_sequential_pronouns.rs b/harper-core/src/linting/multiple_sequential_pronouns.rsindex 6d400dc46..f88fdb70b 100644--- a/harper-core/src/linting/multiple_sequential_pronouns.rs+++ b/harper-core/src/linting/multiple_sequential_pronouns.rs@@ -37,9 +37,9 @@ impl Linter for MultipleSequentialPronouns {let mut found_pronouns = Vec::new();- let mut emit_lint = |found_pronouns: &mut Vec<&Token>| {- let first: &&Token = found_pronouns.first().unwrap();- let last: &&Token = found_pronouns.last().unwrap();+ let mut emit_lint = |found_pronouns: &mut Vec<Token>| {+ let first: &Token = found_pronouns.first().unwrap();+ let last: &Token = found_pronouns.last().unwrap();lints.push(Lint {span: Span::new(first.span.start, last.span.end),diff --git a/harper-core/src/linting/spaces.rs b/harper-core/src/linting/spaces.rsindex 598319434..3a2ac5fa8 100644--- a/harper-core/src/linting/spaces.rs+++ b/harper-core/src/linting/spaces.rs@@ -1,6 +1,6 @@use super::{Lint, Linter};use crate::token::TokenStringExt;-use crate::{Document, LintKind, Suggestion, TokenKind};+use crate::{document, Document, LintKind, Suggestion, TokenKind};#[derive(Debug, Default)]pub struct Spaces;diff --git a/harper-core/src/token.rs b/harper-core/src/token.rsindex fbac711a5..cbacf09c7 100644--- a/harper-core/src/token.rs+++ b/harper-core/src/token.rs@@ -1,10 +1,11 @@use is_macro::Is;use itertools::Itertools;+use paste::paste;use serde::{Deserialize, Serialize};use crate::punctuation::Punctuation;use crate::span::Span;-use crate::Quote;+use crate::{Document, Quote};#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Default)]pub struct Token {@@ -138,46 +139,79 @@ impl TokenKind {self.as_punctuation()?.as_quote()}+ pub fn is_quote(&self) -> bool {+ matches!(self, TokenKind::Punctuation(Punctuation::Quote(_)))+ }+pub fn is_apostrophe(&self) -> bool {matches!(self, TokenKind::Punctuation(Punctuation::Apostrophe))}+ pub fn is_at(&self) -> bool {+ matches!(self, TokenKind::Punctuation(Punctuation::At))+ }+/// Checks whether the token is whitespace.pub fn is_whitespace(&self) -> bool {matches!(self, TokenKind::Space(_) | TokenKind::Newline(_))}}+macro_rules! create_decl_for {+ ($thing:ident) => {+ paste! {+ fn [< first_ $thing >](&self) -> Option<Token> ;++ fn [<iter_ $thing _indices>](&self) -> impl Iterator<Item = usize> + '_;++ fn [<iter_ $thing s>](&self) -> impl Iterator<Item = Token> + '_;+ }+ };+}++macro_rules! create_fns_for {+ ($thing:ident) => {+ paste! {+ fn [< first_ $thing >](&self) -> Option<Token> {+ self.iter().find(|v| v.kind.[<is_ $thing>]()).copied()+ }++ fn [<iter_ $thing _indices>](&self) -> impl Iterator<Item = usize> + '_ {+ self.iter()+ .enumerate()+ .filter(|(_, t)| t.kind.[<is_ $thing>]())+ .map(|(i, _)| i)+ }++ fn [<iter_ $thing s>](&self) -> impl Iterator<Item = Token> + '_ {+ self.[<iter_ $thing _indices>]().map(|i| self[i])+ }+ }+ };+}+pub trait TokenStringExt {- fn first_word(&self) -> Option<Token>;- /// Grabs the first word in the sentence.- /// Will also return [`None`] if there is an unlintable token in the- /// position of the first word.fn first_sentence_word(&self) -> Option<Token>;- /// Grabs the first token that isn't whitespace from the token string.fn first_non_whitespace(&self) -> Option<Token>;- fn iter_word_indices(&self) -> impl Iterator<Item = usize> + '_;- fn iter_words(&self) -> impl Iterator<Item = &Token> + '_;- fn iter_space_indices(&self) -> impl Iterator<Item = usize> + '_;- fn iter_spaces(&self) -> impl Iterator<Item = &Token> + '_;- fn iter_apostrophe_indices(&self) -> impl Iterator<Item = usize> + '_;- fn iter_apostrophes(&self) -> impl Iterator<Item = &Token> + '_;/// Grab the span that represents the beginning of the first element and the/// end of the last element.fn span(&self) -> Option<Span>;- fn iter_quote_indices(&self) -> impl Iterator<Item = usize> + '_;- fn iter_quotes(&self) -> impl Iterator<Item = Token> + '_;- fn iter_number_indices(&self) -> impl Iterator<Item = usize> + '_;- fn iter_numbers(&self) -> impl Iterator<Item = Token> + '_;- fn iter_at_indices(&self) -> impl Iterator<Item = usize> + '_;- fn iter_at(&self) -> impl Iterator<Item = Token> + '_;+ create_decl_for!(word);+ create_decl_for!(space);+ create_decl_for!(apostrophe);+ create_decl_for!(quote);+ create_decl_for!(number);+ create_decl_for!(at);}impl TokenStringExt for [Token] {- fn first_word(&self) -> Option<Token> {- self.iter().find(|v| v.kind.is_word()).copied()- }+ create_fns_for!(word);+ create_fns_for!(space);+ create_fns_for!(apostrophe);+ create_fns_for!(quote);+ create_fns_for!(number);+ create_fns_for!(at);fn first_non_whitespace(&self) -> Option<Token> {self.iter().find(|t| !t.kind.is_whitespace()).copied()@@ -197,83 +231,7 @@ impl TokenStringExt for [Token] {}}- fn iter_word_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.iter()- .enumerate()- .filter(|(_, t)| t.kind.is_word())- .map(|(i, _)| i)- }-- fn iter_words(&self) -> impl Iterator<Item = &Token> + '_ {- self.iter_word_indices().map(|i| &self[i])- }-- fn iter_space_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.iter()- .enumerate()- .filter(|(_, t)| t.kind.is_space())- .map(|(i, _)| i)- }-- fn iter_spaces(&self) -> impl Iterator<Item = &Token> + '_ {- self.iter_space_indices().map(|i| &self[i])- }-- fn iter_apostrophe_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.iter()- .enumerate()- .filter(|(_, t)| t.kind.is_apostrophe())- .map(|(i, _)| i)- }-- fn iter_apostrophes(&self) -> impl Iterator<Item = &Token> + '_ {- self.iter_apostrophe_indices().map(|i| &self[i])- }-fn span(&self) -> Option<Span> {Some(Span::new(self.first()?.span.start, self.last()?.span.end))}-- fn iter_quote_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.iter().enumerate().filter_map(|(idx, token)| {- if let TokenKind::Punctuation(Punctuation::Quote(_)) = &token.kind {- Some(idx)- } else {- None- }- })- }-- fn iter_quotes(&self) -> impl Iterator<Item = Token> + '_ {- self.iter_quote_indices().map(|idx| self[idx])- }-- fn iter_number_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.iter().enumerate().filter_map(|(idx, token)| {- if let TokenKind::Number(..) = &token.kind {- Some(idx)- } else {- None- }- })- }-- fn iter_numbers(&self) -> impl Iterator<Item = Token> + '_ {- self.iter_number_indices().map(|idx| self[idx])- }-- /// Iterates through the indices of all "@" signs.- fn iter_at_indices(&self) -> impl Iterator<Item = usize> + '_ {- self.iter().enumerate().filter_map(|(idx, token)| {- if let TokenKind::Punctuation(Punctuation::At) = &token.kind {- Some(idx)- } else {- None- }- })- }-- fn iter_at(&self) -> impl Iterator<Item = Token> + '_ {- self.iter_at_indices().map(|idx| self[idx])- }}