Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): preserve camelCase proper nouns in title case (#3438)
John Deckerd1a149142a03
diff --git a/harper-core/src/linting/use_title_case.rs b/harper-core/src/linting/use_title_case.rsindex cb8f01163..e4f0534ff 100644--- a/harper-core/src/linting/use_title_case.rs+++ b/harper-core/src/linting/use_title_case.rs@@ -101,6 +101,15 @@ mod tests {);}+ #[test]+ fn preserves_camel_case_proper_nouns_in_heading() {+ assert_markdown_suggestion_result(+ "### apple launched icloud",+ UseTitleCase::new(FstDictionary::curated()),+ "### Apple Launched iCloud",+ );+ }+#[test]fn does_not_capitalize_am_in_time_expression() {assert_no_lints(diff --git a/harper-core/src/title_case.rs b/harper-core/src/title_case.rsindex d99f8d28e..2684ad705 100644--- a/harper-core/src/title_case.rs+++ b/harper-core/src/title_case.rs@@ -115,6 +115,14 @@ pub fn try_make_title_case(continue;}+ // Tracks whether the proper-noun canonical-capitalization pass below+ // wrote an intentionally camelCase form (e.g., `iPhone`, `eBay`,+ // `macOS`). When true, the title-case rules below must not overwrite+ // that intentional lowercase first letter. Detected via the+ // dictionary's pre-computed `LOWER_CAMEL` orthography flag rather+ // than by re-inspecting the canonical string.+ let mut canonical_is_camel_case = false;+if let Some(Some(metadata)) = word.kind.as_word()&& metadata.is_proper_noun(){@@ -128,6 +136,8 @@ pub fn try_make_title_case(set_output_char(word.span.start - start_index + i, *c);}}++ canonical_is_camel_case = metadata.is_lower_camel();}};@@ -143,18 +153,24 @@ pub fn try_make_title_case(|| is_first_alphabetic_word|| word_likes.peek().is_none();- if should_capitalize {- set_output_char(- word.span.start - start_index,- relevant_text[word.span.start - start_index].to_ascii_uppercase(),- );- } else {- // The whole word should be lowercase.- for i in word.span {+ // The generic title-case rules below would overwrite the+ // intentional lowercase first letter of a camelCase canonical+ // (e.g., `iCloud` → `ICloud`). Skip them in that case; the+ // proper-noun pass above has already written the correct form.+ if !canonical_is_camel_case {+ if should_capitalize {set_output_char(- i - start_index,- relevant_text[i - start_index].to_ascii_lowercase(),+ word.span.start - start_index,+ relevant_text[word.span.start - start_index].to_ascii_uppercase(),);+ } else {+ // The whole word should be lowercase.+ for i in word.span {+ set_output_char(+ i - start_index,+ relevant_text[i - start_index].to_ascii_lowercase(),+ );+ }}}@@ -272,6 +288,30 @@ mod tests {)}+ #[test]+ fn preserves_icloud_camel_case_mid_sentence() {+ assert_eq!(+ make_title_case_str(+ "she backs up photos to icloud",+ &PlainEnglish,+ &FstDictionary::curated()+ ),+ "She Backs up Photos to iCloud",+ )+ }++ #[test]+ fn preserves_icloud_camel_case_as_first_word() {+ assert_eq!(+ make_title_case_str(+ "icloud syncs your files",+ &PlainEnglish,+ &FstDictionary::curated()+ ),+ "iCloud Syncs Your Files",+ )+ }+#[quickcheck]fn a_stays_lowercase(prefix: String, postfix: String) -> TestResult {// There must be words other than the `a`.