on master help sign in
open_below is now indentation-aware.
commitb2800489dedb44cf030cd425d737ea14491518aa authorBlaž Hrastnik <blaz@mxxn.io> dateWed 6 Jan 2021, 15:13 (6 years ago) parent8b95c33
1 file changed+27−7
helix-term/src/commands.rs+27−7
helix-term/src/commands.rs+27−7
@@ -529,19 +529,39 @@ pub fn open_below(cx: &mut Context) {
529529 })
530530 .collect();
531531
532− // TODO: use same logic as insert_newline for indentation
533− let changes = positions.iter().copied().map(|index|
532+ let changes = positions.iter().copied().map(|index| {
533+ // TODO: share logic with insert_newline for indentation
534+ let indent_level = helix_core::indent::suggested_indent_for_pos(
535+ cx.view.doc.syntax.as_ref(),
536+ &cx.view.doc.state,
537+ index,
538+ );
539+ let indent = " ".repeat(TAB_WIDTH).repeat(indent_level);
540+ let mut text = String::with_capacity(1 + indent.len());
541+ text.push_str(&indent);
542+ text.push('\n');
543+
544+ // TODO: ideally we want to run a hook over the transactions to figure out and reindent all
545+ // \n's as a post-processing step?
546+ // behaviors:
547+ // - on insert mode enter: we add newline + indent and position cursor at the end
548+ // - on 3o/3O: we insert 3 newlines + indents each and position cursors at ends
549+
534550 // generate changes
535− (index, index, Some(Tendril::from_char('\n'))));
551+ (index, index, Some(text.into()))
552+ });
536553
537554 // TODO: count actually inserts "n" new lines and starts editing on all of them.
538555 // TODO: append "count" newlines and modify cursors to those lines
539556
540557 let selection = Selection::new(
541− positions
542− .iter()
543− .copied()
544− .map(|pos| Range::new(pos, pos))
558+ changes
559+ .clone()
560+ .map(|(start, end, text): (usize, usize, Option<Tendril>)| {
561+ let len = text.map(|text| text.len()).unwrap() - 1; // minus newline
562+ let pos = start + len;
563+ Range::new(pos, pos)
564+ })
545565 .collect(),
546566 0,
547567 );