Skip to content

Commit

Permalink
enhance(sourround_replace): provide visual feedback during the operation
Browse files Browse the repository at this point in the history
When the target character is pressed, the selection is temporarily updated
to display the affected positions. It restores the original selection after
the operation is cancelled or completed.
  • Loading branch information
alevinval committed Jul 10, 2023
1 parent f68956a commit 3dc4ad9
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 @@ -5115,11 +5115,25 @@ fn surround_replace(cx: &mut Context) {
}
};

// Visual feedback
let selection = selection.clone();
let mut ranges: SmallVec<[Range; 1]> = SmallVec::new();
change_pos.chunks(2).for_each(|p| {
if p.len() == 2 {
let from = *p.first().unwrap();
let to = *p.get(1).unwrap();
ranges.push(Range::new(from, from + 1));
ranges.push(Range::new(to, to + 1));
}
});
let feedback = Selection::new(ranges, 0);
doc.set_selection(view.id, feedback);

cx.on_next_key(move |cx, event| {
let (view, doc) = current!(cx.editor);
let to = match event.char() {
Some(to) => to,
None => return,
None => return doc.set_selection(view.id, selection),
};
let (open, close) = surround::get_pair(to);
let transaction = Transaction::change(
Expand All @@ -5131,6 +5145,7 @@ fn surround_replace(cx: &mut Context) {
}),
);
doc.apply(&transaction, view.id);
doc.set_selection(view.id, selection);
exit_select_mode(cx);
});
})
Expand Down

0 comments on commit 3dc4ad9

Please sign in to comment.