diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b608ef9f9..bc10bc6fd 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -18,6 +18,7 @@ use tui::{ pub use typed::*; use helix_core::{ + case_conversion::to_pascal_case, char_idx_at_visual_offset, chars::char_is_word, command_line, comment, @@ -54,7 +55,6 @@ use helix_view::{ }; use anyhow::{anyhow, bail, ensure, Context as _}; -use heck::{ToKebabCase, ToLowerCamelCase, ToSnakeCase, ToTitleCase, ToUpperCamelCase}; use insert::*; use movement::Movement; @@ -353,14 +353,14 @@ 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_uppercase, "Switch to UPPERCASE", + // switch_to_lowercase, "Switch to lowercase", switch_to_pascal_case, "Switch to PascalCase", - 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", + // 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", page_up, "Move page up", page_down, "Move page down", half_page_up, "Move half page up", @@ -1715,20 +1715,41 @@ 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: Fn(&dyn Iterator) -> Tendril, + F: for<'a> Fn(&mut (dyn Iterator + 'a)) -> Tendril, { let (view, doc) = current!(cx.editor); - let selection = doc.selection(view.id); - let transaction = Transaction::change_by_selection(doc.text(), selection, |range| { - let chars = range.slice(doc.text().slice(..)).chars(); - let text = change_fn(&chars); + let view_id = view.id; - (range.from(), range.to(), Some(text)) - }); + let selection = doc.selection(view_id); - doc.apply(&transaction, view.id); + let transaction = { + Transaction::change_by_selection(doc.text(), selection, |range| { + let mut chars = range.slice(doc.text().slice(..)).chars(); + let text: Tendril = change_fn(&mut chars); + (range.from(), range.to(), Some(text)) + }) + }; + + doc.apply(&transaction, view_id); exit_select_mode(cx); } @@ -1781,36 +1802,36 @@ fn switch_case(cx: &mut Context) { } fn switch_to_pascal_case(cx: &mut Context) { - switch_heck_case_impl(cx, |str| str.to_upper_camel_case()) + 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_heck_case_impl(cx, |str| str.to_lower_camel_case()) +// } -fn switch_to_title_case(cx: &mut Context) { - switch_heck_case_impl(cx, |str| str.to_title_case()) -} +// fn switch_to_title_case(cx: &mut Context) { +// switch_heck_case_impl(cx, |str| str.to_title_case()) +// } -fn switch_to_snake_case(cx: &mut Context) { - switch_heck_case_impl(cx, |str| str.to_snake_case()) -} +// fn switch_to_snake_case(cx: &mut Context) { +// switch_heck_case_impl(cx, |str| str.to_snake_case()) +// } -fn switch_to_kebab_case(cx: &mut Context) { - switch_heck_case_impl(cx, |str| str.to_kebab_case()) -} +// fn switch_to_kebab_case(cx: &mut Context) { +// switch_heck_case_impl(cx, |str| str.to_kebab_case()) +// } -fn switch_to_uppercase(cx: &mut Context) { - switch_case_impl(cx, |string| { - string.chunks().map(|chunk| chunk.to_uppercase()).collect() - }); -} +// fn switch_to_uppercase(cx: &mut Context) { +// switch_case_impl(cx, |string| { +// string.chunks().map(|chunk| chunk.to_uppercase()).collect() +// }); +// } -fn switch_to_lowercase(cx: &mut Context) { - switch_case_impl(cx, |string| { - string.chunks().map(|chunk| chunk.to_lowercase()).collect() - }); -} +// fn switch_to_lowercase(cx: &mut Context) { +// switch_case_impl(cx, |string| { +// string.chunks().map(|chunk| chunk.to_lowercase()).collect() +// }); +// } pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor: bool) { use Direction::*; diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index af66f364d..d217d1f70 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -19,15 +19,15 @@ 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, - "t" => switch_to_title_case, - "s" => switch_to_snake_case, - "k" => switch_to_kebab_case, + // "c" => switch_to_camel_case, + // "t" => switch_to_title_case, + // "s" => switch_to_snake_case, + // "k" => switch_to_kebab_case, }, "home" => goto_line_start,