on master help sign in
Add typed commands buffer-next and buffer-previous (#1940)
commit9782204f739813ab5b2dd82451ef2d4695b41d22 authorDr. David A. Kunz <david.kunz@sap.com> dateSun 3 Apr 2022, 14:26 (5 years ago) parentec21de0
3 files changed+43−10
book/src/generated/typable-cmd.md+2 helix-term/src/commands.rs+9−10 helix-term/src/commands/typed.rs+32
book/src/generated/typable-cmd.md+2
@@ -9,6 +9,8 @@
99| `:buffer-close-others!`, `:bco!`, `:bcloseother!` | Close all buffers but the currently focused one. |
1010| `:buffer-close-all`, `:bca`, `:bcloseall` | Close all buffers, without quiting. |
1111| `:buffer-close-all!`, `:bca!`, `:bcloseall!` | Close all buffers forcefully (ignoring unsaved changes), without quiting. |
12+| `:buffer-next`, `:bn`, `:bnext` | Go to next buffer. |
13+| `:buffer-previous`, `:bp`, `:bprev` | Go to previous buffer. |
1214| `:write`, `:w` | Write changes to disk. Accepts an optional path (:write some/path.txt) |
1315| `:new`, `:n` | Create a new scratch buffer. |
1416| `:format`, `:fmt` | Format the file using the LSP formatter. |
helix-term/src/commands.rs+9−10
@@ -637,36 +637,35 @@ fn goto_line_start(cx: &mut Context) {
637637}
638638
639639fn goto_next_buffer(cx: &mut Context) {
640− goto_buffer(cx, Direction::Forward);
640+ goto_buffer(cx.editor, Direction::Forward);
641641}
642642
643643fn goto_previous_buffer(cx: &mut Context) {
644− goto_buffer(cx, Direction::Backward);
644+ goto_buffer(cx.editor, Direction::Backward);
645645}
646646
647−fn goto_buffer(cx: &mut Context, direction: Direction) {
648− let current = view!(cx.editor).doc;
647+fn goto_buffer(editor: &mut Editor, direction: Direction) {
648+ let current = view!(editor).doc;
649649
650650 let id = match direction {
651651 Direction::Forward => {
652− let iter = cx.editor.documents.keys();
652+ let iter = editor.documents.keys();
653653 let mut iter = iter.skip_while(|id| *id != &current);
654654 iter.next(); // skip current item
655− iter.next().or_else(|| cx.editor.documents.keys().next())
655+ iter.next().or_else(|| editor.documents.keys().next())
656656 }
657657 Direction::Backward => {
658− let iter = cx.editor.documents.keys();
658+ let iter = editor.documents.keys();
659659 let mut iter = iter.rev().skip_while(|id| *id != &current);
660660 iter.next(); // skip current item
661− iter.next()
662− .or_else(|| cx.editor.documents.keys().rev().next())
661+ iter.next().or_else(|| editor.documents.keys().rev().next())
663662 }
664663 }
665664 .unwrap();
666665
667666 let id = *id;
668667
669− cx.editor.switch(id, Action::Replace);
668+ editor.switch(id, Action::Replace);
670669}
671670
672671fn extend_to_line_start(cx: &mut Context) {
helix-term/src/commands/typed.rs+32
@@ -172,6 +172,24 @@ fn force_buffer_close_all(
172172 buffer_close_by_ids_impl(cx.editor, &document_ids, true)
173173}
174174
175+fn buffer_next(
176+ cx: &mut compositor::Context,
177+ _args: &[Cow<str>],
178+ _event: PromptEvent,
179+) -> anyhow::Result<()> {
180+ goto_buffer(cx.editor, Direction::Forward);
181+ Ok(())
182+}
183+
184+fn buffer_previous(
185+ cx: &mut compositor::Context,
186+ _args: &[Cow<str>],
187+ _event: PromptEvent,
188+) -> anyhow::Result<()> {
189+ goto_buffer(cx.editor, Direction::Backward);
190+ Ok(())
191+}
192+
175193fn write_impl(cx: &mut compositor::Context, path: Option<&Cow<str>>) -> anyhow::Result<()> {
176194 let jobs = &mut cx.jobs;
177195 let doc = doc_mut!(cx.editor);
@@ -1082,6 +1100,20 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
10821100 fun: force_buffer_close_all,
10831101 completer: None,
10841102 },
1103+ TypableCommand {
1104+ name: "buffer-next",
1105+ aliases: &["bn", "bnext"],
1106+ doc: "Go to next buffer.",
1107+ fun: buffer_next,
1108+ completer: None,
1109+ },
1110+ TypableCommand {
1111+ name: "buffer-previous",
1112+ aliases: &["bp", "bprev"],
1113+ doc: "Go to previous buffer.",
1114+ fun: buffer_previous,
1115+ completer: None,
1116+ },
10851117 TypableCommand {
10861118 name: "write",
10871119 aliases: &["w"],