Add a tabWidth parameter to WrapViewLinesToWidth to match gocui

This commit is contained in:
Stefan Haller
2025-02-19 18:23:35 +01:00
parent ac3824bd7c
commit e5137b86cf
4 changed files with 41 additions and 19 deletions

View File

@@ -109,7 +109,7 @@ func ScanLinesAndTruncateWhenLongerThanBuffer(maxBufferSize int) func(data []byt
// - the line indices of the original lines, indexed by the wrapped line indices
// If wrap is false, the text is returned as is.
// This code needs to behave the same as `gocui.lineWrap` does.
func WrapViewLinesToWidth(wrap bool, editable bool, text string, width int) ([]string, []int, []int) {
func WrapViewLinesToWidth(wrap bool, editable bool, text string, width int, tabWidth int) ([]string, []int, []int) {
if !editable {
text = strings.TrimSuffix(text, "\n")
}
@@ -126,14 +126,18 @@ func WrapViewLinesToWidth(wrap bool, editable bool, text string, width int) ([]s
wrappedLineIndices := make([]int, 0, len(lines))
originalLineIndices := make([]int, 0, len(lines))
if tabWidth < 1 {
tabWidth = 4
}
for originalLineIdx, line := range lines {
wrappedLineIndices = append(wrappedLineIndices, len(wrappedLines))
// convert tabs to spaces
for i := 0; i < len(line); i++ {
if line[i] == '\t' {
numSpaces := 4 - (i % 4)
line = line[:i] + " "[:numSpaces] + line[i+1:]
numSpaces := tabWidth - (i % tabWidth)
line = line[:i] + strings.Repeat(" ", numSpaces) + line[i+1:]
i += numSpaces - 1
}
}

View File

@@ -173,6 +173,7 @@ func TestWrapViewLinesToWidth(t *testing.T) {
editable bool
text string
width int
tabWidth int
expectedWrappedLines []string
expectedWrappedLinesIndices []int
expectedOriginalLinesIndices []int
@@ -353,14 +354,25 @@ func TestWrapViewLinesToWidth(t *testing.T) {
},
},
{
name: "Tabs",
wrap: true,
text: "\ta\tbb\tccc\tdddd\teeeee",
width: 50,
name: "Tabs, width 4",
wrap: true,
text: "\ta\tbb\tccc\tdddd\teeeee",
width: 50,
tabWidth: 4,
expectedWrappedLines: []string{
" a bb ccc dddd eeeee",
},
},
{
name: "Tabs, width 8",
wrap: true,
text: "\ta\tbb\tccc\tdddddddd\teeeee",
width: 100,
tabWidth: 8,
expectedWrappedLines: []string{
" a bb ccc dddddddd eeeee",
},
},
{
name: "Multiple lines",
wrap: true,
@@ -425,7 +437,11 @@ func TestWrapViewLinesToWidth(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wrappedLines, wrappedLinesIndices, originalLinesIndices := WrapViewLinesToWidth(tt.wrap, tt.editable, tt.text, tt.width)
tabWidth := tt.tabWidth
if tabWidth == 0 {
tabWidth = 4
}
wrappedLines, wrappedLinesIndices, originalLinesIndices := WrapViewLinesToWidth(tt.wrap, tt.editable, tt.text, tt.width, tabWidth)
assert.Equal(t, tt.expectedWrappedLines, wrappedLines)
if tt.expectedWrappedLinesIndices != nil {
assert.Equal(t, tt.expectedWrappedLinesIndices, wrappedLinesIndices)
@@ -436,6 +452,7 @@ func TestWrapViewLinesToWidth(t *testing.T) {
// As a sanity check, also test that gocui's line wrapping behaves the same way
view := gocui.NewView("", 0, 0, tt.width+1, 1000, gocui.OutputNormal)
view.TabWidth = tabWidth
assert.Equal(t, tt.width, view.InnerWidth())
view.Wrap = tt.wrap
view.Editable = tt.editable