mirror of https://github.com/helix-editor/helix
Add `:<line>` and `:goto <line>` commands (#1128)
* Add typable `goto` command * Support `:<line-number>` on prompt * Rename function according to convention * Directly call into goto_line_number functionpull/1158/head
parent
21143e8d22
commit
57c14d4a93
|
@ -2460,6 +2460,22 @@ mod cmd {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn goto_line_number(
|
||||||
|
cx: &mut compositor::Context,
|
||||||
|
args: &[&str],
|
||||||
|
_event: PromptEvent,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let line = args[0].parse::<usize>()?;
|
||||||
|
|
||||||
|
goto_line_impl(&mut cx.editor, NonZeroUsize::new(line));
|
||||||
|
|
||||||
|
let (view, doc) = current!(cx.editor);
|
||||||
|
|
||||||
|
view.ensure_cursor_in_view(doc, line);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
|
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
|
||||||
TypableCommand {
|
TypableCommand {
|
||||||
name: "quit",
|
name: "quit",
|
||||||
|
@ -2741,6 +2757,13 @@ mod cmd {
|
||||||
fun: tutor,
|
fun: tutor,
|
||||||
completer: None,
|
completer: None,
|
||||||
},
|
},
|
||||||
|
TypableCommand {
|
||||||
|
name: "goto",
|
||||||
|
aliases: &["g"],
|
||||||
|
doc: "Go to line number.",
|
||||||
|
fun: goto_line_number,
|
||||||
|
completer: None,
|
||||||
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
pub static COMMANDS: Lazy<HashMap<&'static str, &'static TypableCommand>> = Lazy::new(|| {
|
pub static COMMANDS: Lazy<HashMap<&'static str, &'static TypableCommand>> = Lazy::new(|| {
|
||||||
|
@ -2803,6 +2826,15 @@ fn command_mode(cx: &mut Context) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If command is numeric, interpret as line number and go there.
|
||||||
|
if parts.len() == 1 && parts[0].parse::<usize>().ok().is_some() {
|
||||||
|
if let Err(e) = cmd::goto_line_number(cx, &parts[0..], event) {
|
||||||
|
cx.editor.set_error(format!("{}", e));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle typable commands
|
||||||
if let Some(cmd) = cmd::COMMANDS.get(parts[0]) {
|
if let Some(cmd) = cmd::COMMANDS.get(parts[0]) {
|
||||||
if let Err(e) = (cmd.fun)(cx, &parts[1..], event) {
|
if let Err(e) = (cmd.fun)(cx, &parts[1..], event) {
|
||||||
cx.editor.set_error(format!("{}", e));
|
cx.editor.set_error(format!("{}", e));
|
||||||
|
@ -3436,10 +3468,14 @@ fn push_jump(editor: &mut Editor) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn goto_line(cx: &mut Context) {
|
fn goto_line(cx: &mut Context) {
|
||||||
if let Some(count) = cx.count {
|
goto_line_impl(&mut cx.editor, cx.count)
|
||||||
push_jump(cx.editor);
|
}
|
||||||
|
|
||||||
let (view, doc) = current!(cx.editor);
|
fn goto_line_impl(editor: &mut Editor, count: Option<NonZeroUsize>) {
|
||||||
|
if let Some(count) = count {
|
||||||
|
push_jump(editor);
|
||||||
|
|
||||||
|
let (view, doc) = current!(editor);
|
||||||
let max_line = if doc.text().line(doc.text().len_lines() - 1).len_chars() == 0 {
|
let max_line = if doc.text().line(doc.text().len_lines() - 1).len_chars() == 0 {
|
||||||
// If the last line is blank, don't jump to it.
|
// If the last line is blank, don't jump to it.
|
||||||
doc.text().len_lines().saturating_sub(2)
|
doc.text().len_lines().saturating_sub(2)
|
||||||
|
|
Loading…
Reference in New Issue