Skip to content

Commit

Permalink
highlighter: Properly handle rehighlighting within Highlight()
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitry Maluka <[email protected]>
  • Loading branch information
JoeKar and dmaluka committed Oct 20, 2024
1 parent e9c77fa commit fb2ed1b
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/highlight/highlighter.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,10 @@ func (h *Highlighter) HighlightString(input string) []LineMatch {
return lineMatches
}

// Highlight sets the state and matches for each line from startline to endline
// It sets all other matches in the buffer to nil to conserve memory
// Highlight sets the state and matches for each line from startline to endline,
// and also for some amount of lines after endline, until it detects a line
// whose state does not change, which means that the lines after it do not change
// their highlighting and therefore do not need to be updated.
func (h *Highlighter) Highlight(input LineStates, startline, endline int) {
var curState *region
if startline > 0 {
Expand All @@ -459,7 +461,7 @@ func (h *Highlighter) Highlight(input LineStates, startline, endline int) {
input.Unlock()
}

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

var lastState *region
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 fb2ed1b

Please sign in to comment.