on master help sign in
Support m in surround delete and replace
commit76175dbd6da4f57118eb52477f3336c6dcab5692 authorGokul Soumya <gokulps15@gmail.com> dateSun 3 Apr 2022, 22:00 (5 years ago) parentde15d70
2 files changed+16−9
helix-core/src/surround.rs+8−3 helix-term/src/commands.rs+8−6
helix-core/src/surround.rs+8−3
@@ -212,17 +212,22 @@ fn find_nth_close_pair(
212212/// Find position of surround characters around every cursor. Returns None
213213/// if any positions overlap. Note that the positions are in a flat Vec.
214214/// Use get_surround_pos().chunks(2) to get matching pairs of surround positions.
215−/// `ch` can be either closing or opening pair.
215+/// `ch` can be either closing or opening pair. If `ch` is None, surround pairs
216+/// are automatically detected around each cursor (note that this may result
217+/// in them selecting different surround characters for each selection).
216218pub fn get_surround_pos(
217219 text: RopeSlice,
218220 selection: &Selection,
219− ch: char,
221+ ch: Option<char>,
220222 skip: usize,
221223) -> Result<Vec<usize>> {
222224 let mut change_pos = Vec::new();
223225
224226 for &range in selection {
225− let (open_pos, close_pos) = find_nth_pairs_pos(text, ch, range, skip)?;
227+ let (open_pos, close_pos) = match ch {
228+ Some(ch) => find_nth_pairs_pos(text, ch, range, skip)?,
229+ None => find_nth_closest_pairs_pos(text, range, skip)?,
230+ };
226231 if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) {
227232 return Err(Error::CursorOverlap);
228233 }
helix-term/src/commands.rs+8−6
@@ -4111,15 +4111,16 @@ fn surround_add(cx: &mut Context) {
41114111fn surround_replace(cx: &mut Context) {
41124112 let count = cx.count();
41134113 cx.on_next_key(move |cx, event| {
4114− let from = match event.char() {
4115− Some(from) => from,
4114+ let surround_ch = match event.char() {
4115+ Some('m') => None, // m selects the closest surround pair
4116+ Some(ch) => Some(ch),
41164117 None => return,
41174118 };
41184119 let (view, doc) = current!(cx.editor);
41194120 let text = doc.text().slice(..);
41204121 let selection = doc.selection(view.id);
41214122
4122− let change_pos = match surround::get_surround_pos(text, selection, from, count) {
4123+ let change_pos = match surround::get_surround_pos(text, selection, surround_ch, count) {
41234124 Ok(c) => c,
41244125 Err(err) => {
41254126 cx.editor.set_error(err.to_string());
@@ -4150,15 +4151,16 @@ fn surround_replace(cx: &mut Context) {
41504151fn surround_delete(cx: &mut Context) {
41514152 let count = cx.count();
41524153 cx.on_next_key(move |cx, event| {
4153− let ch = match event.char() {
4154− Some(ch) => ch,
4154+ let surround_ch = match event.char() {
4155+ Some('m') => None, // m selects the closest surround pair
4156+ Some(ch) => Some(ch),
41554157 None => return,
41564158 };
41574159 let (view, doc) = current!(cx.editor);
41584160 let text = doc.text().slice(..);
41594161 let selection = doc.selection(view.id);
41604162
4161− let change_pos = match surround::get_surround_pos(text, selection, ch, count) {
4163+ let change_pos = match surround::get_surround_pos(text, selection, surround_ch, count) {
41624164 Ok(c) => c,
41634165 Err(err) => {
41644166 cx.editor.set_error(err.to_string());