@@ -378,13 +378,14 @@ regionLoop:
378378 }
379379}
380380
381- func (h * Highlighter ) highlight (highlights LineMatch , start int , lineNum int , line []byte , curRegion * region ) LineMatch {
381+ func (h * Highlighter ) highlight (highlights LineMatch , start int , lineNum int , line []byte , curRegion * region ) ( LineMatch , * region ) {
382382 lineLen := util .CharacterCount (line )
383383 // log.Println("highlight: lineNum:", lineNum, "start:", start, "line:", string(line))
384384 if lineLen == 0 {
385- return highlights
385+ return highlights , curRegion
386386 }
387387
388+ h .lastRegion = curRegion
388389 h .lastStart = - 1
389390 h .lastEnd = - 1
390391 h .storage = h .storage [:0 ]
@@ -412,7 +413,7 @@ func (h *Highlighter) highlight(highlights LineMatch, start int, lineNum int, li
412413 }
413414 }
414415
415- return highlights
416+ return highlights , h . lastRegion
416417}
417418
418419// HighlightString syntax highlights a string
@@ -426,7 +427,8 @@ func (h *Highlighter) HighlightString(input string) []LineMatch {
426427 for i := 0 ; i < len (lines ); i ++ {
427428 line := []byte (lines [i ])
428429 highlights := make (LineMatch )
429- lineMatches = append (lineMatches , h .highlight (highlights , 0 , i , line , nil ))
430+ match , _ := h .highlight (highlights , 0 , i , line , nil )
431+ lineMatches = append (lineMatches , match )
430432 }
431433
432434 return lineMatches
@@ -435,11 +437,11 @@ func (h *Highlighter) HighlightString(input string) []LineMatch {
435437// Highlight sets the state and matches for each line from startline to endline
436438// It sets all other matches in the buffer to nil to conserve memory
437439func (h * Highlighter ) Highlight (input LineStates , startline , endline int ) {
438- h . lastRegion = nil
440+ var curState * region
439441 if startline > 0 {
440442 input .Lock ()
441443 if startline - 1 < input .LinesNum () {
442- h . lastRegion = input .State (startline - 1 )
444+ curState = input .State (startline - 1 )
443445 }
444446 input .Unlock ()
445447 }
@@ -454,9 +456,10 @@ func (h *Highlighter) Highlight(input LineStates, startline, endline int) {
454456 line := input .LineBytes (i )
455457 highlights := make (LineMatch )
456458
457- match := h .highlight (highlights , 0 , i , line , h .lastRegion )
459+ match , newState := h .highlight (highlights , 0 , i , line , curState )
460+ curState = newState
458461
459- input .SetState (i , h . lastRegion )
462+ input .SetState (i , curState )
460463 input .SetMatch (i , match )
461464 input .Unlock ()
462465 }
@@ -470,13 +473,14 @@ func (h *Highlighter) ReHighlightLine(input LineStates, lineN int) {
470473 line := input .LineBytes (lineN )
471474 highlights := make (LineMatch )
472475
473- h . lastRegion = nil
476+ var curState * region
474477 if lineN > 0 {
475- h . lastRegion = input .State (lineN - 1 )
478+ curState = input .State (lineN - 1 )
476479 }
477480
478- match := h .highlight (highlights , 0 , lineN , line , h .lastRegion )
481+ match , newState := h .highlight (highlights , 0 , lineN , line , curState )
482+ curState = newState
479483
480- input .SetState (lineN , h . lastRegion )
484+ input .SetState (lineN , curState )
481485 input .SetMatch (lineN , match )
482486}
0 commit comments