Skip to content

Commit

Permalink
feat: add buffer-close-hidden commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoohey31 committed Jan 4, 2023
1 parent 0dbee95 commit d9aef1d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
2 changes: 2 additions & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
| `: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-hidden`, `:bch`, `:bclosehidden` | Close all buffers that are not visible. |
| `:buffer-close-hidden!`, `:bch!`, `:bclosehidden!` | Force close all buffers that are not visible ignoring unsaved changes. |
| `: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
79 changes: 68 additions & 11 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ fn open(cx: &mut compositor::Context, args: &[Cow<str>], event: PromptEvent) ->
Ok(())
}

fn buffer_close_by_ids_impl(
fn buffer_close_by_ids_impl<T>(
cx: &mut compositor::Context,
doc_ids: &[DocumentId],
doc_ids: T,
force: bool,
) -> anyhow::Result<()> {
) -> anyhow::Result<()>
where
T: IntoIterator<Item = DocumentId>,
{
cx.block_try_flush_writes()?;

let (modified_ids, modified_names): (Vec<_>, Vec<_>) = doc_ids
.iter()
.filter_map(|&doc_id| {
.into_iter()
.filter_map(|doc_id| {
if let Err(CloseError::BufferModified(name)) = cx.editor.close_document(doc_id, force) {
Some((doc_id, name))
} else {
Expand Down Expand Up @@ -174,7 +177,7 @@ fn buffer_close(
}

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

fn force_buffer_close(
Expand All @@ -187,7 +190,7 @@ fn force_buffer_close(
}

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

fn buffer_gather_others_impl(editor: &mut Editor) -> Vec<DocumentId> {
Expand All @@ -209,7 +212,7 @@ fn buffer_close_others(
}

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

fn force_buffer_close_others(
Expand All @@ -222,7 +225,7 @@ fn force_buffer_close_others(
}

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

fn buffer_gather_all_impl(editor: &mut Editor) -> Vec<DocumentId> {
Expand All @@ -239,7 +242,7 @@ fn buffer_close_all(
}

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

fn force_buffer_close_all(
Expand All @@ -252,7 +255,47 @@ fn force_buffer_close_all(
}

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

fn buffer_close_hidden(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let mut visible_doc_ids = cx
.editor
.documents()
.map(|doc| doc.id())
.collect::<HashSet<_>>();
for view in cx.editor.tree.views() {
visible_doc_ids.remove(&view.0.doc);
}
buffer_close_by_ids_impl(cx, visible_doc_ids, false)
}

fn force_buffer_close_hidden(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let mut visible_doc_ids = cx
.editor
.documents()
.map(|doc| doc.id())
.collect::<HashSet<_>>();
for view in cx.editor.tree.views() {
visible_doc_ids.remove(&view.0.doc);
}
buffer_close_by_ids_impl(cx, visible_doc_ids, true)
}

fn buffer_next(
Expand Down Expand Up @@ -1889,6 +1932,20 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: force_buffer_close_all,
completer: None,
},
TypableCommand {
name: "buffer-close-hidden",
aliases: &["bch", "bclosehidden"],
doc: "Close all buffers that are not visible.",
fun: buffer_close_hidden,
completer: None,
},
TypableCommand {
name: "buffer-close-hidden!",
aliases: &["bch!", "bclosehidden!"],
doc: "Force close all buffers that are not visible ignoring unsaved changes.",
fun: force_buffer_close_hidden,
completer: None,
},
TypableCommand {
name: "buffer-next",
aliases: &["bn", "bnext"],
Expand Down

0 comments on commit d9aef1d

Please sign in to comment.