on master help sign in
commands: refactor open_below to support count.
commit5fa1ba6b1c0a3ac4f93112b94be8477dc4d06b46 authorBlaž Hrastnik <blaz@mxxn.io> dateThu 25 Feb 2021, 16:47 (6 years ago) parent6bd16a7
1 file changed+34−29
helix-term/src/commands.rs+34−29
helix-term/src/commands.rs+34−29
@@ -5,7 +5,7 @@ use helix_core::{
55 regex::{self, Regex},
66 register, selection,
77 state::{Direction, Granularity, State},
8− ChangeSet, Range, Selection, Tendril, Transaction,
8+ Change, ChangeSet, Range, Selection, Tendril, Transaction,
99};
1010
1111use once_cell::sync::Lazy;
@@ -588,47 +588,51 @@ pub fn append_to_line(cx: &mut Context) {
588588
589589// o inserts a new line after each line with a selection
590590pub fn open_below(cx: &mut Context) {
591+ let count = cx.count;
591592 let doc = cx.doc();
592593 enter_insert_mode(doc);
593594
594595 let lines = selection_lines(&doc.state);
595596
596597 let positions = lines.into_iter().map(|index| {
597− // adjust all positions to the end of the line/start of the next one.
598− doc.text().line_to_char(index + 1)
598+ // adjust all positions to the end of the line (next line minus one)
599+ doc.text().line_to_char(index + 1).saturating_sub(1)
599600 });
600601
601− let changes = positions.map(|index| {
602− // TODO: share logic with insert_newline for indentation
603− let indent_level = helix_core::indent::suggested_indent_for_pos(
604− doc.syntax.as_ref(),
605− &doc.state,
606− index - 1, // need to match the indentation to the prev line
607− true,
608− );
609− let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
610− let mut text = String::with_capacity(1 + indent.len());
611− text.push_str(&indent);
612− text.push('\n');
613−
614− // TODO: ideally we want to run a hook over the transactions to figure out and reindent all
615− // \n's as a post-processing step?
616− // behaviors:
617− // - on insert mode enter: we add newline + indent and position cursor at the end
618− // - on 3o/3O: we insert 3 newlines + indents each and position cursors at ends
619−
620− // generate changes
621− (index, index, Some(text.into()))
622− });
602+ let changes: Vec<Change> = positions
603+ .map(|index| {
604+ // TODO: share logic with insert_newline for indentation
605+ let indent_level = helix_core::indent::suggested_indent_for_pos(
606+ doc.syntax.as_ref(),
607+ &doc.state,
608+ index,
609+ true,
610+ );
611+ let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
612+ let mut text = String::with_capacity(1 + indent.len());
613+ text.push('\n');
614+ text.push_str(&indent);
615+ let text = text.repeat(count);
616+
617+ // TODO: ideally we want to run a hook over the transactions to figure out and reindent all
618+ // \n's as a post-processing step?
619+ // behaviors:
620+ // - on insert mode enter: we add newline + indent and position cursor at the end
621+ // - on 3o/3O: we insert 3 newlines + indents each and position cursors at ends
622+
623+ // generate changes
624+ (index, index, Some(text.into()))
625+ })
626+ .collect();
623627
624628 // TODO: count actually inserts "n" new lines and starts editing on all of them.
625629 // TODO: append "count" newlines and modify cursors to those lines
626630
627631 let selection = Selection::new(
628632 changes
629− .clone()
630− .map(|(start, end, text): (usize, usize, Option<Tendril>)| {
631− let len = text.map(|text| text.len()).unwrap() - 1; // minus newline
633+ .iter()
634+ .map(|(start, _end, text): &Change| {
635+ let len = text.as_ref().map(|text| text.len()).unwrap(); // minus newline
632636 let pos = start + len;
633637 Range::new(pos, pos)
634638 })
@@ -636,7 +640,8 @@ pub fn open_below(cx: &mut Context) {
636640 0,
637641 );
638642
639− let transaction = Transaction::change(&doc.state, changes).with_selection(selection);
643+ let transaction =
644+ Transaction::change(&doc.state, changes.into_iter()).with_selection(selection);
640645
641646 doc.apply(&transaction);
642647}