Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands) increment by range #4418

Merged
merged 9 commits into from
Nov 11, 2022
30 changes: 27 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4808,14 +4808,19 @@ fn add_newline_impl(cx: &mut Context, open: Open) {
apply_transaction(&transaction, doc, view);
}

enum IncrementDirection {
Increase,
Decrease,
}

/// Increment object under cursor by count.
fn increment(cx: &mut Context) {
increment_impl(cx, cx.count() as i64);
increment_impl(cx, IncrementDirection::Increase);
}

/// Decrement object under cursor by count.
fn decrement(cx: &mut Context) {
increment_impl(cx, -(cx.count() as i64));
increment_impl(cx, IncrementDirection::Decrease);
}

/// This function differs from find_next_char_impl in that it stops searching at the newline, but also
Expand All @@ -4839,7 +4844,7 @@ fn find_next_char_until_newline<M: CharMatcher>(
}

/// Decrement object under cursor by `amount`.
fn increment_impl(cx: &mut Context, amount: i64) {
fn increment_impl(cx: &mut Context, increment_direction: IncrementDirection) {
// TODO: when incrementing or decrementing a number that gets a new digit or lose one, the
// selection is updated improperly.
find_char_impl(
Expand All @@ -4851,10 +4856,27 @@ fn increment_impl(cx: &mut Context, amount: i64) {
1,
);

let mut amount = match increment_direction {
IncrementDirection::Increase => cx.count() as i64,
IncrementDirection::Decrease => -(cx.count() as i64),
};

let (view, doc) = current!(cx.editor);
let selection = doc.selection(view.id);
let text = doc.text().slice(..);


let increase_by = match cx.register {
Some(value) => match value.eq(&'#') {
true => match increment_direction {
IncrementDirection::Increase => 1,
IncrementDirection::Decrease => -1,
},
false => 0,
},
None => 0,
};
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved

let changes: Vec<_> = selection
.ranges()
.iter()
Expand All @@ -4870,6 +4892,8 @@ fn increment_impl(cx: &mut Context, amount: i64) {

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

amount += increase_by;

Some((range.from(), range.to(), Some(new_text)))
})
.collect();
Expand Down