on master help sign in
< as unindent
commit0826739c6211a342ebf0a58e5364d496cbe50f08 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 13 Oct 2020, 23:08 (6 years ago) parentc7616d2
1 file changed+35−2
helix-view/src/commands.rs+35−2
helix-view/src/commands.rs+35−2
@@ -543,7 +543,7 @@ pub fn paste(view: &mut View, _count: usize) {
543543
544544const TAB_WIDTH: usize = 4;
545545
546−pub fn indent(view: &mut View, _count: usize) {
546+fn get_lines(view: &View) -> Vec<usize> {
547547 let mut lines = Vec::new();
548548
549549 // Get all line numbers
@@ -557,6 +557,11 @@ pub fn indent(view: &mut View, _count: usize) {
557557 }
558558 lines.sort_unstable(); // sorting by usize so _unstable is preferred
559559 lines.dedup();
560+ lines
561+}
562+
563+pub fn indent(view: &mut View, _count: usize) {
564+ let lines = get_lines(view);
560565
561566 // Indent by one level
562567 let indent = Tendril::from(" ".repeat(TAB_WIDTH));
@@ -573,5 +578,33 @@ pub fn indent(view: &mut View, _count: usize) {
573578}
574579
575580pub fn unindent(view: &mut View, _count: usize) {
576− unimplemented!()
581+ let lines = get_lines(view);
582+ let mut changes = Vec::with_capacity(lines.len());
583+
584+ for line_idx in lines {
585+ let line = view.state.doc.line(line_idx);
586+ let mut width = 0;
587+
588+ for ch in line.chars() {
589+ match ch {
590+ ' ' => width += 1,
591+ '\t' => width = (width / TAB_WIDTH + 1) * TAB_WIDTH,
592+ _ => break,
593+ }
594+
595+ if width >= TAB_WIDTH {
596+ break;
597+ }
598+ }
599+
600+ if width > 0 {
601+ let start = view.state.doc.line_to_char(line_idx);
602+ changes.push((start, start + width, None))
603+ }
604+ }
605+
606+ let transaction = Transaction::change(&view.state, changes.into_iter());
607+
608+ transaction.apply(&mut view.state);
609+ append_changes_to_history(view);
577610}