Repository: helix-editor/helix
File: helix-term/src/commands.rs (Rust)

Code before:
fn decrement(cx: &mut Context) {
    increment_impl(cx, -(cx.count() as i64), IncrementDirection::Decrease);
}

/// This function differs from find_next_char_impl in that it stops searching at the newline, but also
/// starts searching at the current character, instead of the next.
/// It does not want to start at the next character because this function is used for incrementing
/// number and we don't want to move forward if we're already on a digit.
fn find_next_char_until_newline<M: CharMatcher>(
    text: RopeSlice,
    char_matcher: M,
    pos: usize,
    _count: usize,
    _inclusive: bool,
) -> Option<usize> {
    // Since we send the current line to find_nth_next instead of the whole text, we need to adjust
    // the position we send to this function so that it's relative to that line and its returned
    // position since it's expected this function returns a global position.
    let line_index = text.char_to_line(pos);
    let pos_delta = text.line_to_char(line_index);
    let pos = pos - pos_delta;
    search::find_nth_next(text.line(line_index), char_matcher, pos, 1).map(|pos| pos + pos_delta)
}

/// Decrement object under cursor by `amount`.
fn increment_impl(cx: &mut Context, amount: i64, 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(
        cx.editor,
        &find_next_char_until_newline,
        true,
        true,
        char::is_ascii_digit,
        1,
    );

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

    let mut amount = amount;

    let increase_by = match cx.register {
        Some(value) => match value.eq(&'#') {
            true => match increment_direction {
                IncrementDirection::Increase => 1,
                IncrementDirection::Decrease => -1,
            },
            false => 0,
        },

Review comment (suggestion):
You can use the direction parameter and `cx.count()` to determine the amount in this function and then `amount` doesn't need to be a parameter.

For example, `increment` can be implemented just as

```rust
fn increment(cx: &mut Context) {
    increment_impl(cx, IncrementDirection::Increase);
}
```

Code after:
    increment_impl(cx, IncrementDirection::Decrease);
}

/// This function differs from find_next_char_impl in that it stops searching at the newline, but also
/// starts searching at the current character, instead of the next.
/// It does not want to start at the next character because this function is used for incrementing
/// number and we don't want to move forward if we're already on a digit.
fn find_next_char_until_newline<M: CharMatcher>(
    text: RopeSlice,
    char_matcher: M,
    pos: usize,
    _count: usize,
    _inclusive: bool,
) -> Option<usize> {
    // Since we send the current line to find_nth_next instead of the whole text, we need to adjust
    // the position we send to this function so that it's relative to that line and its returned
    // position since it's expected this function returns a global position.
    let line_index = text.char_to_line(pos);
    let pos_delta = text.line_to_char(line_index);
    let pos = pos - pos_delta;
    search::find_nth_next(text.line(line_index), char_matcher, pos, 1).map(|pos| pos + pos_delta)
}

/// Decrement object under cursor by `amount`.
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(
        cx.editor,
        &find_next_char_until_newline,
        true,
        true,
        char::is_ascii_digit,
        1,
    );

    // Increase by 1 if `IncrementDirection` is `Increase`
    // Decrease by 1 if `IncrementDirection` is `Decrease`
    let sign = match increment_direction {
        IncrementDirection::Increase => 1,
        IncrementDirection::Decrease => -1,
    };
    let mut amount = sign * cx.count() as i64;

    // If the register is `#` then increase or decrease the `amount` by 1 per element
    let increase_by = if cx.register == Some('#') { sign } else { 0 };

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