From 98365b6bfbc908e140b866eec91c21bfaf02ea3b Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Thu, 2 Mar 2017 19:46:23 -0500 Subject: [PATCH] Fix block indent selection Fixes #572 --- cmd/micro/actions.go | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/cmd/micro/actions.go b/cmd/micro/actions.go index 4f2411ac..422352d7 100644 --- a/cmd/micro/actions.go +++ b/cmd/micro/actions.go @@ -588,14 +588,20 @@ func (v *View) IndentSelection(usePlugin bool) bool { } if v.Cursor.HasSelection() { - startY := v.Cursor.CurSelection[0].Y - endY := v.Cursor.CurSelection[1].Move(-1, v.Buf).Y - endX := v.Cursor.CurSelection[1].Move(-1, v.Buf).X + start := v.Cursor.CurSelection[0] + end := v.Cursor.CurSelection[1] + if end.Y < start.Y { + start, end = end, start + } + + startY := start.Y + endY := end.Move(-1, v.Buf).Y + endX := end.Move(-1, v.Buf).X for y := startY; y <= endY; y++ { tabsize := len(v.Buf.IndentString()) v.Buf.Insert(Loc{0, y}, v.Buf.IndentString()) - if y == startY && v.Cursor.CurSelection[0].X > 0 { - v.Cursor.SetSelectionStart(v.Cursor.CurSelection[0].Move(tabsize, v.Buf)) + if y == startY && start.X > 0 { + v.Cursor.SetSelectionStart(start.Move(tabsize, v.Buf)) } if y == endY { v.Cursor.SetSelectionEnd(Loc{endX + tabsize + 1, endY}) @@ -643,17 +649,23 @@ func (v *View) OutdentSelection(usePlugin bool) bool { } if v.Cursor.HasSelection() { - startY := v.Cursor.CurSelection[0].Y - endY := v.Cursor.CurSelection[1].Move(-1, v.Buf).Y - endX := v.Cursor.CurSelection[1].Move(-1, v.Buf).X + start := v.Cursor.CurSelection[0] + end := v.Cursor.CurSelection[1] + if end.Y < start.Y { + start, end = end, start + } + + startY := start.Y + endY := end.Move(-1, v.Buf).Y + endX := end.Move(-1, v.Buf).X for y := startY; y <= endY; y++ { for x := 0; x < len(v.Buf.IndentString()); x++ { if len(GetLeadingWhitespace(v.Buf.Line(y))) == 0 { break } v.Buf.Remove(Loc{0, y}, Loc{1, y}) - if y == startY && v.Cursor.CurSelection[0].X > 0 { - v.Cursor.SetSelectionStart(v.Cursor.CurSelection[0].Move(-1, v.Buf)) + if y == startY && start.X > 0 { + v.Cursor.SetSelectionStart(start.Move(-1, v.Buf)) } if y == endY { v.Cursor.SetSelectionEnd(Loc{endX - x, endY})