on master help sign in
Merge open_above and open_below definitions.
77cab5aBlaž Hrastnik
helix-term/src/commands.rs+23−48
@@ -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);
946}
946}
947
947
948−// o inserts a new line after each line with a selection
948+enum Open {
949+ Below,
950+ Above,
951+}
952+
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,
971+ };
972+
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);
964
974
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);
990}
1000}
991
1001
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)
1005+}
1006+
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);
997−
998− let text = doc.text().slice(..);
999− let lines = selection_lines(doc.text(), doc.selection(view.id));
1000−
1001− let mut ranges = SmallVec::with_capacity(lines.len());
1002−
1003− let changes: Vec<Change> = lines
1004− .into_iter()
1005− .map(|line| {
1006− // adjust all positions to the end of the previous line
1007− let index = doc.text().line_to_char(line).saturating_sub(1);
1008−
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());
1013− text.push('\n');
1014− text.push_str(&indent);
1015− let text = text.repeat(count);
1016−
1017− // calculate new selection range
1018− let pos = index + text.len();
1019− ranges.push(Range::new(pos, pos));
1020−
1021− // generate changes
1022− (index, index, Some(text.into()))
1023− })
1024− .collect();
1025−
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
1028−
1029− let selection = Selection::new(ranges, 0);
1030−
1031− let transaction =
1032− Transaction::change(doc.text(), changes.into_iter()).with_selection(selection);
1033−
1034− doc.apply(&transaction, view.id);
1035}
1010}
1036
1011
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?
1348− let indent_level =
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()))
1355 });
1330 });
1356 doc.apply(&transaction, view.id);
1331 doc.apply(&transaction, view.id);
1357 }
1332 }