mirror of https://github.com/helix-editor/helix
feat: add :buffer-close-others --skip-visible flag (#5393)
parent
2f560914fb
commit
7e1fbb05fd
|
@ -230,38 +230,51 @@ fn force_buffer_close(
|
||||||
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> {
|
fn buffer_gather_others_impl(editor: &mut Editor, skip_visible: bool) -> Vec<DocumentId> {
|
||||||
|
if skip_visible {
|
||||||
|
let visible_document_ids = editor
|
||||||
|
.tree
|
||||||
|
.views()
|
||||||
|
.map(|view| &view.0.doc)
|
||||||
|
.collect::<HashSet<_>>();
|
||||||
|
editor
|
||||||
|
.documents()
|
||||||
|
.map(|doc| doc.id())
|
||||||
|
.filter(|doc_id| !visible_document_ids.contains(doc_id))
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
let current_document = &doc!(editor).id();
|
let current_document = &doc!(editor).id();
|
||||||
editor
|
editor
|
||||||
.documents()
|
.documents()
|
||||||
.map(|doc| doc.id())
|
.map(|doc| doc.id())
|
||||||
.filter(|doc_id| doc_id != current_document)
|
.filter(|doc_id| doc_id != current_document)
|
||||||
.collect()
|
.collect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn buffer_close_others(
|
fn buffer_close_others(
|
||||||
cx: &mut compositor::Context,
|
cx: &mut compositor::Context,
|
||||||
_args: Args,
|
args: Args,
|
||||||
event: PromptEvent,
|
event: PromptEvent,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
if event != PromptEvent::Validate {
|
if event != PromptEvent::Validate {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let document_ids = buffer_gather_others_impl(cx.editor);
|
let document_ids = buffer_gather_others_impl(cx.editor, args.has_flag("skip-visible"));
|
||||||
buffer_close_by_ids_impl(cx, &document_ids, false)
|
buffer_close_by_ids_impl(cx, &document_ids, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn force_buffer_close_others(
|
fn force_buffer_close_others(
|
||||||
cx: &mut compositor::Context,
|
cx: &mut compositor::Context,
|
||||||
_args: Args,
|
args: Args,
|
||||||
event: PromptEvent,
|
event: PromptEvent,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
if event != PromptEvent::Validate {
|
if event != PromptEvent::Validate {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let document_ids = buffer_gather_others_impl(cx.editor);
|
let document_ids = buffer_gather_others_impl(cx.editor, args.has_flag("skip-visible"));
|
||||||
buffer_close_by_ids_impl(cx, &document_ids, true)
|
buffer_close_by_ids_impl(cx, &document_ids, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2623,6 +2636,18 @@ fn noop(_cx: &mut compositor::Context, _args: Args, _event: PromptEvent) -> anyh
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This command accepts a single boolean --skip-visible flag and no positionals.
|
||||||
|
const BUFFER_CLOSE_OTHERS_SIGNATURE: Signature = Signature {
|
||||||
|
positionals: (0, Some(0)),
|
||||||
|
flags: &[Flag {
|
||||||
|
name: "skip-visible",
|
||||||
|
alias: Some('s'),
|
||||||
|
doc: "don't close buffers that are visible",
|
||||||
|
..Flag::DEFAULT
|
||||||
|
}],
|
||||||
|
..Signature::DEFAULT
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: SHELL_SIGNATURE should specify var args for arguments, so that just completers::filename can be used,
|
// TODO: SHELL_SIGNATURE should specify var args for arguments, so that just completers::filename can be used,
|
||||||
// but Signature does not yet allow for var args.
|
// but Signature does not yet allow for var args.
|
||||||
|
|
||||||
|
@ -2708,10 +2733,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
|
||||||
doc: "Close all buffers but the currently focused one.",
|
doc: "Close all buffers but the currently focused one.",
|
||||||
fun: buffer_close_others,
|
fun: buffer_close_others,
|
||||||
completer: CommandCompleter::none(),
|
completer: CommandCompleter::none(),
|
||||||
signature: Signature {
|
signature: BUFFER_CLOSE_OTHERS_SIGNATURE,
|
||||||
positionals: (0, Some(0)),
|
|
||||||
..Signature::DEFAULT
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
TypableCommand {
|
TypableCommand {
|
||||||
name: "buffer-close-others!",
|
name: "buffer-close-others!",
|
||||||
|
@ -2719,10 +2741,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
|
||||||
doc: "Force close all buffers but the currently focused one.",
|
doc: "Force close all buffers but the currently focused one.",
|
||||||
fun: force_buffer_close_others,
|
fun: force_buffer_close_others,
|
||||||
completer: CommandCompleter::none(),
|
completer: CommandCompleter::none(),
|
||||||
signature: Signature {
|
signature: BUFFER_CLOSE_OTHERS_SIGNATURE,
|
||||||
positionals: (0, Some(0)),
|
|
||||||
..Signature::DEFAULT
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
TypableCommand {
|
TypableCommand {
|
||||||
name: "buffer-close-all",
|
name: "buffer-close-all",
|
||||||
|
|
Loading…
Reference in New Issue