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 command so that we can bind key to switch buffers #4771

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
| `:quit`, `:q` | Close the current view. |
| `:quit!`, `:q!` | Force close the current view, ignoring unsaved changes. |
| `:open`, `:o` | Open a file from disk into the current view. |
| `:buffer`, `:b` | Go to a buffer |
| `:buffer-close`, `:bc`, `:bclose` | Close the current buffer. |
| `: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. |
Expand Down
35 changes: 35 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ fn buffer_gather_paths_impl(editor: &mut Editor, args: &[Cow<str>]) -> Vec<Docum
document_ids
}

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

let name = args.first().unwrap().as_ref();
let arg_path = Some(Path::new(name));
let doc_id = cx.editor.documents().find_map(|doc| {
if doc.path().map(|p| p.as_path()) == arg_path
|| doc.relative_path().as_deref() == arg_path
|| doc.display_name() == name
|| format!("{}", doc.id()) == name
{
Some(doc.id())
} else {
None
}
});
if let Some(document_id) = doc_id {
cx.editor.switch(document_id, Action::Replace);
}
Ok(())
}

fn buffer_close(
cx: &mut compositor::Context,
args: &[Cow<str>],
Expand Down Expand Up @@ -1761,6 +1789,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: open,
completer: Some(completers::filename),
},
TypableCommand {
name: "buffer",
aliases: &["b"],
doc: "Go to a buffer",
fun: buffer,
completer: Some(completers::buffer),
},
TypableCommand {
name: "buffer-close",
aliases: &["bc", "bclose"],
Expand Down