twwidth: purge width cache in SetTabWidth
the width cache key does not include the tab width, so after SetTabWidth, Width returned stale results for strings containing tabs. purge the cache like SetForceNarrow does, and add a test comparing Width with WidthNoCache after changing the tab width.
This commit is contained in:
1 parent
2383574d62
commit
d75166c01a
2 files changed
+16
No files matched your search
@@ -280,6 +280,9 @@ func TabWidth() int {
|
||||
// SetTabWidth sets the global tab width.
|
||||
func SetTabWidth(w int) {
|
||||
TabInstance().SetWidth(w)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
widthCache.Purge() // Clear cache because widths might change
|
||||
}
|
||||
|
||||
func envInt(k string) int {
|
||||
|
||||
@@ -38,3 +38,16 @@ func TestTabinalSetWidth(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetTabWidthPurgesWidthCache(t *testing.T) {
|
||||
orig := twwidth.TabWidth()
|
||||
t.Cleanup(func() { twwidth.SetTabWidth(orig) })
|
||||
|
||||
const s = "a\tb"
|
||||
for _, width := range []int{2, 4, 6} {
|
||||
twwidth.SetTabWidth(width)
|
||||
if got, want := twwidth.Width(s), twwidth.WidthNoCache(s); got != want {
|
||||
t.Errorf("Width(%q) after SetTabWidth(%d) = %d, want %d", s, width, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user