Skip to content

Commit 2dca296

Browse files
Bruce-HopkinsShekhinah Memmel
authored andcommitted
feat(commands): increment by range (helix-editor#4418)
1 parent ab0d80c commit 2dca296

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

helix-term/src/commands.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4903,14 +4903,18 @@ fn add_newline_impl(cx: &mut Context, open: Open) {
49034903
apply_transaction(&transaction, doc, view);
49044904
}
49054905

4906+
enum IncrementDirection {
4907+
Increase,
4908+
Decrease,
4909+
}
49064910
/// Increment object under cursor by count.
49074911
fn increment(cx: &mut Context) {
4908-
increment_impl(cx, cx.count() as i64);
4912+
increment_impl(cx, IncrementDirection::Increase);
49094913
}
49104914

49114915
/// Decrement object under cursor by count.
49124916
fn decrement(cx: &mut Context) {
4913-
increment_impl(cx, -(cx.count() as i64));
4917+
increment_impl(cx, IncrementDirection::Decrease);
49144918
}
49154919

49164920
/// This function differs from find_next_char_impl in that it stops searching at the newline, but also
@@ -4934,7 +4938,7 @@ fn find_next_char_until_newline<M: CharMatcher>(
49344938
}
49354939

49364940
/// Decrement object under cursor by `amount`.
4937-
fn increment_impl(cx: &mut Context, amount: i64) {
4941+
fn increment_impl(cx: &mut Context, increment_direction: IncrementDirection) {
49384942
// TODO: when incrementing or decrementing a number that gets a new digit or lose one, the
49394943
// selection is updated improperly.
49404944
find_char_impl(
@@ -4946,6 +4950,17 @@ fn increment_impl(cx: &mut Context, amount: i64) {
49464950
1,
49474951
);
49484952

4953+
// Increase by 1 if `IncrementDirection` is `Increase`
4954+
// Decrease by 1 if `IncrementDirection` is `Decrease`
4955+
let sign = match increment_direction {
4956+
IncrementDirection::Increase => 1,
4957+
IncrementDirection::Decrease => -1,
4958+
};
4959+
let mut amount = sign * cx.count() as i64;
4960+
4961+
// If the register is `#` then increase or decrease the `amount` by 1 per element
4962+
let increase_by = if cx.register == Some('#') { sign } else { 0 };
4963+
49494964
let (view, doc) = current!(cx.editor);
49504965
let selection = doc.selection(view.id);
49514966
let text = doc.text().slice(..);
@@ -4965,6 +4980,8 @@ fn increment_impl(cx: &mut Context, amount: i64) {
49654980

49664981
let (range, new_text) = incrementor.increment(amount);
49674982

4983+
amount += increase_by;
4984+
49684985
Some((range.from(), range.to(), Some(new_text)))
49694986
})
49704987
.collect();

0 commit comments

Comments
 (0)