Skip to content

Commit 0796776

Browse files
fix(core): adjust how words are iterated in title_case (#2875)
1 parent efe0f56 commit 0796776

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

harper-core/src/linting/use_title_case.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,21 @@ mod tests {
8383
"# My/Our/Your/His/Her/Its/Their",
8484
);
8585
}
86+
87+
#[test]
88+
fn ignores_leading_number_list_marker_in_heading() {
89+
assert_no_lints(
90+
"### 1. To Do a Thing",
91+
UseTitleCase::new(FstDictionary::curated()),
92+
);
93+
}
94+
95+
#[test]
96+
fn still_fixes_non_first_small_words_after_leading_number() {
97+
assert_suggestion_result(
98+
"### 1. To do a thing",
99+
UseTitleCase::new(FstDictionary::curated()),
100+
"### 1. To Do a Thing",
101+
);
102+
}
86103
}

harper-core/src/title_case.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn try_make_title_case(
4040
let start_index = toks.first().unwrap().span.start;
4141
let relevant_text = toks.span().unwrap().get_content(source);
4242

43-
let mut word_likes = toks.iter_word_like_indices().enumerate().peekable();
43+
let mut word_likes = toks.iter_word_like_indices().peekable();
4444

4545
let mut output = None;
4646
let mut previous_word_index = 0;
@@ -65,8 +65,15 @@ pub fn try_make_title_case(
6565
}
6666
};
6767

68-
while let Some((index, word_idx)) = word_likes.next() {
68+
let mut seen_alphabetic_word = false;
69+
70+
while let Some(word_idx) = word_likes.next() {
6971
let word = &toks[word_idx];
72+
let is_alphabetic_word = word
73+
.span
74+
.get_content(source)
75+
.iter()
76+
.any(|c| c.is_alphabetic());
7077

7178
if let Some(Some(metadata)) = word.kind.as_word()
7279
&& metadata.is_proper_noun()
@@ -89,9 +96,10 @@ pub fn try_make_title_case(
8996
.iter()
9097
.any(|tok| matches!(tok.kind, TokenKind::Punctuation(Punctuation::Colon)));
9198

99+
let is_first_alphabetic_word = is_alphabetic_word && !seen_alphabetic_word;
92100
let should_capitalize = is_after_colon
93101
|| should_capitalize_token(word, source)
94-
|| index == 0
102+
|| is_first_alphabetic_word
95103
|| word_likes.peek().is_none();
96104

97105
if should_capitalize {
@@ -109,6 +117,10 @@ pub fn try_make_title_case(
109117
}
110118
}
111119

120+
if is_alphabetic_word {
121+
seen_alphabetic_word = true;
122+
}
123+
112124
previous_word_index = word_idx
113125
}
114126

0 commit comments

Comments
 (0)