From d01a01aeb6028fd18beda416c32273d9f71bbd06 Mon Sep 17 00:00:00 2001 From: Joseph Harrison-Lim Date: Tue, 5 Jul 2022 21:55:45 -0400 Subject: [PATCH] Add goto preview --- helix-term/src/commands/typed.rs | 39 +++++++++++++++++++++++++------- helix-view/src/editor.rs | 3 ++- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 4e1ac0da9a9c..1efe65008030 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1160,18 +1160,41 @@ pub(super) fn goto_line_number( args: &[Cow], event: PromptEvent, ) -> anyhow::Result<()> { - if event != PromptEvent::Validate { - return Ok(()); + match event { + PromptEvent::Abort => { + if let Some(line_number) = cx.editor.last_line_number { + goto_line_impl(cx.editor, NonZeroUsize::new(line_number)); + let (view, doc) = current!(cx.editor); + view.ensure_cursor_in_view(doc, line_number); + cx.editor.last_line_number = None; + } + return Ok(()); + } + PromptEvent::Validate => { + ensure!(!args.is_empty(), "Line number required"); + cx.editor.last_line_number = None; + } + PromptEvent::Update => { + if args.is_empty() { + if let Some(line_number) = cx.editor.last_line_number { + // When a user hits backspace and there are no numbers left, + // we can bring them back to their original line + goto_line_impl(cx.editor, NonZeroUsize::new(line_number)); + let (view, doc) = current!(cx.editor); + view.ensure_cursor_in_view(doc, line_number); + cx.editor.last_line_number = None; + } + return Ok(()); + } + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + let line = doc.selection(view.id).primary().cursor_line(text); + cx.editor.last_line_number.get_or_insert(line + 1); + } } - - ensure!(!args.is_empty(), "Line number required"); - let line = args[0].parse::()?; - goto_line_impl(cx.editor, NonZeroUsize::new(line)); - let (view, doc) = current!(cx.editor); - view.ensure_cursor_in_view(doc, line); Ok(()) } diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index a2943af98182..5cce219e02e2 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -481,7 +481,7 @@ pub struct Editor { /// The currently applied editor theme. While previewing a theme, the previewed theme /// is set here. pub theme: Theme, - + pub last_line_number: Option, pub status_msg: Option<(Cow<'static, str>, Severity)>, pub autoinfo: Option, @@ -555,6 +555,7 @@ impl Editor { syn_loader, theme_loader, last_theme: None, + last_line_number: None, registers: Registers::default(), clipboard_provider: get_clipboard_provider(), status_msg: None,