diff --git a/pkg/highlight/highlighter.go b/pkg/highlight/highlighter.go index 808af5ed9b..377d375ac8 100644 --- a/pkg/highlight/highlighter.go +++ b/pkg/highlight/highlighter.go @@ -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 @@ -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() @@ -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 + } } }