From 69b9db2fbbfa74342831094d6fc66be88ea3a9d9 Mon Sep 17 00:00:00 2001 From: Daniel Bowring Date: Fri, 2 May 2025 00:12:30 +1000 Subject: [PATCH] Add goto_column and extend_to_column commands (#13440) --- book/src/generated/static-cmd.md | 2 ++ book/src/keymap.md | 1 + helix-term/src/commands.rs | 24 ++++++++++++++++++++++++ helix-term/src/keymap/default.rs | 2 ++ 4 files changed, 29 insertions(+) diff --git a/book/src/generated/static-cmd.md b/book/src/generated/static-cmd.md index f6a82020f..92a1fb2a1 100644 --- a/book/src/generated/static-cmd.md +++ b/book/src/generated/static-cmd.md @@ -153,6 +153,8 @@ | `goto_last_change` | Goto last change | normal: `` ]G ``, select: `` ]G `` | | `goto_line_start` | Goto line start | normal: `` gh ``, `` ``, select: `` gh ``, insert: `` `` | | `goto_line_end` | Goto line end | normal: `` gl ``, `` ``, select: `` gl `` | +| `goto_column` | Goto column | normal: `` g\| `` | +| `extend_to_column` | Extend to column | select: `` g\| `` | | `goto_next_buffer` | Goto next buffer | normal: `` gn ``, select: `` gn `` | | `goto_previous_buffer` | Goto previous buffer | normal: `` gp ``, select: `` gp `` | | `goto_line_end_newline` | Goto newline at line end | insert: `` `` | diff --git a/book/src/keymap.md b/book/src/keymap.md index 2797eaee2..be7085351 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -213,6 +213,7 @@ Jumps to various locations. | Key | Description | Command | | ----- | ----------- | ------- | | `g` | Go to line number `` else start of file | `goto_file_start` | +| | | Go to column number `` else start of line | `goto_column` | | `e` | Go to the end of the file | `goto_last_line` | | `f` | Go to files in the selections | `goto_file` | | `h` | Go to the start of the line | `goto_line_start` | diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 2669d8dd0..86d586274 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -451,6 +451,8 @@ impl MappableCommand { goto_last_change, "Goto last change", goto_line_start, "Goto line start", goto_line_end, "Goto line end", + goto_column, "Goto column", + extend_to_column, "Extend to column", goto_next_buffer, "Goto next buffer", goto_previous_buffer, "Goto previous buffer", goto_line_end_newline, "Goto newline at line end", @@ -3829,6 +3831,28 @@ fn goto_last_line_impl(cx: &mut Context, movement: Movement) { doc.set_selection(view.id, selection); } +fn goto_column(cx: &mut Context) { + goto_column_impl(cx, Movement::Move); +} + +fn extend_to_column(cx: &mut Context) { + goto_column_impl(cx, Movement::Extend); +} + +fn goto_column_impl(cx: &mut Context, movement: Movement) { + let count = cx.count(); + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + let selection = doc.selection(view.id).clone().transform(|range| { + let line = range.cursor_line(text); + let line_start = text.line_to_char(line); + let line_end = line_end_char_index(&text, line); + let pos = graphemes::nth_next_grapheme_boundary(text, line_start, count - 1).min(line_end); + range.put_cursor(text, pos, movement == Movement::Extend) + }); + doc.set_selection(view.id, selection); +} + fn goto_last_accessed_file(cx: &mut Context) { let view = view_mut!(cx.editor); if let Some(alt) = view.docs_access_history.pop() { diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index b9189eb30..82baf336f 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -38,6 +38,7 @@ pub fn default() -> HashMap { "G" => goto_line, "g" => { "Goto" "g" => goto_file_start, + "|" => goto_column, "e" => goto_last_line, "f" => goto_file, "h" => goto_line_start, @@ -368,6 +369,7 @@ pub fn default() -> HashMap { "v" => normal_mode, "g" => { "Goto" "g" => extend_to_file_start, + "|" => extend_to_column, "e" => extend_to_last_line, "k" => extend_line_up, "j" => extend_line_down,