Refactor switch_case commands

This commit is contained in:
Gokul Soumya 2021-09-04 20:59:08 +05:30 committed by Blaž Hrastnik
parent 33ce8779fd
commit 95cd2c645b

View file

@ -811,12 +811,25 @@ fn replace(cx: &mut Context) {
}) })
} }
fn switch_case(cx: &mut Context) { fn switch_case_impl<F>(cx: &mut Context, change_fn: F)
where
F: Fn(Cow<str>) -> Tendril,
{
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id); let selection = doc.selection(view.id);
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| { let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
let text: Tendril = range let text: Tendril = change_fn(range.fragment(doc.text().slice(..)));
.fragment(doc.text().slice(..))
(range.from(), range.to(), Some(text))
});
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
}
fn switch_case(cx: &mut Context) {
switch_case_impl(cx, |string| {
string
.chars() .chars()
.flat_map(|ch| { .flat_map(|ch| {
if ch.is_lowercase() { if ch.is_lowercase() {
@ -827,39 +840,16 @@ fn switch_case(cx: &mut Context) {
vec![ch] vec![ch]
} }
}) })
.collect(); .collect()
(range.from(), range.to(), Some(text))
}); });
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
} }
fn switch_to_uppercase(cx: &mut Context) { fn switch_to_uppercase(cx: &mut Context) {
let (view, doc) = current!(cx.editor); switch_case_impl(cx, |string| string.to_uppercase().into());
let selection = doc.selection(view.id);
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
let text: Tendril = range.fragment(doc.text().slice(..)).to_uppercase().into();
(range.from(), range.to(), Some(text))
});
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
} }
fn switch_to_lowercase(cx: &mut Context) { fn switch_to_lowercase(cx: &mut Context) {
let (view, doc) = current!(cx.editor); switch_case_impl(cx, |string| string.to_lowercase().into());
let selection = doc.selection(view.id);
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
let text: Tendril = range.fragment(doc.text().slice(..)).to_lowercase().into();
(range.from(), range.to(), Some(text))
});
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id);
} }
pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) { pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) {