buffer: Lock the LineArray in case of modifications and export this lock

This commit is contained in:
Jöran Karl 2024-04-04 21:22:22 +02:00
parent 53d56d032c
commit 2830c4878e
2 changed files with 21 additions and 0 deletions

View File

@ -1180,7 +1180,11 @@ func (b *Buffer) Retab() {
} }
l = bytes.TrimLeft(l, " \t") l = bytes.TrimLeft(l, " \t")
b.Lock()
b.lines[i].data = append(ws, l...) b.lines[i].data = append(ws, l...)
b.Unlock()
b.MarkModified(i, i) b.MarkModified(i, i)
dirty = true dirty = true
} }

View File

@ -75,6 +75,7 @@ type LineArray struct {
lines []Line lines []Line
Endings FileFormat Endings FileFormat
initsize uint64 initsize uint64
lock sync.Mutex
} }
// Append efficiently appends lines together // Append efficiently appends lines together
@ -206,6 +207,9 @@ func (la *LineArray) newlineBelow(y int) {
// Inserts a byte array at a given location // Inserts a byte array at a given location
func (la *LineArray) insert(pos Loc, value []byte) { func (la *LineArray) insert(pos Loc, value []byte) {
la.lock.Lock()
defer la.lock.Unlock()
x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
for i := 0; i < len(value); i++ { for i := 0; i < len(value); i++ {
if value[i] == '\n' || (value[i] == '\r' && i < len(value)-1 && value[i+1] == '\n') { if value[i] == '\n' || (value[i] == '\r' && i < len(value)-1 && value[i+1] == '\n') {
@ -251,6 +255,9 @@ func (la *LineArray) split(pos Loc) {
// removes from start to end // removes from start to end
func (la *LineArray) remove(start, end Loc) []byte { func (la *LineArray) remove(start, end Loc) []byte {
la.lock.Lock()
defer la.lock.Unlock()
sub := la.Substr(start, end) sub := la.Substr(start, end)
startX := runeToByteIndex(start.X, la.lines[start.Y].data) startX := runeToByteIndex(start.X, la.lines[start.Y].data)
endX := runeToByteIndex(end.X, la.lines[end.Y].data) endX := runeToByteIndex(end.X, la.lines[end.Y].data)
@ -374,6 +381,16 @@ func (la *LineArray) SetRehighlight(lineN int, on bool) {
la.lines[lineN].rehighlight = on la.lines[lineN].rehighlight = on
} }
// Locks the whole LineArray
func (la *LineArray) Lock() {
la.lock.Lock()
}
// Unlocks the whole LineArray
func (la *LineArray) Unlock() {
la.lock.Unlock()
}
// SearchMatch returns true if the location `pos` is within a match // SearchMatch returns true if the location `pos` is within a match
// of the last search for the buffer `b`. // of the last search for the buffer `b`.
// It is used for efficient highlighting of search matches (separately // It is used for efficient highlighting of search matches (separately