feat: 5 functions to change case, lower, alternate, upper, pascal & camel

pull/12043/head
Nikita Revenco 2024-12-20 07:44:22 +00:00
parent ab4ad09dbe
commit 63f82c7bfa
3 changed files with 78 additions and 55 deletions

View File

@ -2,12 +2,63 @@ use crate::Tendril;
// todo: should this be grapheme aware?
pub fn to_pascal_case(text: impl Iterator<Item = char>) -> Tendril {
fn to_case<I>(text: I, to_case_with: fn(I, &mut Tendril)) -> Tendril
where
I: Iterator<Item = char>,
{
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<Item = char>) -> Tendril {
to_case(text, to_camel_case_with)
}
pub fn to_lower_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_lower_case_with)
}
pub fn to_upper_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_upper_case_with)
}
pub fn to_pascal_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_pascal_case_with)
}
pub fn to_alternate_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_pascal_case_with)
}
pub fn to_upper_case_with(text: impl Iterator<Item = char>, 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<Item = char>, 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<Item = char>, 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<Item = char>, 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<Item = char>, buf: &mut Tendril)
}
}
pub fn to_upper_case_with(text: impl Iterator<Item = char>, 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<Item = char>, buf: &mut Tendril) {
for c in text {
for c in c.to_lowercase() {
buf.push(c)
}
}
}
pub fn to_camel_case(text: impl Iterator<Item = char>) -> Tendril {
let mut res = Tendril::new();
to_camel_case_with(text, &mut res);
res
}
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

@ -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<F>(cx: &mut Context, change_fn: F)
where
F: for<'a> Fn(&mut (dyn Iterator<Item = char> + '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())

View File

@ -19,12 +19,12 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"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,