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,18 +591,11 @@ 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 {
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,27 +621,19 @@ 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 < tabsize; x++ {
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 {
if v.Cursor.CurSelection[0].X > 0 {
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})
}
}
}
}
v.Cursor.Relocate()
if usePlugin {
@ -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()
}

View file

@ -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