Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: now lints for non-spelled out numbers
Elijah Potter5a5ea09e9103
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rsindex dce5bf5a4..14e381f02 100644--- a/harper-core/src/document.rs+++ b/harper-core/src/document.rs@@ -338,6 +338,14 @@ impl TokenStringExt for Document {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 is_sentence_terminator(token: &TokenKind) -> bool {diff --git a/harper-core/src/lexing/email_address.rs b/harper-core/src/lexing/email_address.rsindex 6244d7b22..66f7c7909 100644--- a/harper-core/src/lexing/email_address.rs+++ b/harper-core/src/lexing/email_address.rs@@ -18,7 +18,7 @@ pub fn lex_email_address(source: &[char]) -> Option<FoundToken> {Some(FoundToken {next_index: at_loc + 1 + domain_part_len,- token: TokenKind::EmailAddress,+ token: TokenKind::EmailAddress})}@@ -90,7 +90,7 @@ fn valid_unquoted_character(c: char) -> bool {let others = ['!', '#', '$', '%', '&', '\'', '*', '+', '-', '/', '=', '?', '^', '_', '`', '{', '|', '}',- '~', '.',+ '~', '.'];if others.contains(&c) {@@ -125,7 +125,7 @@ mod tests {r#"user-"#,r#"postmaster"#,r#"postmaster"#,- r#"_test"#,+ r#"_test"#].into_iter().map(|s| s.chars().collect())@@ -155,7 +155,8 @@ mod tests {}}- /// Tests that the email parser will not throw a panic under some random situations.+ /// Tests that the email parser will not throw a panic under some random+ /// situations.#[test]fn survives_random_chars() {let mut rng = rand::thread_rng();diff --git a/harper-core/src/lexing/mod.rs b/harper-core/src/lexing/mod.rsindex 8b0cd7a29..bf900ea91 100644--- a/harper-core/src/lexing/mod.rs+++ b/harper-core/src/lexing/mod.rs@@ -36,24 +36,19 @@ pub fn lex_token(source: &[char]) -> Option<FoundToken> {}fn lex_word(source: &[char]) -> Option<FoundToken> {- let mut end = 0;-- while end < source.len() {- if lex_punctuation(&source[end + 1..]).is_none()- && lex_spaces(&source[end + 1..]).is_none()- && lex_newlines(&source[end + 1..]).is_none()- && end + 1 != source.len()- {- end += 1;- } else {- return Some(FoundToken {- next_index: end + 1,- token: TokenKind::Word- });- }- }+ let end = source+ .iter()+ .position(|c| !c.is_ascii_alphabetic())+ .unwrap_or(source.len());- None+ if end == 0 {+ None+ } else {+ Some(FoundToken {+ next_index: end,+ token: TokenKind::Word+ })+ }}pub fn lex_number(source: &[char]) -> Option<FoundToken> {diff --git a/harper-core/src/lexing/url.rs b/harper-core/src/lexing/url.rsindex 4b14fb6b9..83a513653 100644--- a/harper-core/src/lexing/url.rs+++ b/harper-core/src/lexing/url.rs@@ -14,7 +14,7 @@ pub fn lex_url(source: &[char]) -> Option<FoundToken> {Some(FoundToken {next_index: url_end + sep + 1,- token: TokenKind::Url,+ token: TokenKind::Url})}@@ -96,7 +96,7 @@ fn lex_hostport(source: &[char]) -> Option<usize> {c.is_ascii_digit()}).map(|(i, _)| i)- .unwrap_or(source.len()),+ .unwrap_or(source.len()))} else {Some(hostname_end)@@ -235,7 +235,8 @@ mod tests {assert_consumes_full("https://elijahpotter.dev/articles/quantifying_hope_on_a_global_scale")}- /// Tests that the URL parser will not throw a panic under some random situations.+ /// Tests that the URL parser will not throw a panic under some random+ /// situations.#[test]fn survives_random_chars() {let mut rng = rand::thread_rng();diff --git a/harper-core/src/linting/an_a.rs b/harper-core/src/linting/an_a.rsindex 1e96c531f..afb9ccf87 100644--- a/harper-core/src/linting/an_a.rs+++ b/harper-core/src/linting/an_a.rs@@ -16,7 +16,7 @@ impl Linter for AnA {let is_a_an = match chars_first {['a'] => Some(true),['a', 'n'] => Some(false),- _ => None,+ _ => None};let Some(a_an) = is_a_an else {@@ -28,7 +28,7 @@ impl Linter for AnA {if a_an != should_be_a_an {let replacement = match a_an {true => vec!['a', 'n'],- false => vec!['a'],+ false => vec!['a']};lints.push(Lint {@@ -36,7 +36,7 @@ impl Linter for AnA {lint_kind: LintKind::Formatting,suggestions: vec![Suggestion::ReplaceWith(replacement)],message: "This is not vocally correct.".to_string(),- priority: 31,+ priority: 31})}}diff --git a/harper-core/src/linting/lint_set.rs b/harper-core/src/linting/lint_set.rsindex 5c1886940..6ecb88520 100644--- a/harper-core/src/linting/lint_set.rs+++ b/harper-core/src/linting/lint_set.rs@@ -7,6 +7,7 @@ use super::repeated_words::RepeatedWords;use super::sentence_capitalization::SentenceCapitalization;use super::spaces::Spaces;use super::spell_check::SpellCheck;+use super::spelled_numbers::SpelledNumbers;use super::unclosed_quotes::UnclosedQuotes;use super::wrong_quotes::WrongQuotes;use super::Linter;@@ -39,6 +40,7 @@ impl LintSet {pub fn add_standard(&mut self, dictionary: impl Dictionary + 'static) -> &mut Self {self.add_repeated_words()+ .add_spelled_numbers().add_an_a().add_long_sentences().add_unclosed_quotes()@@ -99,6 +101,7 @@ macro_rules! create_simple_builder_methods {}create_simple_builder_methods!(+ SpelledNumbers,AnA,SentenceCapitalization,UnclosedQuotes,diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex c412d22ab..38928367e 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -7,6 +7,7 @@ mod repeated_words;mod sentence_capitalization;mod spaces;mod spell_check;+mod spelled_numbers;mod unclosed_quotes;mod wrong_quotes;diff --git a/harper-core/src/linting/spelled_numbers.rs b/harper-core/src/linting/spelled_numbers.rsnew file mode 100644index 000000000..15924a513--- /dev/null+++ b/harper-core/src/linting/spelled_numbers.rs@@ -0,0 +1,95 @@+use std::f64::EPSILON;++use crate::{Document, Lint, LintKind, Linter, Suggestion, TokenStringExt};++/// Linter that checks to make sure small integers (< one hundred) are spelled+/// out.+#[derive(Default, Clone, Copy)]+pub struct SpelledNumbers;++impl Linter for SpelledNumbers {+ fn lint(&mut self, document: &Document) -> Vec<crate::Lint> {+ let mut lints = Vec::new();++ for number_tok in document.iter_numbers() {+ let number = number_tok.kind.number().unwrap();+ dbg!(number);++ if number - number.floor() < EPSILON && number <= 100. {+ lints.push(Lint {+ span: number_tok.span,+ lint_kind: LintKind::Readability,+ suggestions: vec![Suggestion::ReplaceWith(+ spell_out_number(number as u64).unwrap().chars().collect()+ )],+ message: "Try to spell out numbers less than a hundred.".to_string(),+ priority: 63+ })+ }+ }++ lints+ }+}++/// Converts a number to it's spelled-out variant.+///+/// For example: 100 -> one-hundred.+///+/// Could easily be extended to support numbers greater than 110, but we don't+/// need that yet.+fn spell_out_number(num: u64) -> Option<String> {+ if num > 110 {+ return None;+ }++ Some(match num {+ 0 => "zero".to_string(),+ 1 => "one".to_string(),+ 2 => "two".to_string(),+ 3 => "three".to_string(),+ 4 => "four".to_string(),+ 5 => "five".to_string(),+ 6 => "six".to_string(),+ 7 => "seven".to_string(),+ 8 => "eight".to_string(),+ 9 => "nine".to_string(),+ 10 => "ten".to_string(),+ 11 => "eleven".to_string(),+ 12 => "twelve".to_string(),+ 20 => "twenty".to_string(),+ 30 => "thirty".to_string(),+ 40 => "forty".to_string(),+ 50 => "fifty".to_string(),+ 60 => "sixty".to_string(),+ 70 => "seventy".to_string(),+ 80 => "eighty".to_string(),+ 90 => "ninety".to_string(),+ 100 => "one hundred".to_string(),+ _ => {+ let parent = (num / 10) * 10;+ let child = num % 10;++ format!(+ "{} {}",+ spell_out_number(parent).unwrap(),+ spell_out_number(child).unwrap()+ )+ }+ })+}++#[cfg(test)]+mod tests {+ use super::spell_out_number;++ #[test]+ fn produces_fifty_three() {+ assert_eq!(spell_out_number(53), Some("fifty three".to_string()))+ }++ #[test]+ fn produces_eighty_two() {+ assert_eq!(spell_out_number(82), Some("eighty two".to_string()))+ }+}diff --git a/harper-core/src/token.rs b/harper-core/src/token.rsindex a078547b0..809fd033a 100644--- a/harper-core/src/token.rs+++ b/harper-core/src/token.rs@@ -166,6 +166,8 @@ pub trait TokenStringExt {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> + '_;}impl TokenStringExt for [Token] {@@ -241,4 +243,18 @@ impl TokenStringExt for [Token] {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])+ }}diff --git a/harper-ls/src/tree_sitter_parser.rs b/harper-ls/src/tree_sitter_parser.rsindex 4c60f9b37..8051b6689 100644--- a/harper-ls/src/tree_sitter_parser.rs+++ b/harper-ls/src/tree_sitter_parser.rs@@ -126,9 +126,7 @@ impl Parser for TreeSitterParser {Self::extract_comments(&mut root.walk(), &mut comments_spans);- dbg!(&comments_spans.len());byte_spans_to_char_spans(&mut comments_spans, &text);- dbg!(&comments_spans.len());let mut tokens = Vec::new();@@ -140,7 +138,7 @@ impl Parser for TreeSitterParser {.for_each(|v| v.span.offset(span.start));// The comment parser will insert a newline at end-of-input.- // If the next treesitter chunk is a comment, we want to remove that.+ // If the next tree-sitter chunk is a comment, we want to remove that.if let Some(next_start) = comments_spans.get(s_index + 1).map(|v| v.start) {if is_span_whitespace(Span::new(span.end, next_start), source) {new_tokens.pop();