changeset 6756:5af1d6f79785

Add goto_column and extend_to_column commands (#13440)
author Daniel Bowring <git@daniel.bowring.email>
date Fri, 02 May 2025 00:12:30 +1000
parents e775feb4925f
children b14edc3dcf07
files book/src/generated/static-cmd.md book/src/keymap.md helix-term/src/commands.rs helix-term/src/keymap/default.rs
diffstat 4 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/book/src/generated/static-cmd.md	Tue Apr 29 09:28:20 2025 -0400
+++ b/book/src/generated/static-cmd.md	Fri May 02 00:12:30 2025 +1000
@@ -153,6 +153,8 @@
 | `goto_last_change` | Goto last change | normal: `` ]G ``, select: `` ]G `` |
 | `goto_line_start` | Goto line start | normal: `` gh ``, `` <home> ``, select: `` gh ``, insert: `` <home> `` |
 | `goto_line_end` | Goto line end | normal: `` gl ``, `` <end> ``, 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: `` <end> `` |
--- a/book/src/keymap.md	Tue Apr 29 09:28:20 2025 -0400
+++ b/book/src/keymap.md	Fri May 02 00:12:30 2025 +1000
@@ -213,6 +213,7 @@
 | Key   | Description                                      | Command                    |
 | ----- | -----------                                      | -------                    |
 | `g`   | Go to line number `<n>` else start of file       | `goto_file_start`          |
+| <code>&#124;</code>  | Go to column number `<n>` 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`          |
--- a/helix-term/src/commands.rs	Tue Apr 29 09:28:20 2025 -0400
+++ b/helix-term/src/commands.rs	Fri May 02 00:12:30 2025 +1000
@@ -451,6 +451,8 @@
         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 @@
     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() {
--- a/helix-term/src/keymap/default.rs	Tue Apr 29 09:28:20 2025 -0400
+++ b/helix-term/src/keymap/default.rs	Fri May 02 00:12:30 2025 +1000
@@ -38,6 +38,7 @@
         "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 @@
         "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,