Skip to content

Commit

Permalink
helix-term/commands: use buffer completer for buffer_close
Browse files Browse the repository at this point in the history
  • Loading branch information
cole-h committed Nov 20, 2021
1 parent 09e89de commit 99badb5
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,26 +1787,50 @@ mod cmd {
Ok(())
}

fn buffer_close_impl(editor: &mut Editor, args: &[&str], force: bool) -> anyhow::Result<()> {
if args.is_empty() {
let doc_id = view!(editor).doc;
editor.close_document(doc_id, force)?;
return Ok(());
}

for arg in args {
let doc_id = editor.documents().find_map(|doc| {
let arg_path = Some(Path::new(arg));
if doc.path().map(|p| p.as_path()) == arg_path
|| doc.relative_path().as_deref() == arg_path
{
Some(doc.id())
} else {
None
}
});

match doc_id {
Some(doc_id) => editor.close_document(doc_id, force)?,
None => {
editor.set_error(format!("couldn't close buffer '{}': does not exist", arg));
}
}
}

Ok(())
}

fn buffer_close(
cx: &mut compositor::Context,
_args: &[&str],
args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
let view = view!(cx.editor);
let doc_id = view.doc;
cx.editor.close_document(doc_id, false)?;
Ok(())
buffer_close_impl(cx.editor, args, false)
}

fn force_buffer_close(
cx: &mut compositor::Context,
_args: &[&str],
args: &[&str],
_event: PromptEvent,
) -> anyhow::Result<()> {
let view = view!(cx.editor);
let doc_id = view.doc;
cx.editor.close_document(doc_id, true)?;
Ok(())
buffer_close_impl(cx.editor, args, true)
}

fn write_impl<P: AsRef<Path>>(
Expand Down Expand Up @@ -2414,16 +2438,16 @@ mod cmd {
TypableCommand {
name: "buffer-close",
aliases: &["bc", "bclose"],
doc: "Close the current buffer.",
doc: "Close a buffer. Defaults to the current buffer.",
fun: buffer_close,
completer: None, // FIXME: buffer completer
completer: Some(completers::buffer),
},
TypableCommand {
name: "buffer-close!",
aliases: &["bc!", "bclose!"],
doc: "Close the current buffer forcefully (ignoring unsaved changes).",
doc: "Close a buffer forcefully (ignoring unsaved changes). Defaults to the current buffer.",
fun: force_buffer_close,
completer: None, // FIXME: buffer completer
completer: Some(completers::buffer),
},
TypableCommand {
name: "write",
Expand Down

0 comments on commit 99badb5

Please sign in to comment.