Skip to content

Commit

Permalink
Select surrounding characters when using match/surround (m) mode (hel…
Browse files Browse the repository at this point in the history
…ix-editor#4752)

Co-authored-by: Austen Adler <agadler@austenadler.com>
  • Loading branch information
2 people authored and Shekhinah Memmel committed Dec 11, 2022
1 parent 90634f3 commit dce731a
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4570,18 +4570,33 @@ 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);
let mut c = Tendril::new();
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);
})
}
Expand Down

0 comments on commit dce731a

Please sign in to comment.