feat: jump to a line with counted g

This commit is contained in:
Valdemar Lorenzen
2026-10-13 13:28:25 +02:00
parent 8f87b8d3d1
commit b16232aa1d
6 changed files with 112 additions and 8 deletions
+1 -1
View File
@@ -26,7 +26,7 @@
- [x] git blame (enabled by default).
- [x] when jumping to lsp definitions, that line should be center of the buffer (currently placed at the top of the buffer view)
- [x] diff view: add ability to mark files as reviewed. this should just put a check mark left of the file modification indicator (e.g., M, A, D, maybe others)
- [ ] make `<line>g` go to that line.
- [x] make `<line>g` go to that line.
- [ ] diff view: if the after point is selected to be current working changes (i.e. `@`), diff view should update to include this after point accordingly (and automatically) - i.e. it should move the after point. this should only be the case when the new checkout is ahead of the after point selected. should be enabled by default.
- [ ] buffer picker (with search).
- [ ] pull a pr description and display it in a buffer:
+3 -1
View File
@@ -151,6 +151,8 @@ Normal mode binds `ctrl+option+h/l` (`ctrl+alt+h/l`) to previous/next tab. Tab a
The built-in `gt`/`gb` sequences move to the first/last line in Normal and Visual; `gf` follows Markdown file links in Normal only. Bind `config.core.document_start`, `config.core.document_end`, or `config.core.follow_link` to another key or sequence to add another route. A configured/disabled `g`, a configured sequence starting with `g`, or choosing `g` as the leader overrides the built-in prefix in that binding mode.
With a decimal prefix, `g` instead jumps immediately to that one-based source line: `42g` goes to line 42, clamped at EOF, at first nonblank. Normal moves all carets; Visual extends selections. In diff text panes, explicit line jumps reveal folded source lines rather than counting only displayed excerpts. `g` without a count still begins `gt`, `gb`, or `gf`; a count followed by `g` is complete and does not await a suffix. Rebind the Normal/Visual native action `config.core.go_to_line` to use the numeric prefix with another key. Existing `g` bindings and prefixes override this default route.
Normal `yy` copies complete rows without moving the carets; `dd` / `cc` delete/change complete rows; `dw` / `db` delete forward/backward word spans. Leading and post-operator counts multiply. Bind `config.core.copy_line`, `config.core.delete_line`, `config.core.change_line`, `config.core.delete_word_forward`, or `config.core.delete_word_backward` to another key or sequence for another route. A configured/disabled operator key, a configured sequence starting with that key, or choosing it as the leader overrides its built-in operator prefix. These are dedicated actions, not general operator–motion grammar.
Normal `C` / `D` / `Y` change/delete/copy the cursor-to-line-end suffix through `config.core.change_to_line_end`, `config.core.delete_to_line_end`, and `config.core.copy_to_line_end`. Normal `r` waits for a literal replacement character through `config.core.replace_character`; `s` deletes the current grapheme and enters Insert through `config.core.substitute_character`. Rebinding these actions preserves their native counts, Unicode boundaries, undo, and read-only guards.
@@ -183,7 +185,7 @@ Normal `C` / `D` / `Y` change/delete/copy the cursor-to-line-end suffix through
| Normal, Visual | `select`, `select_linewise`, `search_next`, `search_previous` |
| Normal, Visual | `repeat_last_edit` |
| Normal, Visual | `file_picker`, `file_explorer` |
| Normal, Visual | `document_start`, `document_end` |
| Normal, Visual | `document_start`, `document_end`, `go_to_line` |
| Normal, Visual | `seek_forward`, `seek_backward` |
| Normal, Insert, Visual | `line_start`, `line_end` |
| Normal, Insert, Visual, Tree | `left`, `right` |
+1
View File
@@ -22,6 +22,7 @@ Default controls and editing behavior. For custom bindings, see [configuration](
| Normal, Visual | `f<char>` seeks forward on the current line; `F<char>` seeks backward; `2fx` finds the second `x` after each caret |
| Normal | `gf` follows a Markdown file link or heading fragment at the primary caret |
| Normal, Visual | `gt` goes to the first line; `gb` goes to the last line, at first nonblank; Visual extends every selection |
| Normal, Visual | `<line>g` jumps immediately to that one-based source line at first nonblank (`42g`); clamps at EOF and extends Visual selections |
| Normal | Ctrl+o goes to an older jump location; Ctrl+i/Tab goes forward; counts select multiple locations |
| Normal, Visual | Decimal prefixes repeat cursor/word/search motions: `5j`, `3k`, `2w`, `4e`, `2n`; Esc cancels a pending count |
| Normal | `J` (Shift+j) joins each targeted line with the next line |
+56 -6
View File
@@ -612,6 +612,11 @@ core_actions![
Action::DocumentEnd,
&[Mode::Normal, Mode::Visual],
),
(
"core.go_to_line",
Action::GoToLine,
&[Mode::Normal, Mode::Visual]
),
(
"core.seek_forward",
Action::SeekForward,
@@ -5986,6 +5991,9 @@ impl Editor {
return None;
}
if matches!(mode, Mode::Normal | Mode::Visual) && input == Input::Char('g') {
if self.count.is_some() {
return self.act(Action::GoToLine);
}
self.count = None;
self.g_pending = true;
return None;
@@ -6262,6 +6270,7 @@ impl Editor {
| Action::LineEnd
| Action::DocumentStart
| Action::DocumentEnd
| Action::GoToLine
| Action::WordNext
| Action::WordPrevious
| Action::WordEnd
@@ -9043,6 +9052,12 @@ impl Editor {
fn move_targets(&mut self, action: Action, count: usize) {
self.reveal_caret();
if action == Action::GoToLine
&& let Some(focus) = self.diff_buffer_focus()
{
let row = count.saturating_sub(1).min(self.active.lines.len() - 1);
self.diff.as_mut().unwrap().reveal_source_row(focus, row);
}
for index in 0..self.active.view.targets.len() {
let mut cursor = self.active.view.targets[index].active;
match action {
@@ -9093,6 +9108,10 @@ impl Editor {
}
Action::LineStart => cursor.column = 0,
Action::LineEnd => cursor.column = self.max_column(cursor.row),
Action::GoToLine => {
cursor.row = count.saturating_sub(1).min(self.active.lines.len() - 1);
cursor.column = first_nonblank(&self.active.lines[cursor.row]);
}
Action::DocumentStart | Action::DocumentEnd => {
cursor.row = if action == Action::DocumentStart {
self.navigable_rows(0, self.active.lines.len())
@@ -10708,7 +10727,7 @@ mod tests {
set_carets(&mut editor, &[(0, 1), (1, 1)], 1);
editor.set_message("Written");
let targets = editor.active.view.targets.clone();
for prefix in ["f", "3g", "2d", "4", " b"] {
for prefix in ["f", "g", "2d", "4", " b"] {
type_text(&mut editor, prefix);
editor.handle(Input::Escape);
assert!(editor.notification_visible(), "{prefix}");
@@ -14953,13 +14972,46 @@ mod tests {
fs::write(&source, "[code](main.rs)").unwrap();
fs::write(&target, "fn main() {}").unwrap();
let mut editor = Editor::open(Some(source), vec![]).unwrap();
type_text(&mut editor, "9gf");
type_text(&mut editor, "gf");
assert_eq!(editor.cursor(), Cursor::default());
assert_eq!(editor.count(), None);
assert_eq!(editor.buffer_count(), 2);
assert_eq!(editor.lines(), ["fn main() {}"]);
}
#[test]
fn counted_g_reveals_exact_hidden_source_rows_in_readonly_diffs() {
let directory = Directory::new();
let mut editor = editor();
let before: Vec<_> = (0..80).map(|row| format!("row {row}")).collect();
let mut after = before.clone();
after[40] = "changed".into();
historical_diff(&mut editor, &directory.0, before, after);
editor.diff.as_mut().unwrap().set_context_options(0, 1);
let text = editor.lines().to_vec();
type_text(&mut editor, "10g");
assert_eq!(editor.cursor().row, 9);
let focus = editor.diff_buffer_focus().unwrap();
assert!(
editor
.diff()
.unwrap()
.source_display_row(focus, 9)
.is_some()
);
assert!(editor.mutation_blocked());
assert_eq!(editor.lines(), text);
type_text(&mut editor, "79g");
assert_eq!(editor.cursor().row, 78);
assert!(
editor
.diff()
.unwrap()
.source_display_row(focus, 78)
.is_some()
);
}
#[test]
fn document_navigation_moves_all_carets_to_first_nonblank_without_edit_history() {
let mut editor = editor();
@@ -15159,11 +15211,11 @@ mod tests {
editor
.apply(vec![Effect::Replace("a\nb\nc".into())])
.unwrap();
type_text(&mut editor, "4g");
type_text(&mut editor, "g");
editor.handle(Input::Escape);
assert!(!editor.g_pending());
assert_eq!(editor.count(), None);
type_text(&mut editor, "4gj");
type_text(&mut editor, "gj");
assert_eq!(editor.cursor(), Cursor { row: 1, column: 0 });
assert!(!editor.g_pending());
type_text(&mut editor, "gi");
@@ -18723,8 +18775,6 @@ mod tests {
editor.handle(Input::Escape);
assert_eq!(editor.mode(), Mode::Normal);
assert_eq!(editor.selection_count(), 2);
type_text(&mut editor, "3g");
assert_eq!(editor.count(), None);
editor.bind("normal", "2", None).unwrap();
type_text(&mut editor, "42");
assert_eq!(editor.count(), None);
+1
View File
@@ -23,6 +23,7 @@ Normal: i/a insert, I/A insert at first nonblank/end, o/O new line
Normal/Visual: f<char>/F<char> seek next/previous on current line; 2fx seeks second x
gf follows Markdown file links/reference links and heading fragments (no LSP)
Normal/Visual: gt first line, gb last line, at first nonblank; Esc cancels g
Normal/Visual: <line>g jumps to that one-based source line at first nonblank, clamped at EOF
Ctrl+o older jump location; Ctrl+i/Tab newer (counts supported)
_ splits above/below, | left/right; Option/Alt+h/j/k/l focuses neighboring panes
Ctrl+Option/Alt+h/l previous/next tab; :tab.new opens current buffer in another tab
+50
View File
@@ -0,0 +1,50 @@
use vamp::editor::{Cursor, Editor, Effect, Input, Mode};
fn editor() -> Editor {
let mut editor = Editor::open(None, vec![]).unwrap();
editor
.apply(vec![Effect::Replace(
"first\n e\u{301}second\n\tthird".into(),
)])
.unwrap();
editor
}
fn keys(editor: &mut Editor, text: &str) {
for key in text.chars() {
editor.handle(Input::Char(key));
}
}
#[test]
fn counted_g_is_one_based_clamps_and_leaves_bare_prefixes_intact() {
let mut editor = editor();
let revision = editor.text_revision();
keys(&mut editor, "2g");
assert_eq!(editor.cursor(), Cursor { row: 1, column: 2 });
assert_eq!(editor.count(), None);
keys(&mut editor, "999999999999999999999999999999999g");
assert_eq!(editor.cursor(), Cursor { row: 2, column: 1 });
keys(&mut editor, "1g");
assert_eq!(editor.cursor().row, 0);
keys(&mut editor, "gb");
assert_eq!(editor.cursor().row, 2);
keys(&mut editor, "gt");
assert_eq!(editor.cursor().row, 0);
assert_eq!(editor.text_revision(), revision);
}
#[test]
fn counted_g_extends_visual_selection_and_respects_g_overrides() {
let mut editor = editor();
keys(&mut editor, "v3g");
assert_eq!(editor.mode(), Mode::Visual);
assert_eq!(editor.cursor(), Cursor { row: 2, column: 1 });
assert_eq!(editor.selections()[0].start, Cursor { row: 0, column: 0 });
editor.handle(Input::Escape);
keys(&mut editor, "gt");
editor.bind("normal", "g", None).unwrap();
keys(&mut editor, "2g");
assert_eq!(editor.cursor().row, 0);
editor
.bind("normal", "g", Some("core.down".into()))
.unwrap();
keys(&mut editor, "2g");
assert_eq!(editor.cursor().row, 2);
}