diff --git a/helix-core/src/case_conversion.rs b/helix-core/src/case_conversion.rs index 215456ac9..fca90dc06 100644 --- a/helix-core/src/case_conversion.rs +++ b/helix-core/src/case_conversion.rs @@ -28,7 +28,11 @@ pub fn to_pascal_case(text: impl Iterator) -> Tendril { } pub fn to_alternate_case(text: impl Iterator) -> Tendril { - to_case(text, to_pascal_case_with) + to_case(text, to_alternate_case_with) +} + +pub fn to_title_case(text: impl Iterator) -> Tendril { + to_case(text, to_title_case_with) } pub fn to_upper_case_with(text: impl Iterator, buf: &mut Tendril) { @@ -76,6 +80,24 @@ pub fn to_pascal_case_with(text: impl Iterator, buf: &mut Tendril) } } +pub fn to_title_case_with(text: impl Iterator, buf: &mut Tendril) { + let mut at_word_start = true; + for c in text { + // we don't count _ as a word char here so case conversions work well + if !c.is_alphanumeric() { + at_word_start = true; + continue; + } + if at_word_start { + at_word_start = false; + buf.push(' '); + buf.extend(c.to_uppercase()); + } else { + buf.push(c) + } + } +} + pub fn to_camel_case_with(mut text: impl Iterator, buf: &mut Tendril) { for c in &mut text { if c.is_alphanumeric() { diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 793c0a5f6..cb9e88c4c 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -19,7 +19,8 @@ pub use typed::*; use helix_core::{ case_conversion::{ - to_alternate_case, to_camel_case, to_lower_case, to_pascal_case, to_upper_case, + to_alternate_case, to_camel_case, to_lower_case, to_pascal_case, to_title_case, + to_upper_case, }, char_idx_at_visual_offset, chars::char_is_word, @@ -1806,6 +1807,10 @@ fn switch_to_alternate_case(cx: &mut Context) { switch_case_impl(cx, |chars| to_alternate_case(chars)) } +fn switch_to_title_case(cx: &mut Context) { + switch_case_impl(cx, |chars| to_title_case(chars)) +} + // fn switch_to_snake_case(cx: &mut Context) { // switch_heck_case_impl(cx, |str| str.to_snake_case()) // }