Skip to content

Commit

Permalink
Enabled traversing multiple buffers at once (helix-editor#10463)
Browse files Browse the repository at this point in the history
* Enable traversing multiple buffers at once

* run cargo fmt

* simplify iterator call
  • Loading branch information
mjbozo authored and postsolar committed Apr 20, 2024
1 parent 54be1ef commit 1e94885
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 10 additions & 9 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,28 +809,29 @@ fn goto_line_start(cx: &mut Context) {
}

fn goto_next_buffer(cx: &mut Context) {
goto_buffer(cx.editor, Direction::Forward);
goto_buffer(cx.editor, Direction::Forward, cx.count());
}

fn goto_previous_buffer(cx: &mut Context) {
goto_buffer(cx.editor, Direction::Backward);
goto_buffer(cx.editor, Direction::Backward, cx.count());
}

fn goto_buffer(editor: &mut Editor, direction: Direction) {
fn goto_buffer(editor: &mut Editor, direction: Direction, count: usize) {
let current = view!(editor).doc;

let id = match direction {
Direction::Forward => {
let iter = editor.documents.keys();
let mut iter = iter.skip_while(|id| *id != &current);
iter.next(); // skip current item
iter.next().or_else(|| editor.documents.keys().next())
// skip 'count' times past current buffer
iter.cycle().skip_while(|id| *id != &current).nth(count)
}
Direction::Backward => {
let iter = editor.documents.keys();
let mut iter = iter.rev().skip_while(|id| *id != &current);
iter.next(); // skip current item
iter.next().or_else(|| editor.documents.keys().next_back())
// skip 'count' times past current buffer
iter.rev()
.cycle()
.skip_while(|id| *id != &current)
.nth(count)
}
}
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ fn buffer_next(
return Ok(());
}

goto_buffer(cx.editor, Direction::Forward);
goto_buffer(cx.editor, Direction::Forward, 1);
Ok(())
}

Expand All @@ -322,7 +322,7 @@ fn buffer_previous(
return Ok(());
}

goto_buffer(cx.editor, Direction::Backward);
goto_buffer(cx.editor, Direction::Backward, 1);
Ok(())
}

Expand Down

0 comments on commit 1e94885

Please sign in to comment.