From 80e9e3ddecfd7aeaecde275adfced428f547daf6 Mon Sep 17 00:00:00 2001 From: John Kelly Date: Sun, 8 Dec 2024 18:55:30 +0000 Subject: [PATCH 1/2] Add `buffer-close-left`, `buffer-close-right`, and `force` versions Closes all buffers to the left/right of the current buffer. --- helix-term/src/commands/typed.rs | 110 ++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 8 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 49864abb683e..8e3bb679eb40 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -238,13 +238,79 @@ fn force_buffer_close( buffer_close_by_ids_impl(cx, &document_ids, true) } -fn buffer_gather_others_impl(editor: &mut Editor) -> Vec { +enum OtherBuffers { + All, + Left, + Right, +} + +fn buffer_gather_others_impl(editor: &mut Editor, sel: OtherBuffers) -> Vec { let current_document = &doc!(editor).id(); - editor - .documents() - .map(|doc| doc.id()) - .filter(|doc_id| doc_id != current_document) - .collect() + + let ids = editor.documents().map(|doc| doc.id()); + + match sel { + OtherBuffers::All => ids.filter(|doc_id| doc_id != current_document).collect(), + OtherBuffers::Left => ids + .take_while(|doc_id| doc_id != current_document) + .collect(), + OtherBuffers::Right => ids + .skip_while(|doc_id| doc_id != current_document) + .skip(1) + .collect(), + } +} + +fn buffer_close_right( + cx: &mut compositor::Context, + _args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + let document_ids = buffer_gather_others_impl(cx.editor, OtherBuffers::Right); + buffer_close_by_ids_impl(cx, &document_ids, false) +} + +fn force_buffer_close_right( + cx: &mut compositor::Context, + _args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + let document_ids = buffer_gather_others_impl(cx.editor, OtherBuffers::Right); + buffer_close_by_ids_impl(cx, &document_ids, true) +} + +fn buffer_close_left( + cx: &mut compositor::Context, + _args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + let document_ids = buffer_gather_others_impl(cx.editor, OtherBuffers::Left); + buffer_close_by_ids_impl(cx, &document_ids, false) +} + +fn force_buffer_close_left( + cx: &mut compositor::Context, + _args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + let document_ids = buffer_gather_others_impl(cx.editor, OtherBuffers::Left); + buffer_close_by_ids_impl(cx, &document_ids, true) } fn buffer_close_others( @@ -256,7 +322,7 @@ fn buffer_close_others( return Ok(()); } - let document_ids = buffer_gather_others_impl(cx.editor); + let document_ids = buffer_gather_others_impl(cx.editor, OtherBuffers::All); buffer_close_by_ids_impl(cx, &document_ids, false) } @@ -269,7 +335,7 @@ fn force_buffer_close_others( return Ok(()); } - let document_ids = buffer_gather_others_impl(cx.editor); + let document_ids = buffer_gather_others_impl(cx.editor, OtherBuffers::All); buffer_close_by_ids_impl(cx, &document_ids, true) } @@ -2578,6 +2644,34 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: force_buffer_close_others, signature: CommandSignature::none(), }, + TypableCommand { + name: "buffer-close-left", + aliases: &["bcl", "bcloseleft"], + doc: "Close all buffers to the left of the currently focused one.", + fun: buffer_close_left, + signature: CommandSignature::none(), + }, + TypableCommand { + name: "buffer-close-left!", + aliases: &["bcl!", "bcloseleft!"], + doc: "Force close all buffers to the left of the currently focused one.", + fun: force_buffer_close_left, + signature: CommandSignature::none(), + }, + TypableCommand { + name: "buffer-close-right", + aliases: &["bcr", "bcloseright"], + doc: "Close all buffers to the right of the currently focused one.", + fun: buffer_close_right, + signature: CommandSignature::none(), + }, + TypableCommand { + name: "buffer-close-right!", + aliases: &["bcr!", "bcloseright!"], + doc: "Force close all buffers to the right of the currently focused one.", + fun: force_buffer_close_right, + signature: CommandSignature::none(), + }, TypableCommand { name: "buffer-close-all", aliases: &["bca", "bcloseall"], From c12657ff37f75bcb00b88517ee47657f9ffa3fec Mon Sep 17 00:00:00 2001 From: John Kelly Date: Fri, 3 Jan 2025 11:07:39 +0000 Subject: [PATCH 2/2] Run docgen --- book/src/generated/typable-cmd.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index f0d9a0f492a5..64b213a69059 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -7,6 +7,10 @@ | `:buffer-close!`, `:bc!`, `:bclose!` | Close the current buffer forcefully, ignoring unsaved changes. | | `:buffer-close-others`, `:bco`, `:bcloseother` | Close all buffers but the currently focused one. | | `:buffer-close-others!`, `:bco!`, `:bcloseother!` | Force close all buffers but the currently focused one. | +| `:buffer-close-left`, `:bcl`, `:bcloseleft` | Close all buffers to the left of the currently focused one. | +| `:buffer-close-left!`, `:bcl!`, `:bcloseleft!` | Force close all buffers to the left of the currently focused one. | +| `:buffer-close-right`, `:bcr`, `:bcloseright` | Close all buffers to the right of the currently focused one. | +| `:buffer-close-right!`, `:bcr!`, `:bcloseright!` | Force close all buffers to the right of the currently focused one. | | `:buffer-close-all`, `:bca`, `:bcloseall` | Close all buffers without quitting. | | `:buffer-close-all!`, `:bca!`, `:bcloseall!` | Force close all buffers ignoring unsaved changes without quitting. | | `:buffer-next`, `:bn`, `:bnext` | Goto next buffer. |