diff options
author | Michael Davis | 2023-02-01 21:29:48 +0000 |
---|---|---|
committer | GitHub | 2023-02-01 21:29:48 +0000 |
commit | 685cd383a3a56755b06b4146d5dc14872890a56e (patch) | |
tree | 9974d840baf9ac40691971c21bd3065fb0bd1c8b | |
parent | d5f17d3f6947abbfd0684246a440751355e48f9d (diff) |
Surround with line-endings on `ms<ret>` (#4571)
This change makes `ms<ret>` work similarly to `t<ret>` and related
find commands: when the next event is a keypress of Enter, surround
the selection with the document's line-endings.
-rw-r--r-- | helix-term/src/commands.rs | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 1cbdd0fb..2ee3f6b9 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4775,35 +4775,39 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) { fn surround_add(cx: &mut Context) { cx.on_next_key(move |cx, event| { - let ch = match event.char() { - Some(ch) => ch, + let (view, doc) = current!(cx.editor); + // surround_len is the number of new characters being added. + let (open, close, surround_len) = match event.char() { + Some(ch) => { + let (o, c) = surround::get_pair(ch); + let mut open = Tendril::new(); + open.push(o); + let mut close = Tendril::new(); + close.push(c); + (open, close, 2) + } + None if event.code == KeyCode::Enter => ( + doc.line_ending.as_str().into(), + doc.line_ending.as_str().into(), + 2 * doc.line_ending.len_chars(), + ), None => return, }; - let (view, doc) = current!(cx.editor); - let selection = doc.selection(view.id); - let (open, close) = surround::get_pair(ch); - // The number of chars in get_pair - let surround_len = 2; + let selection = doc.selection(view.id); let mut changes = Vec::with_capacity(selection.len() * 2); let mut ranges = SmallVec::with_capacity(selection.len()); let mut offs = 0; for range in selection.iter() { - let mut o = Tendril::new(); - o.push(open); - let mut c = Tendril::new(); - c.push(close); - changes.push((range.from(), range.from(), Some(o))); - changes.push((range.to(), range.to(), Some(c))); - - // Add 2 characters to the range to select them + changes.push((range.from(), range.from(), Some(open.clone()))); + changes.push((range.to(), range.to(), Some(close.clone()))); + ranges.push( Range::new(offs + range.from(), offs + range.to() + surround_len) .with_direction(range.direction()), ); - // Add 2 characters to the offset for the next ranges offs += surround_len; } |