Skip to content

Commit 4d53759

Browse files
askreetsagnibak-imc
authored andcommitted
Do not add intermediate lines to jumplist with :<linenum> command. (helix-editor#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 5772c43. * Revert "Use jumplist for undo, set jumplist before preview." This reverts commit f73a1b2. * Add last_selection, update implementation. * @pascalkuthe initial feedback * Ensure ":goto 123" keybinding works as expected. * fix clippies, prefer expect() for expect last_selection state
1 parent cd0db75 commit 4d53759

3 files changed

Lines changed: 65 additions & 36 deletions

File tree

helix-term/src/commands.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,10 +2828,15 @@ fn push_jump(view: &mut View, doc: &Document) {
28282828
}
28292829

28302830
fn goto_line(cx: &mut Context) {
2831-
goto_line_impl(cx.editor, cx.count)
2831+
if cx.count.is_some() {
2832+
let (view, doc) = current!(cx.editor);
2833+
push_jump(view, doc);
2834+
2835+
goto_line_without_jumplist(cx.editor, cx.count);
2836+
}
28322837
}
28332838

2834-
fn goto_line_impl(editor: &mut Editor, count: Option<NonZeroUsize>) {
2839+
fn goto_line_without_jumplist(editor: &mut Editor, count: Option<NonZeroUsize>) {
28352840
if let Some(count) = count {
28362841
let (view, doc) = current!(editor);
28372842
let text = doc.text().slice(..);
@@ -2848,7 +2853,6 @@ fn goto_line_impl(editor: &mut Editor, count: Option<NonZeroUsize>) {
28482853
.clone()
28492854
.transform(|range| range.put_cursor(text, pos, editor.mode == Mode::Select));
28502855

2851-
push_jump(view, doc);
28522856
doc.set_selection(view.id, selection);
28532857
}
28542858
}

helix-term/src/commands/typed.rs

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,47 +1572,67 @@ fn tutor(
15721572
Ok(())
15731573
}
15741574

1575+
fn abort_goto_line_number_preview(cx: &mut compositor::Context) {
1576+
if let Some(last_selection) = cx.editor.last_selection.take() {
1577+
let scrolloff = cx.editor.config().scrolloff;
1578+
1579+
let (view, doc) = current!(cx.editor);
1580+
doc.set_selection(view.id, last_selection);
1581+
view.ensure_cursor_in_view(doc, scrolloff);
1582+
}
1583+
}
1584+
1585+
fn update_goto_line_number_preview(
1586+
cx: &mut compositor::Context,
1587+
args: &[Cow<str>],
1588+
) -> anyhow::Result<()> {
1589+
cx.editor.last_selection.get_or_insert_with(|| {
1590+
let (view, doc) = current!(cx.editor);
1591+
doc.selection(view.id).clone()
1592+
});
1593+
1594+
let scrolloff = cx.editor.config().scrolloff;
1595+
let line = args[0].parse::<usize>()?;
1596+
goto_line_without_jumplist(cx.editor, NonZeroUsize::new(line));
1597+
1598+
let (view, doc) = current!(cx.editor);
1599+
view.ensure_cursor_in_view(doc, scrolloff);
1600+
1601+
Ok(())
1602+
}
1603+
15751604
pub(super) fn goto_line_number(
15761605
cx: &mut compositor::Context,
15771606
args: &[Cow<str>],
15781607
event: PromptEvent,
15791608
) -> anyhow::Result<()> {
15801609
match event {
1581-
PromptEvent::Abort => {
1582-
if let Some(line_number) = cx.editor.last_line_number {
1583-
goto_line_impl(cx.editor, NonZeroUsize::new(line_number));
1584-
let (view, doc) = current!(cx.editor);
1585-
view.ensure_cursor_in_view(doc, line_number);
1586-
cx.editor.last_line_number = None;
1587-
}
1588-
return Ok(());
1589-
}
1610+
PromptEvent::Abort => abort_goto_line_number_preview(cx),
15901611
PromptEvent::Validate => {
15911612
ensure!(!args.is_empty(), "Line number required");
1592-
cx.editor.last_line_number = None;
1593-
}
1594-
PromptEvent::Update => {
1595-
if args.is_empty() {
1596-
if let Some(line_number) = cx.editor.last_line_number {
1597-
// When a user hits backspace and there are no numbers left,
1598-
// we can bring them back to their original line
1599-
goto_line_impl(cx.editor, NonZeroUsize::new(line_number));
1600-
let (view, doc) = current!(cx.editor);
1601-
view.ensure_cursor_in_view(doc, line_number);
1602-
cx.editor.last_line_number = None;
1603-
}
1604-
return Ok(());
1605-
}
1613+
1614+
// If we are invoked directly via a keybinding, Validate is
1615+
// sent without any prior Update events. Ensure the cursor
1616+
// is moved to the appropriate location.
1617+
update_goto_line_number_preview(cx, args)?;
1618+
1619+
let last_selection = cx
1620+
.editor
1621+
.last_selection
1622+
.take()
1623+
.expect("update_goto_line_number_preview should always set last_selection");
1624+
16061625
let (view, doc) = current!(cx.editor);
1607-
let text = doc.text().slice(..);
1608-
let line = doc.selection(view.id).primary().cursor_line(text);
1609-
cx.editor.last_line_number.get_or_insert(line + 1);
1626+
view.jumps.push((doc.id(), last_selection));
16101627
}
1628+
1629+
// When a user hits backspace and there are no numbers left,
1630+
// we can bring them back to their original selection. If they
1631+
// begin typing numbers again, we'll start a new preview session.
1632+
PromptEvent::Update if args.is_empty() => abort_goto_line_number_preview(cx),
1633+
PromptEvent::Update => update_goto_line_number_preview(cx, args)?,
16111634
}
1612-
let line = args[0].parse::<usize>()?;
1613-
goto_line_impl(cx.editor, NonZeroUsize::new(line));
1614-
let (view, doc) = current!(cx.editor);
1615-
view.ensure_cursor_in_view(doc, line);
1635+
16161636
Ok(())
16171637
}
16181638

helix-view/src/editor.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ use anyhow::{anyhow, bail, Error};
4040

4141
pub use helix_core::diagnostic::Severity;
4242
pub use helix_core::register::Registers;
43-
use helix_core::Position;
4443
use helix_core::{
4544
auto_pairs::AutoPairs,
4645
syntax::{self, AutoPairConfig},
4746
Change,
4847
};
48+
use helix_core::{Position, Selection};
4949
use helix_dap as dap;
5050
use helix_lsp::lsp;
5151

@@ -848,7 +848,12 @@ pub struct Editor {
848848
/// The currently applied editor theme. While previewing a theme, the previewed theme
849849
/// is set here.
850850
pub theme: Theme,
851-
pub last_line_number: Option<usize>,
851+
852+
/// The primary Selection prior to starting a goto_line_number preview. This is
853+
/// restored when the preview is aborted, or added to the jumplist when it is
854+
/// confirmed.
855+
pub last_selection: Option<Selection>,
856+
852857
pub status_msg: Option<(Cow<'static, str>, Severity)>,
853858
pub autoinfo: Option<Info>,
854859

@@ -964,7 +969,7 @@ impl Editor {
964969
syn_loader,
965970
theme_loader,
966971
last_theme: None,
967-
last_line_number: None,
972+
last_selection: None,
968973
registers: Registers::default(),
969974
clipboard_provider: get_clipboard_provider(),
970975
status_msg: None,

0 commit comments

Comments
 (0)