Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: Add proper lock mechanism to lock the full LineArray instead of single lines #3224

Merged
merged 6 commits into from
Apr 5, 2024
Merged
4 changes: 4 additions & 0 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,11 @@ func (b *Buffer) Retab() {
}

l = bytes.TrimLeft(l, " \t")

b.Lock()
b.lines[i].data = append(ws, l...)
b.Unlock()

b.MarkModified(i, i)
dirty = true
}
Expand Down
4 changes: 1 addition & 3 deletions internal/buffer/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ type operation struct {

func init() {
ulua.L = lua.NewState()
// TODO: uncomment InitRuntimeFiles once we fix races between syntax
// highlighting and buffer editing.
// config.InitRuntimeFiles(false)
config.InitRuntimeFiles(false)
config.InitGlobalSettings()
config.GlobalSettings["backup"] = false
config.GlobalSettings["fastdirty"] = true
Expand Down
67 changes: 33 additions & 34 deletions internal/buffer/line_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ type searchState struct {
type Line struct {
data []byte

state highlight.State
match highlight.LineMatch
rehighlight bool
dmaluka marked this conversation as resolved.
Show resolved Hide resolved
lock sync.Mutex
state highlight.State
match highlight.LineMatch
lock sync.Mutex

// The search states for the line, used for highlighting of search matches,
// separately from the syntax highlighting.
Expand All @@ -75,6 +74,7 @@ type LineArray struct {
lines []Line
Endings FileFormat
initsize uint64
lock sync.Mutex
}

// Append efficiently appends lines together
Expand Down Expand Up @@ -147,20 +147,18 @@ func NewLineArray(size uint64, endings FileFormat, reader io.Reader) *LineArray
if err != nil {
if err == io.EOF {
la.lines = Append(la.lines, Line{
data: data,
state: nil,
match: nil,
rehighlight: false,
data: data,
state: nil,
match: nil,
})
}
// Last line was read
break
} else {
la.lines = Append(la.lines, Line{
data: data[:dlen-1],
state: nil,
match: nil,
rehighlight: false,
data: data[:dlen-1],
state: nil,
match: nil,
})
}
n++
Expand Down Expand Up @@ -190,22 +188,23 @@ func (la *LineArray) Bytes() []byte {
// newlineBelow adds a newline below the given line number
func (la *LineArray) newlineBelow(y int) {
la.lines = append(la.lines, Line{
data: []byte{' '},
state: nil,
match: nil,
rehighlight: false,
data: []byte{' '},
state: nil,
match: nil,
})
copy(la.lines[y+2:], la.lines[y+1:])
la.lines[y+1] = Line{
data: []byte{},
state: la.lines[y].state,
match: nil,
rehighlight: false,
data: []byte{},
state: la.lines[y].state,
match: nil,
}
}

// Inserts a byte array at a given location
func (la *LineArray) insert(pos Loc, value []byte) {
la.lock.Lock()
JoeKar marked this conversation as resolved.
Show resolved Hide resolved
defer la.lock.Unlock()
JoeKar marked this conversation as resolved.
Show resolved Hide resolved

x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
for i := 0; i < len(value); i++ {
if value[i] == '\n' || (value[i] == '\r' && i < len(value)-1 && value[i+1] == '\n') {
Expand Down Expand Up @@ -233,24 +232,26 @@ func (la *LineArray) insertByte(pos Loc, value byte) {

// joinLines joins the two lines a and b
func (la *LineArray) joinLines(a, b int) {
la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
la.lines[a].data = append(la.lines[a].data, la.lines[b].data...)
JoeKar marked this conversation as resolved.
Show resolved Hide resolved
la.deleteLine(b)
}

// split splits a line at a given position
func (la *LineArray) split(pos Loc) {
la.newlineBelow(pos.Y)
la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
la.lines[pos.Y+1].data = append(la.lines[pos.Y+1].data, la.lines[pos.Y].data[pos.X:]...)
la.lines[pos.Y+1].state = la.lines[pos.Y].state
la.lines[pos.Y].state = nil
la.lines[pos.Y].match = nil
la.lines[pos.Y+1].match = nil
la.lines[pos.Y].rehighlight = true
la.deleteToEnd(Loc{pos.X, pos.Y})
}

// removes from start to end
func (la *LineArray) remove(start, end Loc) []byte {
la.lock.Lock()
defer la.lock.Unlock()
JoeKar marked this conversation as resolved.
Show resolved Hide resolved

sub := la.Substr(start, end)
startX := runeToByteIndex(start.X, la.lines[start.Y].data)
endX := runeToByteIndex(end.X, la.lines[end.Y].data)
Expand Down Expand Up @@ -327,11 +328,11 @@ func (la *LineArray) End() Loc {
}

// LineBytes returns line n as an array of bytes
func (la *LineArray) LineBytes(n int) []byte {
if n >= len(la.lines) || n < 0 {
func (la *LineArray) LineBytes(lineN int) []byte {
if lineN >= len(la.lines) || lineN < 0 {
return []byte{}
}
return la.lines[n].data
return la.lines[lineN].data
}

// State gets the highlight state for the given line number
Expand Down Expand Up @@ -362,16 +363,14 @@ func (la *LineArray) Match(lineN int) highlight.LineMatch {
return la.lines[lineN].match
}

func (la *LineArray) Rehighlight(lineN int) bool {
la.lines[lineN].lock.Lock()
defer la.lines[lineN].lock.Unlock()
return la.lines[lineN].rehighlight
// Locks the whole LineArray
func (la *LineArray) Lock() {
la.lock.Lock()
}

func (la *LineArray) SetRehighlight(lineN int, on bool) {
la.lines[lineN].lock.Lock()
defer la.lines[lineN].lock.Unlock()
la.lines[lineN].rehighlight = on
// Unlocks the whole LineArray
func (la *LineArray) Unlock() {
la.lock.Unlock()
}

// SearchMatch returns true if the location `pos` is within a match
Expand Down
28 changes: 26 additions & 2 deletions pkg/highlight/highlighter.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type LineStates interface {
State(lineN int) State
JoeKar marked this conversation as resolved.
Show resolved Hide resolved
SetState(lineN int, s State)
SetMatch(lineN int, m LineMatch)
Lock()
Unlock()
}

// A Highlighter contains the information needed to highlight a string
Expand Down Expand Up @@ -303,7 +305,13 @@ func (h *Highlighter) HighlightString(input string) []LineMatch {

// HighlightStates correctly sets all states for the buffer
func (h *Highlighter) HighlightStates(input LineStates) {
for i := 0; i < input.LinesNum(); i++ {
for i := 0; ; i++ {
input.Lock()
if i >= input.LinesNum() {
input.Unlock()
break
}

line := input.LineBytes(i)
// highlights := make(LineMatch)

Expand All @@ -316,6 +324,7 @@ func (h *Highlighter) HighlightStates(input LineStates) {
curState := h.lastRegion

input.SetState(i, curState)
input.Unlock()
}
}

Expand All @@ -324,7 +333,9 @@ func (h *Highlighter) HighlightStates(input LineStates) {
// This assumes that all the states are set correctly
func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int) {
for i := startline; i <= endline; i++ {
input.Lock()
if i >= input.LinesNum() {
input.Unlock()
break
}

Expand All @@ -339,6 +350,7 @@ func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int)
}

input.SetMatch(i, match)
input.Unlock()
}
}

Expand All @@ -350,9 +362,17 @@ func (h *Highlighter) ReHighlightStates(input LineStates, startline int) int {

h.lastRegion = nil
if startline > 0 {
input.Lock()
h.lastRegion = input.State(startline - 1)
input.Unlock()
}
for i := startline; i < input.LinesNum(); i++ {
for i := startline; ; i++ {
input.Lock()
if i >= input.LinesNum() {
input.Unlock()
break
}

line := input.LineBytes(i)
// highlights := make(LineMatch)

Expand All @@ -366,6 +386,7 @@ func (h *Highlighter) ReHighlightStates(input LineStates, startline int) int {
lastState := input.State(i)

input.SetState(i, curState)
input.Unlock()

if curState == lastState {
return i
Expand All @@ -377,6 +398,9 @@ func (h *Highlighter) ReHighlightStates(input LineStates, startline int) int {

// ReHighlightLine will rehighlight the state and match for a single line
func (h *Highlighter) ReHighlightLine(input LineStates, lineN int) {
input.Lock()
JoeKar marked this conversation as resolved.
Show resolved Hide resolved
defer input.Unlock()
JoeKar marked this conversation as resolved.
Show resolved Hide resolved

line := input.LineBytes(lineN)
highlights := make(LineMatch)

Expand Down
Loading