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

Goto buffer by num #8198

Closed
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
3 changes: 3 additions & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
| `:buffer-close-all!`, `:bca!`, `:bcloseall!` | Force close all buffers ignoring unsaved changes without quitting. |
| `:buffer-next`, `:bn`, `:bnext` | Goto next buffer. |
| `:buffer-previous`, `:bp`, `:bprev` | Goto previous buffer. |
| `:buffer-index`, `:bi` | Goto buffer by number <n>. |
| `:buffer-first`, `:bf` | Goto first buffer. |
| `:buffer-last`, `:bl` | Goto last buffer. |
| `:write`, `:w` | Write changes to disk. Accepts an optional path (:write some/path.txt) |
| `:write!`, `:w!` | Force write changes to disk creating necessary subdirectories. Accepts an optional path (:write! some/path.txt) |
| `:write-buffer-close`, `:wbc` | Write changes to disk and closes the buffer. Accepts an optional path (:write-buffer-close some/path.txt) |
Expand Down
76 changes: 76 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,61 @@ fn buffer_previous(
Ok(())
}

fn buffer_first(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
match cx.editor.documents.keys().nth(0) {
Some(doc_id) => cx.editor.switch(*doc_id, Action::Replace),
None => bail!("The first buffer not found"),
}
Ok(())
}

fn buffer_last(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
match cx.editor.documents.keys().last() {
Some(doc_id) => cx.editor.switch(*doc_id, Action::Replace),
None => bail!("The last buffer not found"),
}
Ok(())
}
fn buffer_index(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
if args.is_empty() {
bail!("At least one index must be provided");
} else if args.len() > 1 {
bail!("Only one index must be provided");
}
let mut index = args[0].parse::<usize>()?;
if index == 0 {
bail!("The index must be greater than 0");
}

let mut document_keys = cx.editor.documents.keys();
if index > document_keys.len() {
index = document_keys.len()
}
let doc_id = document_keys.nth(index - 1).unwrap();
cx.editor.switch(*doc_id, Action::Replace);
Ok(())
}
fn write_impl(
cx: &mut compositor::Context,
path: Option<&Cow<str>>,
Expand Down Expand Up @@ -2369,6 +2424,27 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: buffer_previous,
signature: CommandSignature::none(),
},
TypableCommand {
name: "buffer-index",
aliases: &["bi"],
doc: "Goto buffer by number <n>.",
fun: buffer_index,
signature: CommandSignature::none(),
},
TypableCommand {
name: "buffer-first",
aliases: &["bf"],
doc: "Goto first buffer.",
fun: buffer_first,
signature: CommandSignature::none(),
},
TypableCommand {
name: "buffer-last",
aliases: &["bl"],
doc: "Goto last buffer.",
fun: buffer_last,
signature: CommandSignature::none(),
},
TypableCommand {
name: "write",
aliases: &["w"],
Expand Down