diff --git a/helix-core/src/case_conversion.rs b/helix-core/src/case_conversion.rs index 2054a2bb5..215456ac9 100644 --- a/helix-core/src/case_conversion.rs +++ b/helix-core/src/case_conversion.rs @@ -2,12 +2,63 @@ use crate::Tendril; // todo: should this be grapheme aware? -pub fn to_pascal_case(text: impl Iterator) -> Tendril { +fn to_case(text: I, to_case_with: fn(I, &mut Tendril)) -> Tendril +where + I: Iterator, +{ let mut res = Tendril::new(); - to_pascal_case_with(text, &mut res); + to_case_with(text, &mut res); res } +pub fn to_camel_case(text: impl Iterator) -> Tendril { + to_case(text, to_camel_case_with) +} + +pub fn to_lower_case(text: impl Iterator) -> Tendril { + to_case(text, to_lower_case_with) +} + +pub fn to_upper_case(text: impl Iterator) -> Tendril { + to_case(text, to_upper_case_with) +} + +pub fn to_pascal_case(text: impl Iterator) -> Tendril { + to_case(text, to_pascal_case_with) +} + +pub fn to_alternate_case(text: impl Iterator) -> Tendril { + to_case(text, to_pascal_case_with) +} + +pub fn to_upper_case_with(text: impl Iterator, buf: &mut Tendril) { + for c in text { + for c in c.to_uppercase() { + buf.push(c) + } + } +} + +pub fn to_lower_case_with(text: impl Iterator, buf: &mut Tendril) { + for c in text { + for c in c.to_lowercase() { + buf.push(c) + } + } +} + +pub fn to_alternate_case_with(text: impl Iterator, buf: &mut Tendril) { + for c in text { + if c.is_uppercase() { + buf.extend(c.to_lowercase()) + } else if c.is_lowercase() { + buf.extend(c.to_uppercase()) + } else { + buf.push(c) + } + } +} + pub fn to_pascal_case_with(text: impl Iterator, buf: &mut Tendril) { let mut at_word_start = true; for c in text { @@ -25,27 +76,6 @@ pub fn to_pascal_case_with(text: impl Iterator, buf: &mut Tendril) } } -pub fn to_upper_case_with(text: impl Iterator, buf: &mut Tendril) { - for c in text { - for c in c.to_uppercase() { - buf.push(c) - } - } -} - -pub fn to_lower_case_with(text: impl Iterator, buf: &mut Tendril) { - for c in text { - for c in c.to_lowercase() { - buf.push(c) - } - } -} - -pub fn to_camel_case(text: impl Iterator) -> Tendril { - let mut res = Tendril::new(); - to_camel_case_with(text, &mut res); - res -} 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 bc10bc6fd..793c0a5f6 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -18,7 +18,9 @@ use tui::{ pub use typed::*; use helix_core::{ - case_conversion::to_pascal_case, + case_conversion::{ + to_alternate_case, to_camel_case, to_lower_case, to_pascal_case, to_upper_case, + }, char_idx_at_visual_offset, chars::char_is_word, command_line, comment, @@ -353,11 +355,11 @@ impl MappableCommand { extend_prev_char, "Extend to previous occurrence of char", repeat_last_motion, "Repeat last motion", replace, "Replace with new char", - // switch_to_alternate_case, "Switch to aLTERNATE cASE", - // switch_to_uppercase, "Switch to UPPERCASE", - // switch_to_lowercase, "Switch to lowercase", + switch_to_alternate_case, "Switch to aLTERNATE cASE", + switch_to_upper_case, "Switch to UPPERCASE", + switch_to_lower_case, "Switch to lowercase", switch_to_pascal_case, "Switch to PascalCase", - // switch_to_camel_case, "Switch to camelCase", + switch_to_camel_case, "Switch to camelCase", // switch_to_title_case, "Switch to Title Case", // switch_to_snake_case, "Switch to snake_case", // switch_to_kebab_case, "Switch to kebab-case", @@ -1715,23 +1717,6 @@ fn replace(cx: &mut Context) { }) } -// fn switch_to_alternate_case(cx: &mut Context) { -// switch_case_impl(cx, |string| { -// string -// .chars() -// .flat_map(|ch| { -// if ch.is_lowercase() { -// ch.to_uppercase().collect() -// } else if ch.is_uppercase() { -// ch.to_lowercase().collect() -// } else { -// vec![ch] -// } -// }) -// .collect() -// }); -// } - fn switch_case_impl(cx: &mut Context, change_fn: F) where F: for<'a> Fn(&mut (dyn Iterator + 'a)) -> Tendril, @@ -1805,13 +1790,21 @@ fn switch_to_pascal_case(cx: &mut Context) { switch_case_impl(cx, |chars| to_pascal_case(chars)) } -// fn switch_to_camel_case(cx: &mut Context) { -// switch_heck_case_impl(cx, |str| str.to_lower_camel_case()) -// } +fn switch_to_camel_case(cx: &mut Context) { + switch_case_impl(cx, |chars| to_camel_case(chars)) +} -// fn switch_to_title_case(cx: &mut Context) { -// switch_heck_case_impl(cx, |str| str.to_title_case()) -// } +fn switch_to_lower_case(cx: &mut Context) { + switch_case_impl(cx, |chars| to_upper_case(chars)) +} + +fn switch_to_upper_case(cx: &mut Context) { + switch_case_impl(cx, |chars| to_lower_case(chars)) +} + +fn switch_to_alternate_case(cx: &mut Context) { + switch_case_impl(cx, |chars| to_alternate_case(chars)) +} // fn switch_to_snake_case(cx: &mut Context) { // switch_heck_case_impl(cx, |str| str.to_snake_case()) diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index d217d1f70..6dcff8e04 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -19,12 +19,12 @@ pub fn default() -> HashMap { "R" => replace_with_yanked, "A-." => repeat_last_motion, - // "~" => switch_to_alternate_case, + "~" => switch_to_alternate_case, "`" => { "Case" - // "l" => switch_to_lowercase, - // "u" => switch_to_uppercase, + "l" => switch_to_lowercase, + "u" => switch_to_uppercase, "p" => switch_to_pascal_case, - // "c" => switch_to_camel_case, + "c" => switch_to_camel_case, // "t" => switch_to_title_case, // "s" => switch_to_snake_case, // "k" => switch_to_kebab_case,