@@ -447,8 +447,10 @@ func (h *Highlighter) HighlightString(input string) []LineMatch {
447447 return lineMatches
448448}
449449
450- // Highlight sets the state and matches for each line from startline to endline
451- // It sets all other matches in the buffer to nil to conserve memory
450+ // Highlight sets the state and matches for each line from startline to endline,
451+ // and also for some amount of lines after endline, until it detects a line
452+ // whose state does not change, which means that the lines after it do not change
453+ // their highlighting and therefore do not need to be updated.
452454func (h * Highlighter ) Highlight (input LineStates , startline , endline int ) {
453455 var curState * region
454456 if startline > 0 {
@@ -459,7 +461,7 @@ func (h *Highlighter) Highlight(input LineStates, startline, endline int) {
459461 input .Unlock ()
460462 }
461463
462- for i := startline ; i <= endline ; i ++ {
464+ for i := startline ; ; i ++ {
463465 input .Lock ()
464466 if i >= input .LinesNum () {
465467 input .Unlock ()
@@ -472,9 +474,18 @@ func (h *Highlighter) Highlight(input LineStates, startline, endline int) {
472474 match , newState := h .highlight (highlights , 0 , i , line , curState )
473475 curState = newState
474476
477+ var lastState * region
478+ if i >= endline {
479+ lastState = input .State (i )
480+ }
481+
475482 input .SetState (i , curState )
476483 input .SetMatch (i , match )
477484 input .Unlock ()
485+
486+ if i >= endline && curState == lastState {
487+ break
488+ }
478489 }
479490}
480491
0 commit comments