aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAusten Adler2022-11-18 02:46:03 +0000
committerGitHub2022-11-18 02:46:03 +0000
commit1569d2000ba9622f23f658e60af173c2a6b8c88b (patch)
treec2c39e63ce762901ce9cd10a271a6d1e1adf5518
parent9a4e6fdf2573cca5e14e16b8b3b8bdeb0fc6eb23 (diff)
Select surrounding characters when using match/surround (m) mode (#4752)
Co-authored-by: Austen Adler <agadler@austenadler.com>
-rw-r--r--helix-term/src/commands.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 4e3f0767..b00e02b9 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -4530,8 +4530,13 @@ fn surround_add(cx: &mut Context) {
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 @@ fn surround_add(cx: &mut Context) {
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);
})
}