feat: basic implementation for pascal case

pull/12043/head
Nikita Revenco 2024-12-20 07:22:22 +00:00
parent 7dd782579e
commit ab4ad09dbe
2 changed files with 67 additions and 46 deletions

View File

@ -18,6 +18,7 @@ use tui::{
pub use typed::*; pub use typed::*;
use helix_core::{ use helix_core::{
case_conversion::to_pascal_case,
char_idx_at_visual_offset, char_idx_at_visual_offset,
chars::char_is_word, chars::char_is_word,
command_line, comment, command_line, comment,
@ -54,7 +55,6 @@ use helix_view::{
}; };
use anyhow::{anyhow, bail, ensure, Context as _}; use anyhow::{anyhow, bail, ensure, Context as _};
use heck::{ToKebabCase, ToLowerCamelCase, ToSnakeCase, ToTitleCase, ToUpperCamelCase};
use insert::*; use insert::*;
use movement::Movement; use movement::Movement;
@ -353,14 +353,14 @@ impl MappableCommand {
extend_prev_char, "Extend to previous occurrence of char", extend_prev_char, "Extend to previous occurrence of char",
repeat_last_motion, "Repeat last motion", repeat_last_motion, "Repeat last motion",
replace, "Replace with new char", replace, "Replace with new char",
switch_to_alternate_case, "Switch to aLTERNATE cASE", // switch_to_alternate_case, "Switch to aLTERNATE cASE",
switch_to_uppercase, "Switch to UPPERCASE", // switch_to_uppercase, "Switch to UPPERCASE",
switch_to_lowercase, "Switch to lowercase", // switch_to_lowercase, "Switch to lowercase",
switch_to_pascal_case, "Switch to PascalCase", 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_title_case, "Switch to Title Case",
switch_to_snake_case, "Switch to snake_case", // switch_to_snake_case, "Switch to snake_case",
switch_to_kebab_case, "Switch to kebab-case", // switch_to_kebab_case, "Switch to kebab-case",
page_up, "Move page up", page_up, "Move page up",
page_down, "Move page down", page_down, "Move page down",
half_page_up, "Move half page up", 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<F>(cx: &mut Context, change_fn: F) fn switch_case_impl<F>(cx: &mut Context, change_fn: F)
where where
F: Fn(&dyn Iterator<Item = char>) -> Tendril, F: for<'a> Fn(&mut (dyn Iterator<Item = char> + 'a)) -> Tendril,
{ {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id); let view_id = 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);
(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); exit_select_mode(cx);
} }
@ -1781,36 +1802,36 @@ fn switch_case(cx: &mut Context) {
} }
fn switch_to_pascal_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) { // fn switch_to_camel_case(cx: &mut Context) {
switch_heck_case_impl(cx, |str| str.to_lower_camel_case()) // switch_heck_case_impl(cx, |str| str.to_lower_camel_case())
} // }
fn switch_to_title_case(cx: &mut Context) { // fn switch_to_title_case(cx: &mut Context) {
switch_heck_case_impl(cx, |str| str.to_title_case()) // switch_heck_case_impl(cx, |str| str.to_title_case())
} // }
fn switch_to_snake_case(cx: &mut Context) { // fn switch_to_snake_case(cx: &mut Context) {
switch_heck_case_impl(cx, |str| str.to_snake_case()) // switch_heck_case_impl(cx, |str| str.to_snake_case())
} // }
fn switch_to_kebab_case(cx: &mut Context) { // fn switch_to_kebab_case(cx: &mut Context) {
switch_heck_case_impl(cx, |str| str.to_kebab_case()) // switch_heck_case_impl(cx, |str| str.to_kebab_case())
} // }
fn switch_to_uppercase(cx: &mut Context) { // fn switch_to_uppercase(cx: &mut Context) {
switch_case_impl(cx, |string| { // switch_case_impl(cx, |string| {
string.chunks().map(|chunk| chunk.to_uppercase()).collect() // string.chunks().map(|chunk| chunk.to_uppercase()).collect()
}); // });
} // }
fn switch_to_lowercase(cx: &mut Context) { // fn switch_to_lowercase(cx: &mut Context) {
switch_case_impl(cx, |string| { // switch_case_impl(cx, |string| {
string.chunks().map(|chunk| chunk.to_lowercase()).collect() // string.chunks().map(|chunk| chunk.to_lowercase()).collect()
}); // });
} // }
pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor: bool) { pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor: bool) {
use Direction::*; use Direction::*;

View File

@ -19,15 +19,15 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"R" => replace_with_yanked, "R" => replace_with_yanked,
"A-." => repeat_last_motion, "A-." => repeat_last_motion,
"~" => switch_to_alternate_case, // "~" => switch_to_alternate_case,
"`" => { "Case" "`" => { "Case"
"l" => switch_to_lowercase, // "l" => switch_to_lowercase,
"u" => switch_to_uppercase, // "u" => switch_to_uppercase,
"p" => switch_to_pascal_case, "p" => switch_to_pascal_case,
"c" => switch_to_camel_case, // "c" => switch_to_camel_case,
"t" => switch_to_title_case, // "t" => switch_to_title_case,
"s" => switch_to_snake_case, // "s" => switch_to_snake_case,
"k" => switch_to_kebab_case, // "k" => switch_to_kebab_case,
}, },
"home" => goto_line_start, "home" => goto_line_start,