on master help sign in
Merge open_above and open_below definitions.
commit77cab5a71518299f47d23a985c08ec90ab1c705e authorBlaž Hrastnik <blaz@mxxn.io> dateThu 1 Apr 2021, 11:36 (6 years ago) parentcc058ad
1 file changed+23−48
helix-term/src/commands.rs+23−48
helix-term/src/commands.rs+23−48
@@ -945,8 +945,12 @@ pub fn append_to_line(cx: &mut Context) {
945945 doc.set_selection(view.id, selection);
946946}
947947
948−// o inserts a new line after each line with a selection
949−pub fn open_below(cx: &mut Context) {
948+enum Open {
949+ Below,
950+ Above,
951+}
952+
953+fn open(cx: &mut Context, open: Open) {
950954 let count = cx.count;
951955 let (view, doc) = cx.current();
952956 enter_insert_mode(doc);
@@ -959,8 +963,14 @@ pub fn open_below(cx: &mut Context) {
959963 let changes: Vec<Change> = lines
960964 .into_iter()
961965 .map(|line| {
962− // adjust all positions to the end of the line (next line minus one)
963− let index = doc.text().line_to_char(line + 1).saturating_sub(1);
966+ let line = match open {
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+
973+ let index = doc.text().line_to_char(line).saturating_sub(1);
964974
965975 // TODO: share logic with insert_newline for indentation
966976 let indent_level = indent::suggested_indent_for_pos(doc.syntax(), text, index, true);
@@ -989,49 +999,14 @@ pub fn open_below(cx: &mut Context) {
989999 doc.apply(&transaction, view.id);
9901000}
9911001
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+
9921007// O inserts a new line before each line with a selection
9931008pub fn open_above(cx: &mut Context) {
994− let count = cx.count;
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);
1009+ open(cx, Open::Above)
10351010}
10361011
10371012pub fn normal_mode(cx: &mut Context) {
@@ -1344,14 +1319,14 @@ pub mod insert {
13441319 let text = doc.text().slice(..);
13451320 let transaction =
13461321 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
1322+ let pos = range.head;
13471323 // 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);
13501325 let indent = doc.indent_unit().repeat(indent_level);
13511326 let mut text = String::with_capacity(1 + indent.len());
13521327 text.push('\n');
13531328 text.push_str(&indent);
1354− (range.head, range.head, Some(text.into()))
1329+ (pos, pos, Some(text.into()))
13551330 });
13561331 doc.apply(&transaction, view.id);
13571332 }