@@ -1495,53 +1495,55 @@ fn open(cx: &mut Context, open: Open) {
1495 enter_insert_mode(doc);
1495 enter_insert_mode(doc);
1497 let text = doc.text().slice(..);
1497 let text = doc.text().slice(..);
1498+ let contents = doc.text();
1498 let selection = doc.selection(view.id);
1499 let selection = doc.selection(view.id);
1500 let mut ranges = SmallVec::with_capacity(selection.len());
1501 let mut ranges = SmallVec::with_capacity(selection.len());
1502− let changes: Vec<Change> = selection
1504+ let mut transaction = Transaction::change_by_selection(contents, selection, |range| {
1505− let line = text.char_to_line(range.head);
1505+ let line = text.char_to_line(range.head);
1507− let line = match open {
1508− // adjust position to the end of the line (next line - 1)
1509− Open::Below => line + 1,
1510− // adjust position to the end of the previous line (current line - 1)
1511− Open::Above => line,
1514− let index = doc.text().line_to_char(line).saturating_sub(1);
1507+ let line = match open {
1508+ // adjust position to the end of the line (next line - 1)
1509+ Open::Below => line + 1,
1510+ // adjust position to the end of the previous line (current line - 1)
1511+ Open::Above => line,
1516− // TODO: share logic with insert_newline for indentation
1514+ let index = doc.text().line_to_char(line).saturating_sub(1);
1517− let indent_level = indent::suggested_indent_for_pos(
1518− doc.language_config(),
1524− let indent = doc.indent_unit().repeat(indent_level);
1525− let mut text = String::with_capacity(1 + indent.len());
1527− text.push_str(&indent);
1528− let text = text.repeat(count);
1516+ // TODO: share logic with insert_newline for indentation
1517+ let indent_level = indent::suggested_indent_for_pos(
1518+ doc.language_config(),
1524+ let indent = doc.indent_unit().repeat(indent_level);
1525+ let mut text = String::with_capacity(1 + indent.len());
1527+ text.push_str(&indent);
1528+ let text = text.repeat(count);
1530− // calculate new selection range
1530+ // calculate new selection range
1531+ let pos = if line == 0 {
1532+ 0 // Required since text will have a min len of 1 (\n)
1531− let pos = index + text.chars().count();
1534+ index + offs + text.chars().count()
1532− ranges.push(Range::new(pos, pos));
1536+ ranges.push(Range::new(pos, pos));
1538+ offs += text.chars().count();
1534− (index, index, Some(text.into()))
1540+ (index, index, Some(text.into()))
1538 // TODO: count actually inserts "n" new lines and starts editing on all of them.
1543 // TODO: count actually inserts "n" new lines and starts editing on all of them.
1539 // TODO: append "count" newlines and modify cursors to those lines
1544 // TODO: append "count" newlines and modify cursors to those lines
1541− let selection = Selection::new(ranges, 0);
1546+ transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
1544− Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
1546 doc.apply(&transaction, view.id);
1548 doc.apply(&transaction, view.id);