From f26677dddba793f634da6281df5cf35ec607962d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Vinyals=20Valdepe=C3=B1as?= Date: Mon, 10 Jul 2023 09:25:30 +0200 Subject: [PATCH] enhance(sourround_replace): provide visual feedback during the operation 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. --- helix-term/src/commands.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b90f418a63f7..f3c94304c5b1 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -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( @@ -5130,6 +5144,7 @@ fn surround_replace(cx: &mut Context) { (pos, pos + 1, Some(t)) }), ); + doc.set_selection(view.id, selection); doc.apply(&transaction, view.id); exit_select_mode(cx); });