Use jumplist for undo, set jumplist before preview.

pull/5751/head
Kyle Smith 2 years ago
parent 78cac8f934
commit f73a1b2982

@ -4313,15 +4313,18 @@ fn jump_forward(cx: &mut Context) {
} }
fn jump_backward(cx: &mut Context) { fn jump_backward(cx: &mut Context) {
let count = cx.count(); jump_backward_impl(cx.editor, cx.count());
let config = cx.editor.config(); }
let (view, doc) = current!(cx.editor);
fn jump_backward_impl(editor: &mut Editor, count: usize) {
let config = editor.config();
let (view, doc) = current!(editor);
let doc_id = doc.id(); let doc_id = doc.id();
if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) { if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) {
view.doc = *id; view.doc = *id;
let selection = selection.clone(); let selection = selection.clone();
let (view, doc) = current!(cx.editor); // refetch doc let (view, doc) = current!(editor); // refetch doc
if doc.id() != doc_id { if doc.id() != doc_id {
view.add_to_history(doc_id); view.add_to_history(doc_id);

@ -1410,53 +1410,60 @@ fn tutor(
Ok(()) Ok(())
} }
fn undo_goto_line_number_preview(cx: &mut compositor::Context) {
if cx.editor.goto_line_number_preview {
log::info!("undoing goto_line_number preview, jumping backwards");
// if let Some(line_number) = cx.editor.last_line_number {
// goto_line_without_jumplist(cx.editor, NonZeroUsize::new(line_number));
// Remove the jump we added during the preview session.
jump_backward_impl(cx.editor, 1);
// let (view, doc) = current!(cx.editor);
// view.ensure_cursor_in_view(doc, line_number);
cx.editor.goto_line_number_preview = false;
}
}
fn start_goto_line_number_preview(cx: &mut compositor::Context) {
if !cx.editor.goto_line_number_preview {
let (view, doc) = current!(cx.editor);
// Allow the user to jump back to the previous location before invoking
// `goto_line_number`.
push_jump(view, doc);
cx.editor.goto_line_number_preview = true;
}
}
pub(super) fn goto_line_number( pub(super) fn goto_line_number(
cx: &mut compositor::Context, cx: &mut compositor::Context,
args: &[Cow<str>], args: &[Cow<str>],
event: PromptEvent, event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
match event { match event {
PromptEvent::Abort => { PromptEvent::Abort => undo_goto_line_number_preview(cx),
if let Some(line_number) = cx.editor.last_line_number {
goto_line_without_jumplist(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 => { PromptEvent::Validate => {
ensure!(!args.is_empty(), "Line number required"); ensure!(!args.is_empty(), "Line number required");
cx.editor.goto_line_number_preview = false;
let (view, doc) = current!(cx.editor);
push_jump(view, doc);
cx.editor.last_line_number = None;
} }
PromptEvent::Update => { 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, // When a user hits backspace and there are no numbers left,
// we can bring them back to their original line // we can bring them back to their original selection(s).
goto_line_without_jumplist(cx.editor, NonZeroUsize::new(line_number)); if args.is_empty() {
let (view, doc) = current!(cx.editor); undo_goto_line_number_preview(cx);
view.ensure_cursor_in_view(doc, line_number);
cx.editor.last_line_number = None;
}
return Ok(()); return Ok(());
} }
if !cx.editor.goto_line_number_preview {
start_goto_line_number_preview(cx);
}
cx.editor.last_line_number.get_or_insert_with(|| { let line = args[0].parse::<usize>()?;
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let line = doc.selection(view.id).primary().cursor_line(text);
line + 1
});
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let line = args[0].parse::<usize>()?;
view.ensure_cursor_in_view(doc, line); view.ensure_cursor_in_view(doc, line);
goto_line_without_jumplist(cx.editor, NonZeroUsize::new(line)); goto_line_without_jumplist(cx.editor, NonZeroUsize::new(line));
} }

@ -793,7 +793,10 @@ 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>,
// Are we currently previewing a goto_line_number typed command (`:goto <linenum>`, `:<linenum>`)?
pub goto_line_number_preview: bool,
pub status_msg: Option<(Cow<'static, str>, Severity)>, pub status_msg: Option<(Cow<'static, str>, Severity)>,
pub autoinfo: Option<Info>, pub autoinfo: Option<Info>,
@ -896,7 +899,7 @@ impl Editor {
syn_loader, syn_loader,
theme_loader, theme_loader,
last_theme: None, last_theme: None,
last_line_number: None, goto_line_number_preview: false,
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