on master help sign in
Surround with line-endings on `ms<ret>` (#4571)
685cd38Michael Davis
helix-term/src/commands.rs+20−16
@@ -4775,35 +4775,39 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
4775
4775
4776fn surround_add(cx: &mut Context) {
4776fn surround_add(cx: &mut Context) {
4777 cx.on_next_key(move |cx, event| {
4777 cx.on_next_key(move |cx, event| {
4778+ let (view, doc) = current!(cx.editor);
4779+ // surround_len is the number of new characters being added.
4778− let ch = match event.char() {
4780+ let (open, close, surround_len) = match event.char() {
4779− Some(ch) => ch,
4781+ Some(ch) => {
4782+ let (o, c) = surround::get_pair(ch);
4783+ let mut open = Tendril::new();
4784+ open.push(o);
4785+ let mut close = Tendril::new();
4786+ close.push(c);
4787+ (open, close, 2)
4788+ }
4789+ None if event.code == KeyCode::Enter => (
4790+ doc.line_ending.as_str().into(),
4791+ doc.line_ending.as_str().into(),
4792+ 2 * doc.line_ending.len_chars(),
4793+ ),
4780 None => return,
4794 None => return,
4781 };
4795 };
4782− let (view, doc) = current!(cx.editor);
4783− let selection = doc.selection(view.id);
4784− let (open, close) = surround::get_pair(ch);
4785− // The number of chars in get_pair
4786− let surround_len = 2;
4787
4796
4797+ let selection = doc.selection(view.id);
4788 let mut changes = Vec::with_capacity(selection.len() * 2);
4798 let mut changes = Vec::with_capacity(selection.len() * 2);
4789 let mut ranges = SmallVec::with_capacity(selection.len());
4799 let mut ranges = SmallVec::with_capacity(selection.len());
4790 let mut offs = 0;
4800 let mut offs = 0;
4791
4801
4792 for range in selection.iter() {
4802 for range in selection.iter() {
4793− let mut o = Tendril::new();
4803+ changes.push((range.from(), range.from(), Some(open.clone())));
4794− o.push(open);
4804+ changes.push((range.to(), range.to(), Some(close.clone())));
4795− let mut c = Tendril::new();
4805+
4796− c.push(close);
4797− changes.push((range.from(), range.from(), Some(o)));
4798− changes.push((range.to(), range.to(), Some(c)));
4799−
4800− // Add 2 characters to the range to select them
4801 ranges.push(
4806 ranges.push(
4802 Range::new(offs + range.from(), offs + range.to() + surround_len)
4807 Range::new(offs + range.from(), offs + range.to() + surround_len)
4803 .with_direction(range.direction()),
4808 .with_direction(range.direction()),
4804 );
4809 );
4805
4810
4806− // Add 2 characters to the offset for the next ranges
4807 offs += surround_len;
4811 offs += surround_len;
4808 }
4812 }
4809
4813