Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhanced surround_replace to provide visual feedback #7588

Merged
merged 7 commits into from
Jul 13, 2023
14 changes: 13 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5115,11 +5115,22 @@ 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) {
alevinval marked this conversation as resolved.
Show resolved Hide resolved
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, one last thing: you need to compute the correct primary_idx here or the view will be moved if you have a lot of selections: if you selected all the lines in a very big file and were viewing somewhere further down, an index of 0 would move the view all the way to the top

Something like

let primary = selection.primary();
let (i, _) = ranges.iter().enumerate().find(|_i, range| range.overlaps(primary))...

Selection::new(ranges, i)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I've pushed this change and so far so good during smoke testing, but I wonder if there's a better way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a better way. There should be twice as many matches (surrounding positions) as original selections, so the primary index of this selection is twice the original selection primary index.


cx.on_next_key(move |cx, event| {
archseer marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -5130,6 +5141,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);
});
Expand Down