From fe0dce0960c00a5b7da92200416a82250e1fc2f4 Mon Sep 17 00:00:00 2001 From: Jon Craton Date: Sat, 15 Oct 2016 10:09:20 -0400 Subject: [PATCH] Added IndentString method on Buffer (#415) * Added IndentString function to retrun the string used for indentation (n-spaces or a tab) based on buffer settings * Combined redundant statements * Removed duplicate leading whitespace check * Better IndentString description * Fixed remainder logic that I broke --- cmd/micro/actions.go | 58 ++++++++++++++------------------------------ cmd/micro/buffer.go | 9 +++++++ 2 files changed, 27 insertions(+), 40 deletions(-) diff --git a/cmd/micro/actions.go b/cmd/micro/actions.go index 0def2e39..dfba85c3 100644 --- a/cmd/micro/actions.go +++ b/cmd/micro/actions.go @@ -591,17 +591,10 @@ func (v *View) IndentSelection(usePlugin bool) bool { endY := v.Cursor.CurSelection[1].Move(-1, v.Buf).Y endX := v.Cursor.CurSelection[1].Move(-1, v.Buf).X for y := startY; y <= endY; y++ { - tabsize := 1 - tab := "\t" - if v.Buf.Settings["tabstospaces"].(bool) { - tabsize = int(v.Buf.Settings["tabsize"].(float64)) - tab = Spaces(tabsize) - } - v.Buf.Insert(Loc{0, y}, tab) - if y == startY { - if v.Cursor.CurSelection[0].X > 0 { - v.Cursor.SetSelectionStart(v.Cursor.CurSelection[0].Move(tabsize, v.Buf)) - } + 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 == endY { v.Cursor.SetSelectionEnd(Loc{endX + tabsize + 1, endY}) @@ -628,24 +621,16 @@ func (v *View) OutdentSelection(usePlugin bool) bool { endY := v.Cursor.CurSelection[1].Move(-1, v.Buf).Y endX := v.Cursor.CurSelection[1].Move(-1, v.Buf).X for y := startY; y <= endY; y++ { - if len(GetLeadingWhitespace(v.Buf.Line(y))) > 0 { - tabsize := 1 - if v.Buf.Settings["tabstospaces"].(bool) { - tabsize = int(v.Buf.Settings["tabsize"].(float64)) + for x := 0; x < len(v.Buf.IndentString()); x++ { + if len(GetLeadingWhitespace(v.Buf.Line(y))) == 0 { + break } - for x := 0; x < tabsize; x++ { - if len(GetLeadingWhitespace(v.Buf.Line(y))) == 0 { - break - } - v.Buf.Remove(Loc{0, y}, Loc{1, y}) - if y == startY { - if v.Cursor.CurSelection[0].X > 0 { - v.Cursor.SetSelectionStart(v.Cursor.CurSelection[0].Move(-1, v.Buf)) - } - } - if y == endY { - v.Cursor.SetSelectionEnd(Loc{endX - x, endY}) - } + 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 == endY { + v.Cursor.SetSelectionEnd(Loc{endX - x, endY}) } } } @@ -668,18 +653,11 @@ func (v *View) InsertTab(usePlugin bool) bool { if v.Cursor.HasSelection() { return false } - // Insert a tab - if v.Buf.Settings["tabstospaces"].(bool) { - tabSize := int(v.Buf.Settings["tabsize"].(float64)) - if remainder := v.Cursor.GetVisualX() % tabSize; remainder != 0 { - tabSize = tabSize - remainder - } - v.Buf.Insert(v.Cursor.Loc, Spaces(tabSize)) - for i := 0; i < tabSize; i++ { - v.Cursor.Right() - } - } else { - v.Buf.Insert(v.Cursor.Loc, "\t") + + tabBytes := len(v.Buf.IndentString()) + bytesUntilIndent := tabBytes - (v.Cursor.GetVisualX() % tabBytes) + v.Buf.Insert(v.Cursor.Loc, v.Buf.IndentString()[:bytesUntilIndent]) + for i := 0; i < bytesUntilIndent; i++ { v.Cursor.Right() } diff --git a/cmd/micro/buffer.go b/cmd/micro/buffer.go index 99edacf7..d7f6c78d 100644 --- a/cmd/micro/buffer.go +++ b/cmd/micro/buffer.go @@ -182,6 +182,15 @@ func (b *Buffer) FileType() string { return b.Settings["filetype"].(string) } +// IndentString returns a string representing one level of indentation +func (b *Buffer) IndentString() string { + if b.Settings["tabstospaces"].(bool) { + return Spaces(int(b.Settings["tabsize"].(float64))) + } else { + return "\t" + } +} + // CheckModTime makes sure that the file this buffer points to hasn't been updated // by an external program since it was last read // If it has, we ask the user if they would like to reload the file