@@ -945,8 +945,12 @@ pub fn append_to_line(cx: &mut Context) {
945 doc.set_selection(view.id, selection);
945 doc.set_selection(view.id, selection);
948−// o inserts a new line after each line with a selection
948+enum Open {
949−pub fn open_below(cx: &mut Context) {
953+fn open(cx: &mut Context, open: Open) {
950 let count = cx.count;
954 let count = cx.count;
951 let (view, doc) = cx.current();
955 let (view, doc) = cx.current();
952 enter_insert_mode(doc);
956 enter_insert_mode(doc);
@@ -959,8 +963,14 @@ pub fn open_below(cx: &mut Context) {
959 let changes: Vec<Change> = lines
963 let changes: Vec<Change> = lines
960 .into_iter()
964 .into_iter()
961 .map(|line| {
965 .map(|line| {
966+ let line = match open {
962− // adjust all positions to the end of the line (next line minus one)
967+ // adjust position to the end of the line (next line - 1)
968+ Open::Below => line + 1,
969+ // adjust position to the end of the previous line (current line - 1)
970+ Open::Above => line,
963− let index = doc.text().line_to_char(line + 1).saturating_sub(1);
973+ let index = doc.text().line_to_char(line).saturating_sub(1);
965 // TODO: share logic with insert_newline for indentation
975 // TODO: share logic with insert_newline for indentation
966 let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
976 let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
@@ -989,49 +999,14 @@ pub fn open_below(cx: &mut Context) {
989 doc.apply(&transaction, view.id);
999 doc.apply(&transaction, view.id);
1002+// o inserts a new line after each line with a selection
1003+pub fn open_below(cx: &mut Context) {
1004+ open(cx, Open::Below)
992// O inserts a new line before each line with a selection
1007// O inserts a new line before each line with a selection
993pub fn open_above(cx: &mut Context) {
1008pub fn open_above(cx: &mut Context) {
994− let count = cx.count;
1009+ open(cx, Open::Above)
995− let (view, doc) = cx.current();
996− enter_insert_mode(doc);
998− let text = doc.text().slice(..);
999− let lines = selection_lines(doc.text(), doc.selection(view.id));
1001− let mut ranges = SmallVec::with_capacity(lines.len());
1003− let changes: Vec<Change> = lines
1006− // adjust all positions to the end of the previous line
1007− let index = doc.text().line_to_char(line).saturating_sub(1);
1009− // TODO: share logic with insert_newline for indentation
1010− let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
1011− let indent = doc.indent_unit().repeat(indent_level);
1012− let mut text = String::with_capacity(1 + indent.len());
1014− text.push_str(&indent);
1015− let text = text.repeat(count);
1017− // calculate new selection range
1018− let pos = index + text.len();
1019− ranges.push(Range::new(pos, pos));
1021− // generate changes
1022− (index, index, Some(text.into()))
1026− // TODO: count actually inserts "n" new lines and starts editing on all of them.
1027− // TODO: append "count" newlines and modify cursors to those lines
1029− let selection = Selection::new(ranges, 0);
1032− Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
1034− doc.apply(&transaction, view.id);
1037pub fn normal_mode(cx: &mut Context) {
1012pub fn normal_mode(cx: &mut Context) {
@@ -1344,14 +1319,14 @@ pub mod insert {
1344 let text = doc.text().slice(..);
1319 let text = doc.text().slice(..);
1345 let transaction =
1320 let transaction =
1346 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
1321 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
1322+ let pos = range.head;
1347 // TODO: offset range.head by 1? when calculating?
1323 // TODO: offset range.head by 1? when calculating?
1349− indent::suggested_indent_for_pos(doc.syntax(), text, range.head, true);
1324+ let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, pos, true);
1350 let indent = doc.indent_unit().repeat(indent_level);
1325 let indent = doc.indent_unit().repeat(indent_level);
1351 let mut text = String::with_capacity(1 + indent.len());
1326 let mut text = String::with_capacity(1 + indent.len());
1352 text.push('\n');
1327 text.push('\n');
1353 text.push_str(&indent);
1328 text.push_str(&indent);
1354− (range.head, range.head, Some(text.into()))
1329+ (pos, pos, Some(text.into()))
1356 doc.apply(&transaction, view.id);
1331 doc.apply(&transaction, view.id);