Do not add intermediate lines to jumplist with :<linenum> command. (#5751)

* Do not add intermediate lines to jumplist with :<linenum> command.

* Revert jumplist index changes.

* Reduce calculations during update cycle.

* Use jumplist for undo, set jumplist before preview.

* remove some debug logging

* Revert "remove some debug logging"

This reverts commit 5772c4327e.

* Revert "Use jumplist for undo, set jumplist before preview."

This reverts commit f73a1b2982.

* Add last_selection, update implementation.

* @pascalkuthe initial feedback

* Ensure ":goto 123" keybinding works as expected.

* fix clippies, prefer expect() for expect last_selection state
pull/5411/head
Kyle Smith 2 years ago committed by GitHub
parent 0c6d25acae
commit f4bdbe4674
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2828,10 +2828,15 @@ fn push_jump(view: &mut View, doc: &Document) {
} }
fn goto_line(cx: &mut Context) { fn goto_line(cx: &mut Context) {
goto_line_impl(cx.editor, cx.count) if cx.count.is_some() {
let (view, doc) = current!(cx.editor);
push_jump(view, doc);
goto_line_without_jumplist(cx.editor, cx.count);
}
} }
fn goto_line_impl(editor: &mut Editor, count: Option<NonZeroUsize>) { fn goto_line_without_jumplist(editor: &mut Editor, count: Option<NonZeroUsize>) {
if let Some(count) = count { if let Some(count) = count {
let (view, doc) = current!(editor); let (view, doc) = current!(editor);
let text = doc.text().slice(..); let text = doc.text().slice(..);
@ -2848,7 +2853,6 @@ fn goto_line_impl(editor: &mut Editor, count: Option<NonZeroUsize>) {
.clone() .clone()
.transform(|range| range.put_cursor(text, pos, editor.mode == Mode::Select)); .transform(|range| range.put_cursor(text, pos, editor.mode == Mode::Select));
push_jump(view, doc);
doc.set_selection(view.id, selection); doc.set_selection(view.id, selection);
} }
} }

@ -1572,47 +1572,67 @@ fn tutor(
Ok(()) Ok(())
} }
fn abort_goto_line_number_preview(cx: &mut compositor::Context) {
if let Some(last_selection) = cx.editor.last_selection.take() {
let scrolloff = cx.editor.config().scrolloff;
let (view, doc) = current!(cx.editor);
doc.set_selection(view.id, last_selection);
view.ensure_cursor_in_view(doc, scrolloff);
}
}
fn update_goto_line_number_preview(
cx: &mut compositor::Context,
args: &[Cow<str>],
) -> anyhow::Result<()> {
cx.editor.last_selection.get_or_insert_with(|| {
let (view, doc) = current!(cx.editor);
doc.selection(view.id).clone()
});
let scrolloff = cx.editor.config().scrolloff;
let line = args[0].parse::<usize>()?;
goto_line_without_jumplist(cx.editor, NonZeroUsize::new(line));
let (view, doc) = current!(cx.editor);
view.ensure_cursor_in_view(doc, scrolloff);
Ok(())
}
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 => abort_goto_line_number_preview(cx),
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 => { PromptEvent::Validate => {
ensure!(!args.is_empty(), "Line number required"); ensure!(!args.is_empty(), "Line number required");
cx.editor.last_line_number = None;
} // If we are invoked directly via a keybinding, Validate is
PromptEvent::Update => { // sent without any prior Update events. Ensure the cursor
if args.is_empty() { // is moved to the appropriate location.
if let Some(line_number) = cx.editor.last_line_number { update_goto_line_number_preview(cx, args)?;
// When a user hits backspace and there are no numbers left,
// we can bring them back to their original line let last_selection = cx
goto_line_impl(cx.editor, NonZeroUsize::new(line_number)); .editor
let (view, doc) = current!(cx.editor); .last_selection
view.ensure_cursor_in_view(doc, line_number); .take()
cx.editor.last_line_number = None; .expect("update_goto_line_number_preview should always set last_selection");
}
return Ok(());
}
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..); view.jumps.push((doc.id(), last_selection));
let line = doc.selection(view.id).primary().cursor_line(text);
cx.editor.last_line_number.get_or_insert(line + 1);
} }
// When a user hits backspace and there are no numbers left,
// we can bring them back to their original selection. If they
// begin typing numbers again, we'll start a new preview session.
PromptEvent::Update if args.is_empty() => abort_goto_line_number_preview(cx),
PromptEvent::Update => update_goto_line_number_preview(cx, args)?,
} }
let line = args[0].parse::<usize>()?;
goto_line_impl(cx.editor, NonZeroUsize::new(line));
let (view, doc) = current!(cx.editor);
view.ensure_cursor_in_view(doc, line);
Ok(()) Ok(())
} }

@ -40,12 +40,12 @@ use anyhow::{anyhow, bail, Error};
pub use helix_core::diagnostic::Severity; pub use helix_core::diagnostic::Severity;
pub use helix_core::register::Registers; pub use helix_core::register::Registers;
use helix_core::Position;
use helix_core::{ use helix_core::{
auto_pairs::AutoPairs, auto_pairs::AutoPairs,
syntax::{self, AutoPairConfig}, syntax::{self, AutoPairConfig},
Change, Change,
}; };
use helix_core::{Position, Selection};
use helix_dap as dap; use helix_dap as dap;
use helix_lsp::lsp; use helix_lsp::lsp;
@ -848,7 +848,12 @@ 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>,
/// The primary Selection prior to starting a goto_line_number preview. This is
/// restored when the preview is aborted, or added to the jumplist when it is
/// confirmed.
pub last_selection: Option<Selection>,
pub status_msg: Option<(Cow<'static, str>, Severity)>, pub status_msg: Option<(Cow<'static, str>, Severity)>,
pub autoinfo: Option<Info>, pub autoinfo: Option<Info>,
@ -964,7 +969,7 @@ impl Editor {
syn_loader, syn_loader,
theme_loader, theme_loader,
last_theme: None, last_theme: None,
last_line_number: None, last_selection: 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