Skip to content

Commit

Permalink
Keep simplifying: update transaction to work on selection, stop chunk…
Browse files Browse the repository at this point in the history
…ing.
  • Loading branch information
alevinval committed Jul 11, 2023
1 parent c9c392e commit 78fa40e
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5115,33 +5115,27 @@ fn surround_replace(cx: &mut Context) {
}
};

// Visual feedback
let selection = selection.clone();
let mut ranges: SmallVec<[Range; 1]> = SmallVec::new();
// TODO: Use [`array_chunks`] once stabilized
for p in change_pos.chunks_exact(2) {
let [from, to] = *p else { unreachable!() };
ranges.push(Range::point(from));
ranges.push(Range::point(to));
}
doc.set_selection(view.id, Selection::new(ranges, 0));
let ranges: SmallVec<_> = change_pos.iter().map(|&p| Range::new(p, p + 1)).collect();
let target_selection = Selection::new(ranges, 0);
let original_selection = selection.clone();
doc.set_selection(view.id, target_selection.clone());

cx.on_next_key(move |cx, event| {
let (view, doc) = current!(cx.editor);
let to = match event.char() {
Some(to) => to,
None => return doc.set_selection(view.id, selection),
None => return doc.set_selection(view.id, original_selection),
};
let (open, close) = surround::get_pair(to);
let transaction = Transaction::change(
doc.text(),
change_pos.iter().enumerate().map(|(i, &pos)| {
let mut i = 0;
let transaction =
Transaction::change_by_selection(doc.text(), &target_selection, |range| {
let mut t = Tendril::new();
t.push(if i % 2 == 0 { open } else { close });
(pos, pos + 1, Some(t))
}),
);
doc.set_selection(view.id, selection);
i += 1;
(range.from(), range.to(), Some(t))
});
doc.set_selection(view.id, original_selection);
doc.apply(&transaction, view.id);
exit_select_mode(cx);
});
Expand Down

0 comments on commit 78fa40e

Please sign in to comment.