mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-30 14:47:16 +09:00
fixes #379 (second try)
This commit is contained in:
@@ -157,7 +157,19 @@ func GetModTime(path string) (time.Time, bool) {
|
|||||||
// StringWidth returns the width of a string where tabs count as `tabsize` width
|
// StringWidth returns the width of a string where tabs count as `tabsize` width
|
||||||
func StringWidth(str string, tabsize int) int {
|
func StringWidth(str string, tabsize int) int {
|
||||||
sw := runewidth.StringWidth(str)
|
sw := runewidth.StringWidth(str)
|
||||||
sw += NumOccurrences(str, '\t') * (tabsize - 1)
|
lineIdx := 0
|
||||||
|
for _, ch := range str {
|
||||||
|
switch ch {
|
||||||
|
case '\t':
|
||||||
|
ts := tabsize - (lineIdx % tabsize)
|
||||||
|
sw += ts - 1
|
||||||
|
lineIdx += ts
|
||||||
|
case '\n':
|
||||||
|
lineIdx = 0
|
||||||
|
default:
|
||||||
|
lineIdx++
|
||||||
|
}
|
||||||
|
}
|
||||||
return sw
|
return sw
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,16 +177,22 @@ func StringWidth(str string, tabsize int) int {
|
|||||||
// that have a width larger than 1 (this also counts tabs as `tabsize` width)
|
// that have a width larger than 1 (this also counts tabs as `tabsize` width)
|
||||||
func WidthOfLargeRunes(str string, tabsize int) int {
|
func WidthOfLargeRunes(str string, tabsize int) int {
|
||||||
count := 0
|
count := 0
|
||||||
|
lineIdx := 0
|
||||||
for _, ch := range str {
|
for _, ch := range str {
|
||||||
var w int
|
var w int
|
||||||
if ch == '\t' {
|
if ch == '\t' {
|
||||||
w = tabsize
|
w = tabsize - (lineIdx % tabsize)
|
||||||
} else {
|
} else {
|
||||||
w = runewidth.RuneWidth(ch)
|
w = runewidth.RuneWidth(ch)
|
||||||
}
|
}
|
||||||
if w > 1 {
|
if w > 1 {
|
||||||
count += (w - 1)
|
count += (w - 1)
|
||||||
}
|
}
|
||||||
|
if ch == '\n' {
|
||||||
|
lineIdx = 0
|
||||||
|
} else {
|
||||||
|
lineIdx += w
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,3 +116,41 @@ func TestJoinAndSplitCommandArgs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStringWidth(t *testing.T) {
|
||||||
|
tabsize := 4
|
||||||
|
if w := StringWidth("1\t2", tabsize); w != 5 {
|
||||||
|
t.Error("StringWidth 1 Failed. Got", w)
|
||||||
|
}
|
||||||
|
if w := StringWidth("\t", tabsize); w != 4 {
|
||||||
|
t.Error("StringWidth 2 Failed. Got", w)
|
||||||
|
}
|
||||||
|
if w := StringWidth("1\t", tabsize); w != 4 {
|
||||||
|
t.Error("StringWidth 3 Failed. Got", w)
|
||||||
|
}
|
||||||
|
if w := StringWidth("\t\t", tabsize); w != 8 {
|
||||||
|
t.Error("StringWidth 4 Failed. Got", w)
|
||||||
|
}
|
||||||
|
if w := StringWidth("12\t2\t", tabsize); w != 8 {
|
||||||
|
t.Error("StringWidth 5 Failed. Got", w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWidthOfLargeRunes(t *testing.T) {
|
||||||
|
tabsize := 4
|
||||||
|
if w := WidthOfLargeRunes("1\t2", tabsize); w != 2 {
|
||||||
|
t.Error("WidthOfLargeRunes 1 Failed. Got", w)
|
||||||
|
}
|
||||||
|
if w := WidthOfLargeRunes("\t", tabsize); w != 3 {
|
||||||
|
t.Error("WidthOfLargeRunes 2 Failed. Got", w)
|
||||||
|
}
|
||||||
|
if w := WidthOfLargeRunes("1\t", tabsize); w != 2 {
|
||||||
|
t.Error("WidthOfLargeRunes 3 Failed. Got", w)
|
||||||
|
}
|
||||||
|
if w := WidthOfLargeRunes("\t\t", tabsize); w != 6 {
|
||||||
|
t.Error("WidthOfLargeRunes 4 Failed. Got", w)
|
||||||
|
}
|
||||||
|
if w := WidthOfLargeRunes("12\t2\t", tabsize); w != 3 {
|
||||||
|
t.Error("WidthOfLargeRunes 5 Failed. Got", w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -777,7 +777,9 @@ func (v *View) DisplayView() {
|
|||||||
}
|
}
|
||||||
// Now the tab has to be displayed as a bunch of spaces
|
// Now the tab has to be displayed as a bunch of spaces
|
||||||
tabSize := int(v.Buf.Settings["tabsize"].(float64))
|
tabSize := int(v.Buf.Settings["tabsize"].(float64))
|
||||||
for i := 0; i < tabSize-1; i++ {
|
visLoc := StringWidth(line[:colN], tabSize)
|
||||||
|
remainder := tabSize - (visLoc % tabSize)
|
||||||
|
for i := 0; i < remainder-1; i++ {
|
||||||
screenX++
|
screenX++
|
||||||
if screenX-v.x-v.leftCol >= v.lineNumOffset {
|
if screenX-v.x-v.leftCol >= v.lineNumOffset {
|
||||||
v.drawCell(screenX-v.leftCol, screenY, ' ', nil, lineStyle)
|
v.drawCell(screenX-v.leftCol, screenY, ' ', nil, lineStyle)
|
||||||
|
|||||||
Reference in New Issue
Block a user