Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
ongoing experiments
hippietrailb8e6e0bdb8e9
diff --git a/harper-core/src/linting/comma_fixes.rs b/harper-core/src/linting/comma_fixes.rsindex 3c1655152..6c55d5c6b 100644--- a/harper-core/src/linting/comma_fixes.rs+++ b/harper-core/src/linting/comma_fixes.rs@@ -1,7 +1,7 @@-use itertools::Itertools;+//use itertools::Itertools;use super::{Lint, LintKind, Linter, Suggestion};-use crate::TokenStringExt;+use crate::{Token, TokenKind, TokenStringExt};/// A linter that fixes common comma errors such as no space after, erroneous/// space before, etc, Asian commas instead of English commas, etc.@@ -10,6 +10,40 @@ pub struct CommaFixes;impl Linter for CommaFixes {fn lint(&mut self, document: &crate::Document) -> Vec<Lint> {+ let mut lints = Vec::new();++ let source = document.get_source();++ for ci in document.iter_comma_indices() {+ let mut toks = (None, None, document.get_token(ci).unwrap(), None, None);+ toks.0 = (ci >= 2).then(|| document.get_token(ci - 2).unwrap());+ toks.1 = (ci >= 1).then(|| document.get_token(ci - 1).unwrap());+ toks.3 = document.get_token(ci + 1);+ toks.4 = document.get_token(ci + 2);++ let which_comma = toks.2.span.get_content(source).first().unwrap();++ if which_comma != &',' {+ let flag_comma = true;+ }++ let msg = make_message(source, toks, *which_comma);++ lints.push(Lint {+ span: toks.2.span,+ lint_kind: LintKind::Formatting,+ suggestions: vec![Suggestion::ReplaceWith(vec![+ '«', 'c', 'o', 'm', 'm', 'a', '»',+ ])],+ message: msg,+ priority: 32,+ })+ }++ lints+ }++ /*fn oldlint(&mut self, document: &crate::Document) -> Vec<Lint> {let mut lints: Vec<Lint> = Vec::new();for tok in document.iter_commas() {@@ -29,13 +63,107 @@ impl Linter for CommaFixes {}lints- }+ }*/fn description(&self) -> &'static str {"Fix common comma errors such as no space after, erroneous space before, etc, Asian commas instead of English commas, etc."}}+fn make_message(+ source: &[char],+ toks: (+ Option<Token>,+ Option<Token>,+ Token,+ Option<Token>,+ Option<Token>,+ ),+ which_comma: char,+) -> String {+ let mut msg = String::new();++ let tok_str = |tok: Option<Token>| match tok {+ None => "❌".to_string(),+ Some(Token {+ kind: TokenKind::Word(_),+ ..+ }) => "Word".to_string(),+ Some(Token {+ kind: TokenKind::Punctuation(_),+ ..+ }) => "Punc".to_string(),+ Some(Token {+ kind: TokenKind::Decade,+ ..+ }) => "Decade".to_string(),+ Some(Token {+ kind: TokenKind::Number(_),+ ..+ }) => format!(+ "#{}",+ tok.unwrap()+ .span+ .get_content(source)+ .iter()+ .collect::<String>()+ ),+ Some(Token {+ kind: TokenKind::Space(_),+ ..+ }) => "␣".to_string(),+ Some(Token {+ kind: TokenKind::Newline(_),+ ..+ }) => "⏎".to_string(),+ Some(Token {+ kind: TokenKind::EmailAddress,+ ..+ }) => "@".to_string(),+ Some(Token {+ kind: TokenKind::Url,+ ..+ }) => "🔗".to_string(),+ Some(Token {+ kind: TokenKind::Hostname,+ ..+ }) => "Host".to_string(),+ Some(Token {+ kind: TokenKind::Unlintable,+ ..+ }) => "🚫".to_string(),+ Some(Token {+ kind: TokenKind::ParagraphBreak,+ ..+ }) => "¶".to_string(),+ Some(Token {+ kind: TokenKind::Regexish,+ ..+ }) => "[A-Z]".to_string(),+ };++ // add the type of each token to the message+ msg.push_str(&tok_str(toks.0));+ msg.push(' ');+ msg.push_str(&tok_str(toks.1));+ // if which_comma == ',' {+ // msg.push_str(" ●̧ ");+ // } else {+ // msg.push_str(format!(" ●̧<U+{:04X}> ", which_comma as u32).as_str());+ // }+ let comma_str = if which_comma == ',' {+ " ●̧ ".to_string()+ } else {+ format!(" ●̧<U+{:04X}> ", which_comma as u32)+ };+ msg.push_str(&comma_str);+ msg.push_str(&tok_str(toks.3));+ msg.push(' ');+ msg.push_str(&tok_str(toks.4));++ msg+}+#[cfg(test)]mod tests {use super::CommaFixes;