Skip to content

Commit

Permalink
Implement "Goto next buffer / Goto previous buffer" commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ath3 committed Nov 3, 2021
1 parent e5de103 commit 05706f4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ Jumps to various locations.
| `r` | Go to references | `goto_reference` |
| `i` | Go to implementation | `goto_implementation` |
| `a` | Go to the last accessed/alternate file | `goto_last_accessed_file` |
| `n` | Go to next buffer | `goto_next_buffer` |
| `p` | Go to previous buffer | `goto_previous_buffer` |

#### Match mode

Expand Down
29 changes: 29 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ impl Command {
goto_prev_diag, "Goto previous diagnostic",
goto_line_start, "Goto line start",
goto_line_end, "Goto line end",
goto_next_buffer, "Goto next buffer",
goto_previous_buffer, "Goto previous buffer",
// TODO: different description ?
goto_line_end_newline, "Goto line end",
goto_first_nonwhitespace, "Goto first non-blank in line",
Expand Down Expand Up @@ -519,6 +521,33 @@ fn goto_line_start(cx: &mut Context) {
)
}

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

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

fn goto_buffer(cx: &mut Context, direction: Direction) {
let buf_cur = current!(cx.editor).1.id();

if let Some(pos) = cx.editor.documents.iter().position(|(id, _)| id == buf_cur) {
let goto_id = if direction == Direction::Forward {
if pos < cx.editor.documents.iter().count() - 1 {
cx.editor.documents.iter().nth(pos + 1).unwrap().0
} else {
cx.editor.documents.iter().next().unwrap().0
}
} else if pos > 0 {
cx.editor.documents.iter().nth(pos - 1).unwrap().0
} else {
cx.editor.documents.iter().last().unwrap().0
};
cx.editor.switch(goto_id, Action::Replace);
}
}

fn extend_to_line_start(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
goto_line_start_impl(view, doc, Movement::Extend)
Expand Down
2 changes: 2 additions & 0 deletions helix-term/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ impl Default for Keymaps {
"m" => goto_window_middle,
"b" => goto_window_bottom,
"a" => goto_last_accessed_file,
"n" => goto_next_buffer,
"p" => goto_previous_buffer,
},
":" => command_mode,

Expand Down

0 comments on commit 05706f4

Please sign in to comment.