on master help sign in
Select surrounding characters when using match/surround (m) mode (#4752)
1569d20Austen Adler
helix-term/src/commands.rs+16−1
@@ -4530,8 +4530,13 @@ fn surround_add(cx: &mut Context) {
4530 let (view, doc) = current!(cx.editor);
4530 let (view, doc) = current!(cx.editor);
4531 let selection = doc.selection(view.id);
4531 let selection = doc.selection(view.id);
4532 let (open, close) = surround::get_pair(ch);
4532 let (open, close) = surround::get_pair(ch);
4533+ // The number of chars in get_pair
4534+ let surround_len = 2;
4533
4535
4534 let mut changes = Vec::with_capacity(selection.len() * 2);
4536 let mut changes = Vec::with_capacity(selection.len() * 2);
4537+ let mut ranges = SmallVec::with_capacity(selection.len());
4538+ let mut offs = 0;
4539+
4535 for range in selection.iter() {
4540 for range in selection.iter() {
4536 let mut o = Tendril::new();
4541 let mut o = Tendril::new();
4537 o.push(open);
4542 o.push(open);
@@ -4539,9 +4544,19 @@ fn surround_add(cx: &mut Context) {
4539 c.push(close);
4544 c.push(close);
4540 changes.push((range.from(), range.from(), Some(o)));
4545 changes.push((range.from(), range.from(), Some(o)));
4541 changes.push((range.to(), range.to(), Some(c)));
4546 changes.push((range.to(), range.to(), Some(c)));
4547+
4548+ // Add 2 characters to the range to select them
4549+ ranges.push(
4550+ Range::new(offs + range.from(), offs + range.to() + surround_len)
4551+ .with_direction(range.direction()),
4552+ );
4553+
4554+ // Add 2 characters to the offset for the next ranges
4555+ offs += surround_len;
4542 }
4556 }
4543
4557
4544− let transaction = Transaction::change(doc.text(), changes.into_iter());
4558+ let transaction = Transaction::change(doc.text(), changes.into_iter())
4559+ .with_selection(Selection::new(ranges, selection.primary_index()));
4545 apply_transaction(&transaction, doc, view);
4560 apply_transaction(&transaction, doc, view);
4546 })
4561 })
4547}
4562}