Add goto preview (#2982)

pull/4085/head
Joe 2 years ago committed by GitHub
parent 18f6ec7a8e
commit bcba5d67f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1196,18 +1196,41 @@ pub(super) fn goto_line_number(
args: &[Cow<str>], args: &[Cow<str>],
event: PromptEvent, event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
if event != PromptEvent::Validate { 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(()); return Ok(());
} }
PromptEvent::Validate => {
ensure!(!args.is_empty(), "Line number required"); 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);
}
}
let line = args[0].parse::<usize>()?; let line = args[0].parse::<usize>()?;
goto_line_impl(cx.editor, NonZeroUsize::new(line)); goto_line_impl(cx.editor, NonZeroUsize::new(line));
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
view.ensure_cursor_in_view(doc, line); view.ensure_cursor_in_view(doc, line);
Ok(()) Ok(())
} }

@ -643,7 +643,7 @@ pub struct Editor {
/// The currently applied editor theme. While previewing a theme, the previewed theme /// The currently applied editor theme. While previewing a theme, the previewed theme
/// is set here. /// is set here.
pub theme: Theme, pub theme: Theme,
pub last_line_number: Option<usize>,
pub status_msg: Option<(Cow<'static, str>, Severity)>, pub status_msg: Option<(Cow<'static, str>, Severity)>,
pub autoinfo: Option<Info>, pub autoinfo: Option<Info>,
@ -717,6 +717,7 @@ impl Editor {
syn_loader, syn_loader,
theme_loader, theme_loader,
last_theme: None, last_theme: None,
last_line_number: None,
registers: Registers::default(), registers: Registers::default(),
clipboard_provider: get_clipboard_provider(), clipboard_provider: get_clipboard_provider(),
status_msg: None, status_msg: None,

Loading…
Cancel
Save