From a4e28c6927e59cf08d056e9c9a74be095eae5f29 Mon Sep 17 00:00:00 2001
From: Blaž Hrastnik
Date: Mon, 5 Jul 2021 10:12:34 +0900
Subject: Implement `X` as extend selection to line bounds

---
 helix-term/src/commands.rs | 21 +++++++++++++++++++++
 helix-term/src/keymap.rs   |  4 +++-
 2 files changed, 24 insertions(+), 1 deletion(-)

(limited to 'helix-term')

diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index d8892c9c..63b91942 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -200,6 +200,7 @@ impl Command {
         extend_search_next,
         search_selection,
         extend_line,
+        extend_to_line_bounds,
         delete_selection,
         change_selection,
         collapse_selection,
@@ -1021,6 +1022,26 @@ fn extend_line(cx: &mut Context) {
     doc.set_selection(view.id, Selection::single(start, end));
 }
 
+fn extend_to_line_bounds(cx: &mut Context) {
+    let (view, doc) = current!(cx.editor);
+
+    let text = doc.text();
+    let selection = doc.selection(view.id).transform(|range| {
+        let start = text.line_to_char(text.char_to_line(range.from()));
+        let end = text
+            .line_to_char(text.char_to_line(range.to()) + 1)
+            .saturating_sub(1);
+
+        if range.anchor < range.head {
+            Range::new(start, end)
+        } else {
+            Range::new(end, start)
+        }
+    });
+
+    doc.set_selection(view.id, selection);
+}
+
 fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId) {
     // first yank the selection
     let values: Vec<String> = doc
diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs
index 6c7a24b1..c340eb2c 100644
--- a/helix-term/src/keymap.rs
+++ b/helix-term/src/keymap.rs
@@ -209,7 +209,9 @@ impl Default for Keymaps {
             alt!(';') => Command::flip_selections,
             key!('%') => Command::select_all,
             key!('x') => Command::extend_line,
-            // extend_to_whole_line, crop_to_whole_line
+            key!('x') => Command::extend_line,
+            key!('X') => Command::extend_to_line_bounds,
+            // crop_to_whole_line
 
 
             key!('m') => Command::match_mode,
-- 
cgit v1.2.3-70-g09d2