on master help sign in
Add goto preview (#2982)
bcba5d6Joe
helix-term/src/commands/typed.rs+31−8
@@ -1196,18 +1196,41 @@ pub(super) fn goto_line_number(
1196 args: &[Cow<str>],
1196 args: &[Cow<str>],
1197 event: PromptEvent,
1197 event: PromptEvent,
1198) -> anyhow::Result<()> {
1198) -> anyhow::Result<()> {
1199+ match event {
1200+ PromptEvent::Abort => {
1201+ if let Some(line_number) = cx.editor.last_line_number {
1202+ goto_line_impl(cx.editor, NonZeroUsize::new(line_number));
1203+ let (view, doc) = current!(cx.editor);
1204+ view.ensure_cursor_in_view(doc, line_number);
1205+ cx.editor.last_line_number = None;
1206+ }
1207+ return Ok(());
1208+ }
1199− if event != PromptEvent::Validate {
1209+ PromptEvent::Validate => {
1210+ ensure!(!args.is_empty(), "Line number required");
1211+ cx.editor.last_line_number = None;
1212+ }
1213+ PromptEvent::Update => {
1214+ if args.is_empty() {
1215+ if let Some(line_number) = cx.editor.last_line_number {
1216+ // When a user hits backspace and there are no numbers left,
1217+ // we can bring them back to their original line
1218+ goto_line_impl(cx.editor, NonZeroUsize::new(line_number));
1219+ let (view, doc) = current!(cx.editor);
1220+ view.ensure_cursor_in_view(doc, line_number);
1221+ cx.editor.last_line_number = None;
1222+ }
1200− return Ok(());
1223+ return Ok(());
1224+ }
1225+ let (view, doc) = current!(cx.editor);
1226+ let text = doc.text().slice(..);
1227+ let line = doc.selection(view.id).primary().cursor_line(text);
1228+ cx.editor.last_line_number.get_or_insert(line + 1);
1229+ }
1201 }
1230 }
1202−
1203− ensure!(!args.is_empty(), "Line number required");
1204−
1205 let line = args[0].parse::<usize>()?;
1231 let line = args[0].parse::<usize>()?;
1206−
1207 goto_line_impl(cx.editor, NonZeroUsize::new(line));
1232 goto_line_impl(cx.editor, NonZeroUsize::new(line));
1208−
1209 let (view, doc) = current!(cx.editor);
1233 let (view, doc) = current!(cx.editor);
1210−
1211 view.ensure_cursor_in_view(doc, line);
1234 view.ensure_cursor_in_view(doc, line);
1212 Ok(())
1235 Ok(())
1213}
1236}
helix-view/src/editor.rs+2−1
@@ -643,7 +643,7 @@ pub struct Editor {
643 /// The currently applied editor theme. While previewing a theme, the previewed theme
643 /// The currently applied editor theme. While previewing a theme, the previewed theme
644 /// is set here.
644 /// is set here.
645 pub theme: Theme,
645 pub theme: Theme,
646−
646+ pub last_line_number: Option<usize>,
647 pub status_msg: Option<(Cow<'static, str>, Severity)>,
647 pub status_msg: Option<(Cow<'static, str>, Severity)>,
648 pub autoinfo: Option<Info>,
648 pub autoinfo: Option<Info>,
649
649
@@ -717,6 +717,7 @@ impl Editor {
717 syn_loader,
717 syn_loader,
718 theme_loader,
718 theme_loader,
719 last_theme: None,
719 last_theme: None,
720+ last_line_number: None,
720 registers: Registers::default(),
721 registers: Registers::default(),
721 clipboard_provider: get_clipboard_provider(),
722 clipboard_provider: get_clipboard_provider(),
722 status_msg: None,
723 status_msg: None,