Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): adjust how words are iterated in `title_case` (#2875)
Elijah Potter0796776dec05
diff --git a/harper-core/src/linting/use_title_case.rs b/harper-core/src/linting/use_title_case.rsindex 335862d36..d09db2400 100644--- a/harper-core/src/linting/use_title_case.rs+++ b/harper-core/src/linting/use_title_case.rs@@ -83,4 +83,21 @@ mod tests {"# My/Our/Your/His/Her/Its/Their",);}++ #[test]+ fn ignores_leading_number_list_marker_in_heading() {+ assert_no_lints(+ "### 1. To Do a Thing",+ UseTitleCase::new(FstDictionary::curated()),+ );+ }++ #[test]+ fn still_fixes_non_first_small_words_after_leading_number() {+ assert_suggestion_result(+ "### 1. To do a thing",+ UseTitleCase::new(FstDictionary::curated()),+ "### 1. To Do a Thing",+ );+ }}diff --git a/harper-core/src/title_case.rs b/harper-core/src/title_case.rsindex 6845d422d..e2bbaf405 100644--- a/harper-core/src/title_case.rs+++ b/harper-core/src/title_case.rs@@ -40,7 +40,7 @@ pub fn try_make_title_case(let start_index = toks.first().unwrap().span.start;let relevant_text = toks.span().unwrap().get_content(source);- let mut word_likes = toks.iter_word_like_indices().enumerate().peekable();+ let mut word_likes = toks.iter_word_like_indices().peekable();let mut output = None;let mut previous_word_index = 0;@@ -65,8 +65,15 @@ pub fn try_make_title_case(}};- while let Some((index, word_idx)) = word_likes.next() {+ let mut seen_alphabetic_word = false;++ while let Some(word_idx) = word_likes.next() {let word = &toks[word_idx];+ let is_alphabetic_word = word+ .span+ .get_content(source)+ .iter()+ .any(|c| c.is_alphabetic());if let Some(Some(metadata)) = word.kind.as_word()&& metadata.is_proper_noun()@@ -89,9 +96,10 @@ pub fn try_make_title_case(.iter().any(|tok| matches!(tok.kind, TokenKind::Punctuation(Punctuation::Colon)));+ let is_first_alphabetic_word = is_alphabetic_word && !seen_alphabetic_word;let should_capitalize = is_after_colon|| should_capitalize_token(word, source)- || index == 0+ || is_first_alphabetic_word|| word_likes.peek().is_none();if should_capitalize {@@ -109,6 +117,10 @@ pub fn try_make_title_case(}}+ if is_alphabetic_word {+ seen_alphabetic_word = true;+ }+previous_word_index = word_idx}