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
This commit is contained in:
Jon Craton
2016-10-15 10:09:20 -04:00
committed by Zachary Yedidia
parent 766f836952
commit fe0dce0960
2 changed files with 27 additions and 40 deletions

View File

@@ -591,17 +591,10 @@ func (v *View) IndentSelection(usePlugin bool) bool {
endY := v.Cursor.CurSelection[1].Move(-1, v.Buf).Y endY := v.Cursor.CurSelection[1].Move(-1, v.Buf).Y
endX := v.Cursor.CurSelection[1].Move(-1, v.Buf).X endX := v.Cursor.CurSelection[1].Move(-1, v.Buf).X
for y := startY; y <= endY; y++ { for y := startY; y <= endY; y++ {
tabsize := 1 tabsize := len(v.Buf.IndentString())
tab := "\t" v.Buf.Insert(Loc{0, y}, v.Buf.IndentString())
if v.Buf.Settings["tabstospaces"].(bool) { if y == startY && v.Cursor.CurSelection[0].X > 0 {
tabsize = int(v.Buf.Settings["tabsize"].(float64)) v.Cursor.SetSelectionStart(v.Cursor.CurSelection[0].Move(tabsize, v.Buf))
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))
}
} }
if y == endY { if y == endY {
v.Cursor.SetSelectionEnd(Loc{endX + tabsize + 1, 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 endY := v.Cursor.CurSelection[1].Move(-1, v.Buf).Y
endX := v.Cursor.CurSelection[1].Move(-1, v.Buf).X endX := v.Cursor.CurSelection[1].Move(-1, v.Buf).X
for y := startY; y <= endY; y++ { for y := startY; y <= endY; y++ {
if len(GetLeadingWhitespace(v.Buf.Line(y))) > 0 { for x := 0; x < len(v.Buf.IndentString()); x++ {
tabsize := 1 if len(GetLeadingWhitespace(v.Buf.Line(y))) == 0 {
if v.Buf.Settings["tabstospaces"].(bool) { break
tabsize = int(v.Buf.Settings["tabsize"].(float64))
} }
for x := 0; x < tabsize; x++ { v.Buf.Remove(Loc{0, y}, Loc{1, y})
if len(GetLeadingWhitespace(v.Buf.Line(y))) == 0 { if y == startY && v.Cursor.CurSelection[0].X > 0 {
break v.Cursor.SetSelectionStart(v.Cursor.CurSelection[0].Move(-1, v.Buf))
} }
v.Buf.Remove(Loc{0, y}, Loc{1, y}) if y == endY {
if y == startY { v.Cursor.SetSelectionEnd(Loc{endX - x, endY})
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})
}
} }
} }
} }
@@ -668,18 +653,11 @@ func (v *View) InsertTab(usePlugin bool) bool {
if v.Cursor.HasSelection() { if v.Cursor.HasSelection() {
return false return false
} }
// Insert a tab
if v.Buf.Settings["tabstospaces"].(bool) { tabBytes := len(v.Buf.IndentString())
tabSize := int(v.Buf.Settings["tabsize"].(float64)) bytesUntilIndent := tabBytes - (v.Cursor.GetVisualX() % tabBytes)
if remainder := v.Cursor.GetVisualX() % tabSize; remainder != 0 { v.Buf.Insert(v.Cursor.Loc, v.Buf.IndentString()[:bytesUntilIndent])
tabSize = tabSize - remainder for i := 0; i < bytesUntilIndent; i++ {
}
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")
v.Cursor.Right() v.Cursor.Right()
} }

View File

@@ -182,6 +182,15 @@ func (b *Buffer) FileType() string {
return b.Settings["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 // CheckModTime makes sure that the file this buffer points to hasn't been updated
// by an external program since it was last read // by an external program since it was last read
// If it has, we ask the user if they would like to reload the file // If it has, we ask the user if they would like to reload the file