Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add buffer-close-right and buffer-close-left commands #13009

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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