@@ -869,13 +869,6 @@ pub fn open_below(cx: &mut Context) {
869 text.push_str(&indent);
869 text.push_str(&indent);
870 let text = text.repeat(count);
870 let text = text.repeat(count);
872− // TODO: ideally we want to run a hook over the transactions to figure out and reindent all
873− // \n's as a post-processing step?
875− // - on insert mode enter: we add newline + indent and position cursor at the end
876− // - on 3o/3O: we insert 3 newlines + indents each and position cursors at ends
879 (index, index, Some(text.into()))
872 (index, index, Some(text.into()))
881 .collect();
874 .collect();
@@ -902,6 +895,56 @@ pub fn open_below(cx: &mut Context) {
904// O inserts a new line before each line with a selection
897// O inserts a new line before each line with a selection
898+pub fn open_above(cx: &mut Context) {
899+ let count = cx.count;
900+ let mut doc = cx.doc();
901+ enter_insert_mode(&mut doc);
903+ let lines = selection_lines(doc.text(), doc.selection());
905+ let positions = lines.into_iter().map(|index| {
906+ // adjust all positions to the end of the previous line
907+ doc.text().line_to_char(index).saturating_sub(1)
910+ let text = doc.text().slice(..);
912+ let changes: Vec<Change> = positions
914+ // TODO: share logic with insert_newline for indentation
916+ helix_core::indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
917+ let indent = doc.indent_unit().repeat(indent_level);
918+ let mut text = String::with_capacity(1 + indent.len());
920+ text.push_str(&indent);
921+ let text = text.repeat(count);
924+ (index, index, Some(text.into()))
928+ // TODO: count actually inserts "n" new lines and starts editing on all of them.
929+ // TODO: append "count" newlines and modify cursors to those lines
931+ let selection = Selection::new(
934+ .map(|(start, _end, text): &Change| {
935+ let len = text.as_ref().map(|text| text.len()).unwrap(); // minus newline
936+ let pos = start + len;
937+ Range::new(pos, pos)
944+ Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
946+ doc.apply(&transaction);
906pub fn normal_mode(cx: &mut Context) {
949pub fn normal_mode(cx: &mut Context) {
907 let mut doc = cx.doc();
950 let mut doc = cx.doc();