feat: `switch_to_title_case`

pull/12043/head
Nikita Revenco 2024-12-20 07:47:13 +00:00
parent 63f82c7bfa
commit 42e150293a
2 changed files with 29 additions and 2 deletions

View File

@ -28,7 +28,11 @@ pub fn to_pascal_case(text: impl Iterator<Item = char>) -> Tendril {
}
pub fn to_alternate_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_pascal_case_with)
to_case(text, to_alternate_case_with)
}
pub fn to_title_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_title_case_with)
}
pub fn to_upper_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
@ -76,6 +80,24 @@ pub fn to_pascal_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril)
}
}
pub fn to_title_case_with(text: impl Iterator<Item = char>, 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<Item = char>, buf: &mut Tendril) {
for c in &mut text {
if c.is_alphanumeric() {

View File

@ -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())
// }