Skip to content

feat(commands) increment by range #4418

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

Merged
merged 9 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ impl MappableCommand {
rename_symbol, "Rename symbol",
increment, "Increment item under cursor",
decrement, "Decrement item under cursor",
increasing_increment, "Increment item under cursor. Increment amount increases by 1 each item",
decreasing_decrement, "Decrement item under cursor. Decreament amount decreases by 1 each item",
record_macro, "Record macro",
replay_macro, "Replay macro",
command_palette, "Open command palette",
Expand Down Expand Up @@ -4810,12 +4812,20 @@ fn add_newline_impl(cx: &mut Context, open: Open) {

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

/// Decrement object under cursor by count.
fn decrement(cx: &mut Context) {
increment_impl(cx, -(cx.count() as i64));
increment_impl(cx, -(cx.count() as i64), 0);
}
/// Increment object under cursor by count. Each increment increases by 1.
fn increasing_increment(cx: &mut Context) {
increment_impl(cx, cx.count() as i64, 1);
}
/// Decrement object under cursor by count. Each decrement decreases by -1.
fn decreasing_decrement(cx: &mut Context) {
increment_impl(cx, -(cx.count() as i64), -1);
}

/// This function differs from find_next_char_impl in that it stops searching at the newline, but also
Expand All @@ -4839,7 +4849,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, amount: i64, increase_by: i64) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing amounts to increase_impl, we can create an enum

enum IncrementDirection {
    Increase,
    Decrease,
}

And pass that to increment_impl, then check the cx.count and cx.register in the body of increment_impl and then decide whether to use a positive or negative amount based on the passed direction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, won't we need a 3rd value for the enum if we don't want to increase it at all?

And pass that to increment_impl, then check the cx.count and cx.register in the body of increment_impl and then decide whether to use a positive or negative amount based on the passed direction.

Alright, that makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Count can't be 0 so we don't need a third member

// 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 @@ -4855,6 +4865,7 @@ fn increment_impl(cx: &mut Context, amount: i64) {
let selection = doc.selection(view.id);
let text = doc.text().slice(..);

let mut amount = amount;
let changes: Vec<_> = selection
.ranges()
.iter()
Expand All @@ -4870,6 +4881,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
3 changes: 3 additions & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ pub fn default() -> HashMap<Mode, Keymap> {

"C-a" => increment,
"C-x" => decrement,

"A-a" => increasing_increment,
// "A-x" => decreasing_decrement,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this uncommented?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A-x is already used for shrink_to_line_bounds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A-x is already used for shrink_to_line_bounds.

Do you have any suggestions for a better keymap?

});
let mut select = normal.clone();
select.merge_nodes(keymap!({ "Select mode"
Expand Down