on master help sign in
Implement "Goto next buffer / Goto previous buffer" commands
commit78c68fae91579ccda6f65e55f79316b01c5b654a authorath3 <ha05190@protonmail.com> dateMon 1 Nov 2021, 20:52 (5 years ago) parente2560f4
3 files changed+33
book/src/keymap.md+2 helix-term/src/commands.rs+29 helix-term/src/keymap.rs+2
book/src/keymap.md+2
@@ -161,6 +161,8 @@ Jumps to various locations.
161161| `r` | Go to references | `goto_reference` |
162162| `i` | Go to implementation | `goto_implementation` |
163163| `a` | Go to the last accessed/alternate file | `goto_last_accessed_file` |
164+| `n` | Go to next buffer | `goto_next_buffer` |
165+| `p` | Go to previous buffer | `goto_previous_buffer` |
164166
165167#### Match mode
166168
helix-term/src/commands.rs+29
@@ -262,6 +262,8 @@ impl Command {
262262 goto_prev_diag, "Goto previous diagnostic",
263263 goto_line_start, "Goto line start",
264264 goto_line_end, "Goto line end",
265+ goto_next_buffer, "Goto next buffer",
266+ goto_previous_buffer, "Goto previous buffer",
265267 // TODO: different description ?
266268 goto_line_end_newline, "Goto line end",
267269 goto_first_nonwhitespace, "Goto first non-blank in line",
@@ -519,6 +521,33 @@ fn goto_line_start(cx: &mut Context) {
519521 )
520522}
521523
524+fn goto_next_buffer(cx: &mut Context) {
525+ goto_buffer(cx, Direction::Forward);
526+}
527+
528+fn goto_previous_buffer(cx: &mut Context) {
529+ goto_buffer(cx, Direction::Backward);
530+}
531+
532+fn goto_buffer(cx: &mut Context, direction: Direction) {
533+ let buf_cur = current!(cx.editor).1.id();
534+
535+ if let Some(pos) = cx.editor.documents.iter().position(|(id, _)| id == buf_cur) {
536+ let goto_id = if direction == Direction::Forward {
537+ if pos < cx.editor.documents.iter().count() - 1 {
538+ cx.editor.documents.iter().nth(pos + 1).unwrap().0
539+ } else {
540+ cx.editor.documents.iter().next().unwrap().0
541+ }
542+ } else if pos > 0 {
543+ cx.editor.documents.iter().nth(pos - 1).unwrap().0
544+ } else {
545+ cx.editor.documents.iter().last().unwrap().0
546+ };
547+ cx.editor.switch(goto_id, Action::Replace);
548+ }
549+}
550+
522551fn extend_to_line_start(cx: &mut Context) {
523552 let (view, doc) = current!(cx.editor);
524553 goto_line_start_impl(view, doc, Movement::Extend)
helix-term/src/keymap.rs+2
@@ -453,6 +453,8 @@ impl Default for Keymaps {
453453 "m" => goto_window_middle,
454454 "b" => goto_window_bottom,
455455 "a" => goto_last_accessed_file,
456+ "n" => goto_next_buffer,
457+ "p" => goto_previous_buffer,
456458 },
457459 ":" => command_mode,
458460