on master help sign in
Add goto preview (#2982)
commitbcba5d67f9b8650936c391f6c113945291941f51 authorJoe <Josephharrisonlim@gmail.com> dateMon 3 Oct 2022, 10:33 (4 years ago) parent18f6ec7
2 files changed+33−9
helix-term/src/commands/typed.rs+31−8 helix-view/src/editor.rs+2−1
helix-term/src/commands/typed.rs+31−8
@@ -1196,18 +1196,41 @@ pub(super) fn goto_line_number(
11961196 args: &[Cow<str>],
11971197 event: PromptEvent,
11981198) -> anyhow::Result<()> {
1199− if event != PromptEvent::Validate {
1200− return Ok(());
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+ }
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+ }
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+ }
12011230 }
1202−
1203− ensure!(!args.is_empty(), "Line number required");
1204−
12051231 let line = args[0].parse::<usize>()?;
1206−
12071232 goto_line_impl(cx.editor, NonZeroUsize::new(line));
1208−
12091233 let (view, doc) = current!(cx.editor);
1210−
12111234 view.ensure_cursor_in_view(doc, line);
12121235 Ok(())
12131236}
helix-view/src/editor.rs+2−1
@@ -643,7 +643,7 @@ pub struct Editor {
643643 /// The currently applied editor theme. While previewing a theme, the previewed theme
644644 /// is set here.
645645 pub theme: Theme,
646−
646+ pub last_line_number: Option<usize>,
647647 pub status_msg: Option<(Cow<'static, str>, Severity)>,
648648 pub autoinfo: Option<Info>,
649649
@@ -717,6 +717,7 @@ impl Editor {
717717 syn_loader,
718718 theme_loader,
719719 last_theme: None,
720+ last_line_number: None,
720721 registers: Registers::default(),
721722 clipboard_provider: get_clipboard_provider(),
722723 status_msg: None,