Skip to content

Commit

Permalink
feat: add buffer-close-right and buffer-close-left commands
Browse files Browse the repository at this point in the history
  • Loading branch information
atomicptr committed Mar 3, 2025
1 parent 1a28999 commit 91cc9ff
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
| `:buffer-close-others!`, `:bco!`, `:bcloseother!` | Force close all buffers but 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-close-right`, `:bcr`, `:bcloseright` | Close all to the right buffers without quitting. |
| `:buffer-close-right!`, `:bcr!`, `:bcloseright!` | Force close all buffers to the right ignoring unsaved changes without quitting. |
| `:buffer-close-left`, `:bcl`, `:bcloseleft` | Close all to the left buffers without quitting. |
| `:buffer-close-left!`, `:bcl!`, `:bcloseleft!` | Force close all buffers to the left ignoring unsaved changes without quitting. |
| `:buffer-next`, `:bn`, `:bnext` | Goto next buffer. |
| `:buffer-previous`, `:bp`, `:bprev` | Goto previous buffer. |
| `:write`, `:w` | Write changes to disk. Accepts an optional path (:write some/path.txt) |
Expand Down
115 changes: 115 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,77 @@ fn force_buffer_close_all(
buffer_close_by_ids_impl(cx, &document_ids, true)
}

fn buffer_gather_right_impl(editor: &mut Editor) -> Vec<DocumentId> {
let current_document = &doc!(editor).id();
editor
.documents()
.map(|doc| doc.id())
.skip_while(|doc| doc != current_document)
.skip(1)
.collect()
}

fn buffer_close_right(
cx: &mut compositor::Context,
_args: Args,
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let document_ids = buffer_gather_right_impl(cx.editor);
buffer_close_by_ids_impl(cx, &document_ids, false)
}

fn force_buffer_close_right(
cx: &mut compositor::Context,
_args: Args,
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let document_ids = buffer_gather_right_impl(cx.editor);
buffer_close_by_ids_impl(cx, &document_ids, true)
}

fn buffer_gather_left_impl(editor: &mut Editor) -> Vec<DocumentId> {
let current_document = &doc!(editor).id();
editor
.documents()
.map(|doc| doc.id())
.take_while(|doc| doc != current_document)
.collect()
}

fn buffer_close_left(
cx: &mut compositor::Context,
_args: Args,
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let document_ids = buffer_gather_left_impl(cx.editor);
buffer_close_by_ids_impl(cx, &document_ids, false)
}

fn force_buffer_close_left(
cx: &mut compositor::Context,
_args: Args,
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let document_ids = buffer_gather_left_impl(cx.editor);
buffer_close_by_ids_impl(cx, &document_ids, true)
}

fn buffer_next(
cx: &mut compositor::Context,
_args: Args,
Expand Down Expand Up @@ -2613,6 +2684,50 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
..Signature::DEFAULT
},
},
TypableCommand {
name: "buffer-close-right",
aliases: &["bcr", "bcloseright"],
doc: "Close all to the right buffers without quitting.",
fun: buffer_close_right,
completer: CommandCompleter::none(),
signature: Signature {
positionals: (0, Some(0)),
..Signature::DEFAULT
},
},
TypableCommand {
name: "buffer-close-right!",
aliases: &["bcr!", "bcloseright!"],
doc: "Force close all buffers to the right ignoring unsaved changes without quitting.",
fun: force_buffer_close_right,
completer: CommandCompleter::none(),
signature: Signature {
positionals: (0, Some(0)),
..Signature::DEFAULT
},
},
TypableCommand {
name: "buffer-close-left",
aliases: &["bcl", "bcloseleft"],
doc: "Close all to the left buffers without quitting.",
fun: buffer_close_left,
completer: CommandCompleter::none(),
signature: Signature {
positionals: (0, Some(0)),
..Signature::DEFAULT
},
},
TypableCommand {
name: "buffer-close-left!",
aliases: &["bcl!", "bcloseleft!"],
doc: "Force close all buffers to the left ignoring unsaved changes without quitting.",
fun: force_buffer_close_left,
completer: CommandCompleter::none(),
signature: Signature {
positionals: (0, Some(0)),
..Signature::DEFAULT
},
},
TypableCommand {
name: "buffer-next",
aliases: &["bn", "bnext"],
Expand Down

0 comments on commit 91cc9ff

Please sign in to comment.