@@ -4775,35 +4775,39 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
47754775
47764776fn surround_add(cx: &mut Context) {
47774777 cx.on_next_key(move |cx, event| {
4778− let ch = match event.char() {
4779− Some(ch) => ch,
4778+ let (view, doc) = current!(cx.editor);
4779+ // surround_len is the number of new characters being added.
4780+ let (open, close, surround_len) = match event.char() {
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+ ),
47804794 None => return,
47814795 };
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;
47874796
4797+ let selection = doc.selection(view.id);
47884798 let mut changes = Vec::with_capacity(selection.len() * 2);
47894799 let mut ranges = SmallVec::with_capacity(selection.len());
47904800 let mut offs = 0;
47914801
47924802 for range in selection.iter() {
4793− let mut o = Tendril::new();
4794− o.push(open);
4795− let mut c = Tendril::new();
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
4803+ changes.push((range.from(), range.from(), Some(open.clone())));
4804+ changes.push((range.to(), range.to(), Some(close.clone())));
4805+
48014806 ranges.push(
48024807 Range::new(offs + range.from(), offs + range.to() + surround_len)
48034808 .with_direction(range.direction()),
48044809 );
48054810
4806− // Add 2 characters to the offset for the next ranges
48074811 offs += surround_len;
48084812 }
48094813