@@ -40,7 +40,7 @@ func newDisplayLineCache() *displayLineCache {
4040
4141// get returns cached rendered lines when the file signature, width, and
4242// highlight state match.
43- func (c * displayLineCache ) get (file * internal.FileDiff , width int , highlightSet map [int ]bool , build func () []string ) []string {
43+ func (c * displayLineCache ) get (file * internal.FileDiff , width int , highlightSet map [lineKey ]bool , build func () []string ) []string {
4444 if c == nil {
4545 return build ()
4646 }
@@ -67,7 +67,7 @@ func (c *displayLineCache) get(file *internal.FileDiff, width int, highlightSet
6767}
6868
6969// newDisplayLineCacheKey fingerprints the rendered inputs that affect visual lines.
70- func newDisplayLineCacheKey (file * internal.FileDiff , width int , highlightSet map [int ]bool ) displayLineCacheKey {
70+ func newDisplayLineCacheKey (file * internal.FileDiff , width int , highlightSet map [lineKey ]bool ) displayLineCacheKey {
7171 key := displayLineCacheKey {width : width }
7272 if file == nil {
7373 return key
@@ -90,7 +90,7 @@ func newDisplayLineCacheKey(file *internal.FileDiff, width int, highlightSet map
9090}
9191
9292// RenderDiffView renders the current file diff within the viewport.
93- func RenderDiffView (file * internal.FileDiff , scrollOffset , width , height int , highlightSet map [int ]bool ) string {
93+ func RenderDiffView (file * internal.FileDiff , scrollOffset , width , height int , highlightSet map [lineKey ]bool ) string {
9494 lines := diffDisplayLines (file , width , highlightSet )
9595 return renderDisplayLines (lines , scrollOffset , height )
9696}
@@ -179,7 +179,7 @@ func renderSeparator(width int) string {
179179}
180180
181181// diffDisplayLines expands one file diff into the exact visual lines shown in the viewport.
182- func diffDisplayLines (file * internal.FileDiff , width int , highlightSet map [int ]bool ) []string {
182+ func diffDisplayLines (file * internal.FileDiff , width int , highlightSet map [lineKey ]bool ) []string {
183183 if file == nil {
184184 return nil
185185 }
@@ -202,17 +202,17 @@ func diffDisplayLines(file *internal.FileDiff, width int, highlightSet map[int]b
202202
203203 var lines []string
204204 for hunkIdx , hunk := range file .Hunks {
205- highlightedHunk := highlightSet [hunkIdx ]
206205 lines = append (lines , renderSeparator (width ))
207- for _ , diffLine := range hunk .Lines {
206+ for lineIdx , diffLine := range hunk .Lines {
207+ highlightedLine := highlightSet [lineKey {HunkIdx : hunkIdx , LineIdx : lineIdx }]
208208 lineNo := displayedLineNo (diffLine )
209209 for i , segment := range wrapLineParts (diffLine .Content , contentWidth ) {
210210 if compactGutter {
211211 prefix := "↳"
212212 if i == 0 {
213213 prefix = diffPrefix (diffLine .Type )
214214 }
215- lines = append (lines , renderDiffSegment ("" , prefix , segment , diffLine .Type , width , file .Path , highlightedHunk ))
215+ lines = append (lines , renderDiffSegment ("" , prefix , segment , diffLine .Type , width , file .Path , highlightedLine ))
216216 continue
217217 }
218218
@@ -222,7 +222,7 @@ func diffDisplayLines(file *internal.FileDiff, width int, highlightSet map[int]b
222222 lineNoText = formatDisplayedLineNo (lineNo , lineNumberWidth )
223223 prefix = diffPrefix (diffLine .Type )
224224 }
225- lines = append (lines , renderDiffSegment (lineNoText , prefix , segment , diffLine .Type , width , file .Path , highlightedHunk ))
225+ lines = append (lines , renderDiffSegment (lineNoText , prefix , segment , diffLine .Type , width , file .Path , highlightedLine ))
226226 }
227227 }
228228 }
@@ -427,20 +427,25 @@ func renderDiffSegment(lineNoText, prefix, code string, lineType internal.LineTy
427427 }
428428}
429429
430- func highlightSignature (highlightSet map [int ]bool ) uint64 {
430+ func highlightSignature (highlightSet map [lineKey ]bool ) uint64 {
431431 if len (highlightSet ) == 0 {
432432 return 0
433433 }
434434
435- indices := make ([]int , 0 , len (highlightSet ))
436- for idx := range highlightSet {
437- indices = append (indices , idx )
435+ keys := make ([]lineKey , 0 , len (highlightSet ))
436+ for key := range highlightSet {
437+ keys = append (keys , key )
438438 }
439- sort .Ints (indices )
439+ sort .Slice (keys , func (i , j int ) bool {
440+ if keys [i ].HunkIdx != keys [j ].HunkIdx {
441+ return keys [i ].HunkIdx < keys [j ].HunkIdx
442+ }
443+ return keys [i ].LineIdx < keys [j ].LineIdx
444+ })
440445
441446 hasher := fnv .New64a ()
442- for _ , idx := range indices {
443- _ , _ = hasher .Write ([]byte (fmt .Sprintf ("%d\x00 " , idx )))
447+ for _ , key := range keys {
448+ _ , _ = hasher .Write ([]byte (fmt .Sprintf ("%d\x00 %d \x00 " , key . HunkIdx , key . LineIdx )))
444449 }
445450
446451 return hasher .Sum64 ()
0 commit comments