changeset 3794:04061678a727

Select surrounding characters when using match/surround (m) mode (#4752) Co-authored-by: Austen Adler <[email protected]>
author Austen Adler <stonewareslord@gmail.com>
date Fri, 18 Nov 2022 02:46:03 +0000
parents a6ba47d0d84d
children 10eb35954fa4
files helix-term/src/commands.rs
diffstat 1 files changed, 16 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/helix-term/src/commands.rs	Thu Nov 17 23:22:49 2022 -0300
+++ b/helix-term/src/commands.rs	Fri Nov 18 02:46:03 2022 +0000
@@ -4530,8 +4530,13 @@
         let (view, doc) = current!(cx.editor);
         let selection = doc.selection(view.id);
         let (open, close) = surround::get_pair(ch);
+        // The number of chars in get_pair
+        let surround_len = 2;
 
         let mut changes = Vec::with_capacity(selection.len() * 2);
+        let mut ranges = SmallVec::with_capacity(selection.len());
+        let mut offs = 0;
+
         for range in selection.iter() {
             let mut o = Tendril::new();
             o.push(open);
@@ -4539,9 +4544,19 @@
             c.push(close);
             changes.push((range.from(), range.from(), Some(o)));
             changes.push((range.to(), range.to(), Some(c)));
+
+            // Add 2 characters to the range to select them
+            ranges.push(
+                Range::new(offs + range.from(), offs + range.to() + surround_len)
+                    .with_direction(range.direction()),
+            );
+
+            // Add 2 characters to the offset for the next ranges
+            offs += surround_len;
         }
 
-        let transaction = Transaction::change(doc.text(), changes.into_iter());
+        let transaction = Transaction::change(doc.text(), changes.into_iter())
+            .with_selection(Selection::new(ranges, selection.primary_index()));
         apply_transaction(&transaction, doc, view);
     })
 }