Skip to content

Commit

Permalink
highlighter: Properly handle rehighlighting within Highlight()
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKar committed Apr 15, 2024
1 parent 1a436a5 commit e9f848d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pkg/highlight/highlighter.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ func (h *Highlighter) HighlightString(input string) []LineMatch {
return lineMatches
}

// Highlight sets the state and matches for each line from startline to endline
// Highlight sets the state and matches for each line beginning with the startline
// until it detects an unchanged state from the endline downwards.
// It sets all other matches in the buffer to nil to conserve memory
func (h *Highlighter) Highlight(input LineStates, startline, endline int) {
var curState *region
Expand All @@ -466,7 +467,9 @@ func (h *Highlighter) Highlight(input LineStates, startline, endline int) {
input.Unlock()
}

for i := startline; i <= endline; i++ {
for i := startline; ; i++ {
var lastState *region

input.Lock()
if i >= input.LinesNum() {
input.Unlock()
Expand All @@ -479,9 +482,17 @@ func (h *Highlighter) Highlight(input LineStates, startline, endline int) {
match, newState := h.highlight(highlights, 0, i, line, curState)
curState = newState

if i >= endline {
lastState = input.State(i)
}

input.SetState(i, curState)
input.SetMatch(i, match)
input.Unlock()

if i >= endline && curState == lastState {
break
}
}
}

Expand Down

0 comments on commit e9f848d

Please sign in to comment.