refactor: better function naming

pull/12043/head
Nikita Revenco 2024-12-20 11:32:14 +00:00
parent 1821000acd
commit 47826f53a4
2 changed files with 34 additions and 34 deletions

View File

@ -2,7 +2,7 @@ use crate::Tendril;
// todo: should this be grapheme aware? // todo: should this be grapheme aware?
pub fn to_simple_case_with( pub fn simple_case_conversion(
text: impl Iterator<Item = char>, text: impl Iterator<Item = char>,
buf: &mut Tendril, buf: &mut Tendril,
transform_char: impl Fn(&char) -> char, transform_char: impl Fn(&char) -> char,
@ -12,7 +12,7 @@ pub fn to_simple_case_with(
} }
} }
pub fn to_camel_or_pascal_or_title_case_with( pub fn complex_case_conversion(
text: impl Iterator<Item = char>, text: impl Iterator<Item = char>,
buf: &mut Tendril, buf: &mut Tendril,
capitalize_first: bool, capitalize_first: bool,
@ -49,7 +49,7 @@ pub fn to_camel_or_pascal_or_title_case_with(
} }
} }
pub fn to_case_with_separator( pub fn separator_case_conversion(
text: impl Iterator<Item = char>, text: impl Iterator<Item = char>,
buf: &mut Tendril, buf: &mut Tendril,
separator: char, separator: char,
@ -70,8 +70,8 @@ pub fn to_case_with_separator(
} }
} }
pub fn to_alternate_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn into_alternate_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_simple_case_with(text, buf, |c| { simple_case_conversion(text, buf, |c| {
if c.is_uppercase() { if c.is_uppercase() {
c.to_ascii_lowercase() c.to_ascii_lowercase()
} else if c.is_lowercase() { } else if c.is_lowercase() {
@ -82,32 +82,32 @@ pub fn to_alternate_case_with(text: impl Iterator<Item = char>, buf: &mut Tendri
}); });
} }
pub fn to_upper_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn into_upper_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_simple_case_with(text, buf, char::to_ascii_uppercase); simple_case_conversion(text, buf, char::to_ascii_uppercase);
} }
pub fn to_lower_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn into_lower_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_simple_case_with(text, buf, char::to_ascii_lowercase); simple_case_conversion(text, buf, char::to_ascii_lowercase);
} }
pub fn to_kebab_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn into_kebab_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_case_with_separator(text, buf, '-'); separator_case_conversion(text, buf, '-');
} }
pub fn to_snake_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn into_snake_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_case_with_separator(text, buf, '_'); separator_case_conversion(text, buf, '_');
} }
pub fn to_title_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn into_title_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_camel_or_pascal_or_title_case_with(text, buf, true, true, Some(' ')); complex_case_conversion(text, buf, true, true, Some(' '));
} }
pub fn to_camel_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn into_camel_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_camel_or_pascal_or_title_case_with(text, buf, false, true, None); complex_case_conversion(text, buf, false, true, None);
} }
pub fn to_pascal_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn into_pascal_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_camel_or_pascal_or_title_case_with(text, buf, true, true, None); complex_case_conversion(text, buf, true, true, None);
} }
fn to_case<I>(text: I, to_case_with: fn(I, &mut Tendril)) -> Tendril fn to_case<I>(text: I, to_case_with: fn(I, &mut Tendril)) -> Tendril
@ -120,35 +120,35 @@ where
} }
pub fn to_camel_case(text: impl Iterator<Item = char>) -> Tendril { pub fn to_camel_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_camel_case_with) to_case(text, into_camel_case)
} }
pub fn to_lower_case(text: impl Iterator<Item = char>) -> Tendril { pub fn to_lower_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_lower_case_with) to_case(text, into_lower_case)
} }
pub fn to_upper_case(text: impl Iterator<Item = char>) -> Tendril { pub fn to_upper_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_upper_case_with) to_case(text, into_upper_case)
} }
pub fn to_pascal_case(text: impl Iterator<Item = char>) -> Tendril { pub fn to_pascal_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_pascal_case_with) to_case(text, into_pascal_case)
} }
pub fn to_alternate_case(text: impl Iterator<Item = char>) -> Tendril { pub fn to_alternate_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_alternate_case_with) to_case(text, into_alternate_case)
} }
pub fn to_title_case(text: impl Iterator<Item = char>) -> Tendril { pub fn to_title_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_title_case_with) to_case(text, into_title_case)
} }
pub fn to_kebab_case(text: impl Iterator<Item = char>) -> Tendril { pub fn to_kebab_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_kebab_case_with) to_case(text, into_kebab_case)
} }
pub fn to_snake_case(text: impl Iterator<Item = char>) -> Tendril { pub fn to_snake_case(text: impl Iterator<Item = char>) -> Tendril {
to_case(text, to_snake_case_with) to_case(text, into_snake_case)
} }
#[cfg(test)] #[cfg(test)]

View File

@ -10,9 +10,9 @@ use regex_cursor::engines::meta::Regex;
use regex_cursor::regex_automata::util::syntax::Config as RegexConfig; use regex_cursor::regex_automata::util::syntax::Config as RegexConfig;
use ropey::RopeSlice; use ropey::RopeSlice;
use crate::case_conversion::to_lower_case_with; use crate::case_conversion::into_lower_case;
use crate::case_conversion::to_upper_case_with; use crate::case_conversion::into_upper_case;
use crate::case_conversion::{to_camel_case_with, to_pascal_case_with}; use crate::case_conversion::{into_camel_case, into_pascal_case};
use crate::snippets::parser::{self, CaseChange, FormatItem}; use crate::snippets::parser::{self, CaseChange, FormatItem};
use crate::snippets::{TabstopIdx, LAST_TABSTOP_IDX}; use crate::snippets::{TabstopIdx, LAST_TABSTOP_IDX};
use crate::Tendril; use crate::Tendril;
@ -348,15 +348,15 @@ impl Transform {
if let Some(cap) = cap.get_group(i).filter(|i| !i.is_empty()) { if let Some(cap) = cap.get_group(i).filter(|i| !i.is_empty()) {
let mut chars = doc.byte_slice(cap.range()).chars(); let mut chars = doc.byte_slice(cap.range()).chars();
match change { match change {
CaseChange::Upcase => to_upper_case_with(chars, &mut buf), CaseChange::Upcase => into_upper_case(chars, &mut buf),
CaseChange::Downcase => to_lower_case_with(chars, &mut buf), CaseChange::Downcase => into_lower_case(chars, &mut buf),
CaseChange::Capitalize => { CaseChange::Capitalize => {
let first_char = chars.next().unwrap(); let first_char = chars.next().unwrap();
buf.extend(first_char.to_uppercase()); buf.extend(first_char.to_uppercase());
buf.extend(chars); buf.extend(chars);
} }
CaseChange::PascalCase => to_pascal_case_with(chars, &mut buf), CaseChange::PascalCase => into_pascal_case(chars, &mut buf),
CaseChange::CamelCase => to_camel_case_with(chars, &mut buf), CaseChange::CamelCase => into_camel_case(chars, &mut buf),
} }
} }
} }