Skip to content

Commit

Permalink
Update change-case commands to work with gap indexing.
Browse files Browse the repository at this point in the history
  • Loading branch information
cessen committed Jul 17, 2021
1 parent a77274e commit 954314a
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,50 +786,59 @@ fn replace(cx: &mut Context) {

fn switch_case(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
let text: Tendril = range
.fragment(doc.text().slice(..))
.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();
let selection = doc
.selection(view.id)
.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(..))
.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();

(range.from(), range.to() + 1, Some(text))
});
(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) {
let (view, doc) = current!(cx.editor);
let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
let text: Tendril = range.fragment(doc.text().slice(..)).to_uppercase().into();
let selection = doc
.selection(view.id)
.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.append_changes_to_history(view.id);
}

fn switch_to_lowercase(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
let transaction =
Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
let text: Tendril = range.fragment(doc.text().slice(..)).to_lowercase().into();
let selection = doc
.selection(view.id)
.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.append_changes_to_history(view.id);
Expand Down

0 comments on commit 954314a

Please sign in to comment.