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
+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