Skip to content

Commit 7b96ddd

Browse files
committed
fix: cleanups
1 parent 2a3bb65 commit 7b96ddd

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

viewport/highlight.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func parseMatches(
5959
hi.lineEnd = line
6060

6161
graphemeStart := graphemePos
62-
graphemeEnd := graphemePos
6362

6463
// loop until we find the end
6564
for byteEnd > bytePos {
@@ -69,9 +68,8 @@ func parseMatches(
6968

7069
// if it ends with a new line, add the range, increase line, and continue
7170
if content[bytePos] == '\n' {
72-
graphemeEnd = graphemePos
7371
colstart := max(0, graphemeStart-previousLinesOffset)
74-
colend := max(graphemeEnd-previousLinesOffset+1, colstart) // +1 its \n itself
72+
colend := max(graphemePos-previousLinesOffset+1, colstart) // +1 its \n itself
7573

7674
// fmt.Printf(
7775
// "nl line=%d linestart=%d lineend=%d colstart=%d colend=%d start=%d end=%d processed=%d width=%d\n",
@@ -93,9 +91,8 @@ func parseMatches(
9391

9492
// we found it!, add highlight and continue
9593
if bytePos == byteEnd {
96-
graphemeEnd = graphemePos
9794
colstart := max(0, graphemeStart-previousLinesOffset)
98-
colend := max(graphemeEnd-previousLinesOffset, colstart)
95+
colend := max(graphemePos-previousLinesOffset, colstart)
9996

10097
// fmt.Printf(
10198
// "no line=%d linestart=%d lineend=%d colstart=%d colend=%d start=%d end=%d processed=%d width=%d\n",
@@ -106,7 +103,6 @@ func parseMatches(
106103
hi.lines[line] = [2]int{colstart, colend}
107104
hi.lineEnd = line
108105
}
109-
110106
}
111107

112108
highlights = append(highlights, hi)
@@ -125,7 +121,7 @@ type highlightInfo struct {
125121

126122
// coords returns the line x column of this highlight.
127123
func (hi highlightInfo) coords() (line int, col int) {
128-
for i := hi.lineStart; i <= hi.lineEnd; i++ {
124+
for i := hi.lineStart; i < hi.lineEnd; i++ {
129125
hl, ok := hi.lines[i]
130126
if !ok {
131127
continue

viewport/viewport.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ type Model struct {
8282

8383
initialized bool
8484
lines []string
85-
lineWidths []int
8685
longestLineWidth int
8786

8887
// HighlightStyle highlights the ranges set with [SetHighligths].
@@ -190,7 +189,7 @@ func (m *Model) SetContent(s string) {
190189
if len(m.lines) == 1 && ansi.StringWidth(m.lines[0]) == 0 {
191190
m.lines = nil
192191
}
193-
m.lineWidths, m.longestLineWidth = calcLineWidths(m.lines)
192+
m.longestLineWidth = maxLineWidth(m.lines)
194193
m.ClearHighlights()
195194

196195
if m.YOffset > len(m.lines)-1 {
@@ -730,15 +729,13 @@ func max(a, b int) int {
730729
return b
731730
}
732731

733-
func calcLineWidths(lines []string) ([]int, int) {
732+
func maxLineWidth(lines []string) int {
734733
maxlen := 0
735-
all := make([]int, 0, len(lines))
736734
for _, line := range lines {
737735
llen := ansi.StringWidth(line)
738-
all = append(all, llen+1) // account for "\n"
739736
if llen > maxlen {
740737
maxlen = llen
741738
}
742739
}
743-
return all, maxlen
740+
return maxlen
744741
}

0 commit comments

Comments
 (0)