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

Add buffer-close-left, buffer-close-right, and force versions #12229

Open
wants to merge 2 commits 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 @@ -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. |
Expand Down
110 changes: 102 additions & 8 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentId> {
enum OtherBuffers {
All,
Left,
Right,
}

fn buffer_gather_others_impl(editor: &mut Editor, sel: OtherBuffers) -> Vec<DocumentId> {
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<str>],
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<str>],
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<str>],
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<str>],
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(
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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"],
Expand Down
Loading