Skip to content

Commit

Permalink
highlighter: Fix last open racy access in the *Highlight*() functions
Browse files Browse the repository at this point in the history
Otherwise it's possible that `*Highlight*()` functions called from the async
initial highlighting can be interrupted by the main goroutine, which then
changes the already set and furthermore used `h.lastRegion`. The resulting
last region respective set state isn't reliable any longer then.
  • Loading branch information
JoeKar committed Apr 11, 2024
1 parent acb0d76 commit 37b2ede
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions pkg/highlight/highlighter.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ func (h *Highlighter) HighlightStates(input LineStates) {
line := input.LineBytes(i)
// highlights := make(LineMatch)

h.lastRegion = nil
if i > 0 {
h.lastRegion = input.State(i - 1)
}

if i == 0 || h.lastRegion == nil {
h.highlightEmptyRegion(nil, 0, true, i, line, true)
} else {
Expand All @@ -332,6 +337,14 @@ func (h *Highlighter) HighlightStates(input LineStates) {
// It sets all other matches in the buffer to nil to conserve memory
// This assumes that all the states are set correctly
func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int) {
var curState *region
if startline > 0 {
input.Lock()
if startline-1 < input.LinesNum() {
curState = input.State(startline - 1)
}
input.Unlock()
}
for i := startline; i <= endline; i++ {
input.Lock()
if i >= input.LinesNum() {
Expand All @@ -343,11 +356,13 @@ func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int)
highlights := make(LineMatch)

var match LineMatch
if i == 0 || input.State(i-1) == nil {
h.lastRegion = curState
if i == 0 || h.lastRegion == nil {
match = h.highlightEmptyRegion(highlights, 0, true, i, line, false)
} else {
match = h.highlightRegion(highlights, 0, true, i, line, input.State(i-1), false)
match = h.highlightRegion(highlights, 0, true, i, line, h.lastRegion, false)
}
curState = h.lastRegion

input.SetMatch(i, match)
input.Unlock()
Expand All @@ -360,11 +375,11 @@ func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int)
func (h *Highlighter) ReHighlightStates(input LineStates, startline int) int {
// lines := input.LineData()

h.lastRegion = nil
var curState *region
if startline > 0 {
input.Lock()
if startline-1 < input.LinesNum() {
h.lastRegion = input.State(startline - 1)
curState = input.State(startline - 1)
}
input.Unlock()
}
Expand All @@ -379,12 +394,13 @@ func (h *Highlighter) ReHighlightStates(input LineStates, startline int) int {
// highlights := make(LineMatch)

// var match LineMatch
h.lastRegion = curState
if i == 0 || h.lastRegion == nil {
h.highlightEmptyRegion(nil, 0, true, i, line, true)
} else {
h.highlightRegion(nil, 0, true, i, line, h.lastRegion, true)
}
curState := h.lastRegion
curState = h.lastRegion
lastState := input.State(i)

input.SetState(i, curState)
Expand Down

0 comments on commit 37b2ede

Please sign in to comment.