@@ -46,10 +46,9 @@ type searchState struct {
4646type Line struct {
4747 data []byte
4848
49- state highlight.State
50- match highlight.LineMatch
51- rehighlight bool
52- lock sync.Mutex
49+ state highlight.State
50+ match highlight.LineMatch
51+ lock sync.Mutex
5352
5453 // The search states for the line, used for highlighting of search matches,
5554 // separately from the syntax highlighting.
@@ -75,6 +74,7 @@ type LineArray struct {
7574 lines []Line
7675 Endings FileFormat
7776 initsize uint64
77+ lock sync.Mutex
7878}
7979
8080// Append efficiently appends lines together
@@ -147,20 +147,18 @@ func NewLineArray(size uint64, endings FileFormat, reader io.Reader) *LineArray
147147 if err != nil {
148148 if err == io .EOF {
149149 la .lines = Append (la .lines , Line {
150- data : data ,
151- state : nil ,
152- match : nil ,
153- rehighlight : false ,
150+ data : data ,
151+ state : nil ,
152+ match : nil ,
154153 })
155154 }
156155 // Last line was read
157156 break
158157 } else {
159158 la .lines = Append (la .lines , Line {
160- data : data [:dlen - 1 ],
161- state : nil ,
162- match : nil ,
163- rehighlight : false ,
159+ data : data [:dlen - 1 ],
160+ state : nil ,
161+ match : nil ,
164162 })
165163 }
166164 n ++
@@ -190,22 +188,23 @@ func (la *LineArray) Bytes() []byte {
190188// newlineBelow adds a newline below the given line number
191189func (la * LineArray ) newlineBelow (y int ) {
192190 la .lines = append (la .lines , Line {
193- data : []byte {' ' },
194- state : nil ,
195- match : nil ,
196- rehighlight : false ,
191+ data : []byte {' ' },
192+ state : nil ,
193+ match : nil ,
197194 })
198195 copy (la .lines [y + 2 :], la .lines [y + 1 :])
199196 la .lines [y + 1 ] = Line {
200- data : []byte {},
201- state : la .lines [y ].state ,
202- match : nil ,
203- rehighlight : false ,
197+ data : []byte {},
198+ state : la .lines [y ].state ,
199+ match : nil ,
204200 }
205201}
206202
207203// Inserts a byte array at a given location
208204func (la * LineArray ) insert (pos Loc , value []byte ) {
205+ la .lock .Lock ()
206+ defer la .lock .Unlock ()
207+
209208 x , y := runeToByteIndex (pos .X , la .lines [pos .Y ].data ), pos .Y
210209 for i := 0 ; i < len (value ); i ++ {
211210 if value [i ] == '\n' || (value [i ] == '\r' && i < len (value )- 1 && value [i + 1 ] == '\n' ) {
@@ -233,24 +232,26 @@ func (la *LineArray) insertByte(pos Loc, value byte) {
233232
234233// joinLines joins the two lines a and b
235234func (la * LineArray ) joinLines (a , b int ) {
236- la .insert ( Loc { len ( la .lines [a ].data ), a }, la .lines [b ].data )
235+ la .lines [ a ]. data = append ( la .lines [a ].data , la .lines [b ].data ... )
237236 la .deleteLine (b )
238237}
239238
240239// split splits a line at a given position
241240func (la * LineArray ) split (pos Loc ) {
242241 la .newlineBelow (pos .Y )
243- la .insert ( Loc { 0 , pos .Y + 1 } , la .lines [pos .Y ].data [pos .X :])
242+ la .lines [ pos .Y + 1 ]. data = append ( la . lines [ pos . Y + 1 ]. data , la .lines [pos .Y ].data [pos .X :]... )
244243 la .lines [pos .Y + 1 ].state = la .lines [pos .Y ].state
245244 la .lines [pos .Y ].state = nil
246245 la .lines [pos .Y ].match = nil
247246 la .lines [pos .Y + 1 ].match = nil
248- la .lines [pos .Y ].rehighlight = true
249247 la .deleteToEnd (Loc {pos .X , pos .Y })
250248}
251249
252250// removes from start to end
253251func (la * LineArray ) remove (start , end Loc ) []byte {
252+ la .lock .Lock ()
253+ defer la .lock .Unlock ()
254+
254255 sub := la .Substr (start , end )
255256 startX := runeToByteIndex (start .X , la .lines [start .Y ].data )
256257 endX := runeToByteIndex (end .X , la .lines [end .Y ].data )
@@ -327,11 +328,11 @@ func (la *LineArray) End() Loc {
327328}
328329
329330// LineBytes returns line n as an array of bytes
330- func (la * LineArray ) LineBytes (n int ) []byte {
331- if n >= len (la .lines ) || n < 0 {
331+ func (la * LineArray ) LineBytes (lineN int ) []byte {
332+ if lineN >= len (la .lines ) || lineN < 0 {
332333 return []byte {}
333334 }
334- return la .lines [n ].data
335+ return la .lines [lineN ].data
335336}
336337
337338// State gets the highlight state for the given line number
@@ -362,16 +363,14 @@ func (la *LineArray) Match(lineN int) highlight.LineMatch {
362363 return la .lines [lineN ].match
363364}
364365
365- func (la * LineArray ) Rehighlight (lineN int ) bool {
366- la .lines [lineN ].lock .Lock ()
367- defer la .lines [lineN ].lock .Unlock ()
368- return la .lines [lineN ].rehighlight
366+ // Locks the whole LineArray
367+ func (la * LineArray ) Lock () {
368+ la .lock .Lock ()
369369}
370370
371- func (la * LineArray ) SetRehighlight (lineN int , on bool ) {
372- la .lines [lineN ].lock .Lock ()
373- defer la .lines [lineN ].lock .Unlock ()
374- la .lines [lineN ].rehighlight = on
371+ // Unlocks the whole LineArray
372+ func (la * LineArray ) Unlock () {
373+ la .lock .Unlock ()
375374}
376375
377376// SearchMatch returns true if the location `pos` is within a match
0 commit comments