Update change-case commands to work with gap indexing.

pull/376/head
Nathan Vegdahl 3 years ago
parent a77274e8bb
commit 954314a7c9

@ -786,24 +786,27 @@ fn replace(cx: &mut Context) {
fn switch_case(cx: &mut Context) { fn switch_case(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let transaction = let selection = doc
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { .selection(view.id)
let text: Tendril = range .clone()
.fragment(doc.text().slice(..)) .min_width_1(doc.text().slice(..));
.chars() let transaction = Transaction::change_by_selection(doc.text(), &selection, |range| {
.flat_map(|ch| { let text: Tendril = range
if ch.is_lowercase() { .fragment(doc.text().slice(..))
ch.to_uppercase().collect() .chars()
} else if ch.is_uppercase() { .flat_map(|ch| {
ch.to_lowercase().collect() if ch.is_lowercase() {
} else { ch.to_uppercase().collect()
vec![ch] } else if ch.is_uppercase() {
} ch.to_lowercase().collect()
}) } else {
.collect(); vec![ch]
}
})
.collect();
(range.from(), range.to() + 1, Some(text)) (range.from(), range.to(), Some(text))
}); });
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id); doc.append_changes_to_history(view.id);
@ -811,12 +814,15 @@ fn switch_case(cx: &mut Context) {
fn switch_to_uppercase(cx: &mut Context) { fn switch_to_uppercase(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let transaction = let selection = doc
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { .selection(view.id)
let text: Tendril = range.fragment(doc.text().slice(..)).to_uppercase().into(); .clone()
.min_width_1(doc.text().slice(..));
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() + 1, Some(text)) (range.from(), range.to(), Some(text))
}); });
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id); doc.append_changes_to_history(view.id);
@ -824,12 +830,15 @@ fn switch_to_uppercase(cx: &mut Context) {
fn switch_to_lowercase(cx: &mut Context) { fn switch_to_lowercase(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let transaction = let selection = doc
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| { .selection(view.id)
let text: Tendril = range.fragment(doc.text().slice(..)).to_lowercase().into(); .clone()
.min_width_1(doc.text().slice(..));
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() + 1, Some(text)) (range.from(), range.to(), Some(text))
}); });
doc.apply(&transaction, view.id); doc.apply(&transaction, view.id);
doc.append_changes_to_history(view.id); doc.append_changes_to_history(view.id);

Loading…
Cancel
Save