Skip to content

Commit 9967d0e

Browse files
authored
Merge pull request #67 from jesseduffield/fix-lineWrap-issues
Fix lineWrap issues
2 parents 68c437b + 99b952c commit 9967d0e

File tree

3 files changed

+65
-33
lines changed

3 files changed

+65
-33
lines changed

gui.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func (g *Gui) SetView(name string, x0, y0, x1, y1 int, overlaps byte) (*View, er
335335

336336
g.Mutexes.ViewsMutex.Lock()
337337

338-
v := newView(name, x0, y0, x1, y1, g.outputMode)
338+
v := NewView(name, x0, y0, x1, y1, g.outputMode)
339339
v.BgColor, v.FgColor = g.BgColor, g.FgColor
340340
v.SelBgColor, v.SelFgColor = g.SelBgColor, g.SelFgColor
341341
v.Overlaps = overlaps

view.go

+22-31
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ func (l lineType) String() string {
402402
return str
403403
}
404404

405-
// newView returns a new View object.
406-
func newView(name string, x0, y0, x1, y1 int, mode OutputMode) *View {
405+
// NewView returns a new View object.
406+
func NewView(name string, x0, y0, x1, y1 int, mode OutputMode) *View {
407407
v := &View{
408408
name: name,
409409
x0: x0,
@@ -494,31 +494,15 @@ func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) {
494494
bgColor = v.BgColor
495495
ch = v.Mask
496496
} else if v.Highlight {
497-
var ry, rcy int
498-
499-
_, ry, ok := v.realPosition(x, y)
500-
if !ok {
501-
return
502-
}
503-
_, rrcy, ok := v.realPosition(v.cx, v.cy)
504-
// out of bounds is fine
505-
if ok {
506-
rcy = rrcy
507-
}
508-
509-
rangeSelectStart := rcy
510-
rangeSelectEnd := rcy
497+
rangeSelectStart := v.cy
498+
rangeSelectEnd := v.cy
511499
if v.rangeSelectStartY != -1 {
512-
_, realRangeSelectStart, ok := v.realPosition(0, v.rangeSelectStartY-v.oy)
513-
if !ok {
514-
return
515-
}
516-
517-
rangeSelectStart = min(realRangeSelectStart, rcy)
518-
rangeSelectEnd = max(realRangeSelectStart, rcy)
500+
relativeRangeSelectStart := v.rangeSelectStartY - v.oy
501+
rangeSelectStart = min(relativeRangeSelectStart, v.cy)
502+
rangeSelectEnd = max(relativeRangeSelectStart, v.cy)
519503
}
520504

521-
if ry >= rangeSelectStart && ry <= rangeSelectEnd {
505+
if y >= rangeSelectStart && y <= rangeSelectEnd {
522506
// this ensures we use the bright variant of a colour upon highlight
523507
fgColorComponent := fgColor & ^AttrAll
524508
if fgColorComponent >= AttrIsValidColor && fgColorComponent < AttrIsValidColor+8 {
@@ -1103,6 +1087,8 @@ func (v *View) updateSearchPositions() {
11031087

11041088
if v.searcher.modelSearchResults != nil {
11051089
for _, result := range v.searcher.modelSearchResults {
1090+
// This code only works when v.Wrap is false.
1091+
11061092
if result.Y >= len(v.lines) {
11071093
break
11081094
}
@@ -1131,8 +1117,9 @@ func (v *View) updateSearchPositions() {
11311117
}
11321118
}
11331119
} else {
1134-
for y, line := range v.lines {
1135-
v.searcher.searchPositions = append(v.searcher.searchPositions, searchPositionsForLine(line, y)...)
1120+
v.refreshViewLinesIfNeeded()
1121+
for y, line := range v.viewLines {
1122+
v.searcher.searchPositions = append(v.searcher.searchPositions, searchPositionsForLine(line.line, y)...)
11361123
}
11371124
}
11381125
}
@@ -1373,6 +1360,8 @@ func (v *View) ViewBufferLines() []string {
13731360
v.writeMutex.Lock()
13741361
defer v.writeMutex.Unlock()
13751362

1363+
v.refreshViewLinesIfNeeded()
1364+
13761365
lines := make([]string, len(v.viewLines))
13771366
for i, l := range v.viewLines {
13781367
str := lineType(l.line).String()
@@ -1512,18 +1501,20 @@ func lineWrap(line []cell, columns int) [][]cell {
15121501
lines = append(lines, line[offset:i])
15131502
offset = i
15141503
n = rw
1515-
} else if lastWhitespaceIndex != -1 && lastWhitespaceIndex+1 != i {
1504+
} else if lastWhitespaceIndex != -1 {
15161505
// if there is a space in the line and the line is not breaking at a space/hyphen
15171506
if line[lastWhitespaceIndex].chr == '-' {
15181507
// if break occurs at hyphen, we'll retain the hyphen
15191508
lines = append(lines, line[offset:lastWhitespaceIndex+1])
1520-
offset = lastWhitespaceIndex + 1
1521-
n = i - offset
15221509
} else {
15231510
// if break occurs at space, we'll omit the space
15241511
lines = append(lines, line[offset:lastWhitespaceIndex])
1525-
offset = lastWhitespaceIndex + 1
1526-
n = i - offset + 1
1512+
}
1513+
// Either way, continue *after* the break
1514+
offset = lastWhitespaceIndex + 1
1515+
n = 0
1516+
for _, c := range line[offset : i+1] {
1517+
n += runewidth.RuneWidth(c.chr)
15271518
}
15281519
} else {
15291520
// in this case we're breaking mid-word

view_test.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,48 @@ func TestLineWrap(t *testing.T) {
221221
expected: []string{
222222
"one-two-",
223223
"three-",
224-
"four-five",
224+
"four-",
225+
"five",
226+
},
227+
},
228+
{
229+
name: "Several lines using all the available width",
230+
line: "aaa bb cc ddd-ee ff",
231+
columns: 5,
232+
expected: []string{
233+
"aaa",
234+
"bb cc",
235+
"ddd-",
236+
"ee ff",
237+
},
238+
},
239+
{
240+
name: "Multi-cell runes",
241+
line: "🐤🐤🐤 🐝🐝 🙉 🦊🦊🦊-🐬🐬 🦢🦢",
242+
columns: 9,
243+
expected: []string{
244+
"🐤🐤🐤",
245+
"🐝🐝 🙉",
246+
"🦊🦊🦊-",
247+
"🐬🐬 🦢🦢",
248+
},
249+
},
250+
{
251+
name: "Space in last column",
252+
line: "hello world",
253+
columns: 6,
254+
expected: []string{
255+
"hello",
256+
"world",
257+
},
258+
},
259+
{
260+
name: "Hyphen in last column",
261+
line: "hello-world",
262+
columns: 6,
263+
expected: []string{
264+
"hello-",
265+
"world",
225266
},
226267
},
227268
{

0 commit comments

Comments
 (0)