9 "github.com/zyedidia/micro/v2/internal/util"
10 "github.com/zyedidia/micro/v2/pkg/highlight"
13 // Finds the byte index of the nth rune in a byte slice
14 func runeToByteIndex(n int, txt []byte) int {
22 _, _, size := util.DecodeCharacter(txt)
35 // A Line contains the data in bytes as well as a highlight state, match
36 // and a flag for whether the highlighting needs to be updated
41 match highlight.LineMatch
47 // Line ending file formats
48 FFAuto = 0 // Autodetect format
49 FFUnix = 1 // LF line endings (unix style '\n')
50 FFDos = 2 // CRLF line endings (dos style '\r\n')
55 // A LineArray simply stores and array of lines and makes it easy to insert
57 type LineArray struct {
63 // Append efficiently appends lines together
64 // It allocates an additional 10000 lines if the original estimate
66 func Append(slice []Line, data ...Line) []Line {
68 if l+len(data) > cap(slice) { // reallocate
69 newSlice := make([]Line, (l+len(data))+10000)
73 slice = slice[0 : l+len(data)]
74 for i, c := range data {
80 // NewLineArray returns a new line array from an array of bytes
81 func NewLineArray(size uint64, endings FileFormat, reader io.Reader) *LineArray {
84 la.lines = make([]Line, 0, 1000)
87 br := bufio.NewReader(reader)
94 data, err := br.ReadBytes('\n')
95 // Detect the line ending by checking to see if there is a '\r' char
97 // Even if the file format is set to DOS, the '\r' is removed so
98 // that all lines end with '\n'
100 if dlen > 1 && data[dlen-2] == '\r' {
101 data = append(data[:dlen-2], '\n')
102 if endings == FFAuto {
107 if endings == FFAuto {
112 // If we are loading a large file (greater than 1000) we use the file
113 // size and the length of the first 1000 lines to try to estimate
114 // how many lines will need to be allocated for the rest of the file
115 // We add an extra 10000 to the original estimate to be safe and give
116 // plenty of room for expansion
117 if n >= 1000 && loaded >= 0 {
118 totalLinesNum := int(float64(size) * (float64(n) / float64(loaded)))
119 newSlice := make([]Line, len(la.lines), totalLinesNum+10000)
120 copy(newSlice, la.lines)
125 // Counter for the number of bytes in the first 1000 lines
132 la.lines = Append(la.lines, Line{
139 // Last line was read
142 la.lines = Append(la.lines, Line{
155 // Bytes returns the string that should be written to disk when
156 // the line array is saved
157 func (la *LineArray) Bytes() []byte {
158 b := new(bytes.Buffer)
159 // initsize should provide a good estimate
160 b.Grow(int(la.initsize + 4096))
161 for i, l := range la.lines {
163 if i != len(la.lines)-1 {
164 if la.Endings == FFDos {
173 // newlineBelow adds a newline below the given line number
174 func (la *LineArray) newlineBelow(y int) {
175 la.lines = append(la.lines, Line{
181 copy(la.lines[y+2:], la.lines[y+1:])
182 la.lines[y+1] = Line{
184 state: la.lines[y].state,
190 // Inserts a byte array at a given location
191 func (la *LineArray) insert(pos Loc, value []byte) {
192 x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
193 for i := 0; i < len(value); i++ {
194 if value[i] == '\n' {
200 la.insertByte(Loc{x, y}, value[i])
205 // InsertByte inserts a byte at a given location
206 func (la *LineArray) insertByte(pos Loc, value byte) {
207 la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
208 copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
209 la.lines[pos.Y].data[pos.X] = value
212 // joinLines joins the two lines a and b
213 func (la *LineArray) joinLines(a, b int) {
214 la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
218 // split splits a line at a given position
219 func (la *LineArray) split(pos Loc) {
220 la.newlineBelow(pos.Y)
221 la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
222 la.lines[pos.Y+1].state = la.lines[pos.Y].state
223 la.lines[pos.Y].state = nil
224 la.lines[pos.Y].match = nil
225 la.lines[pos.Y+1].match = nil
226 la.lines[pos.Y].rehighlight = true
227 la.deleteToEnd(Loc{pos.X, pos.Y})
230 // removes from start to end
231 func (la *LineArray) remove(start, end Loc) []byte {
232 sub := la.Substr(start, end)
233 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
234 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
235 if start.Y == end.Y {
236 la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
238 la.deleteLines(start.Y+1, end.Y-1)
239 la.deleteToEnd(Loc{startX, start.Y})
240 la.deleteFromStart(Loc{endX - 1, start.Y + 1})
241 la.joinLines(start.Y, start.Y+1)
246 // deleteToEnd deletes from the end of a line to the position
247 func (la *LineArray) deleteToEnd(pos Loc) {
248 la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
251 // deleteFromStart deletes from the start of a line to the position
252 func (la *LineArray) deleteFromStart(pos Loc) {
253 la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
256 // deleteLine deletes the line number
257 func (la *LineArray) deleteLine(y int) {
258 la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
261 func (la *LineArray) deleteLines(y1, y2 int) {
262 la.lines = la.lines[:y1+copy(la.lines[y1:], la.lines[y2+1:])]
265 // DeleteByte deletes the byte at a position
266 func (la *LineArray) deleteByte(pos Loc) {
267 la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X+copy(la.lines[pos.Y].data[pos.X:], la.lines[pos.Y].data[pos.X+1:])]
270 // Substr returns the string representation between two locations
271 func (la *LineArray) Substr(start, end Loc) []byte {
272 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
273 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
274 if start.Y == end.Y {
275 src := la.lines[start.Y].data[startX:endX]
276 dest := make([]byte, len(src))
280 str := make([]byte, 0, len(la.lines[start.Y+1].data)*(end.Y-start.Y))
281 str = append(str, la.lines[start.Y].data[startX:]...)
282 str = append(str, '\n')
283 for i := start.Y + 1; i <= end.Y-1; i++ {
284 str = append(str, la.lines[i].data...)
285 str = append(str, '\n')
287 str = append(str, la.lines[end.Y].data[:endX]...)
291 // LinesNum returns the number of lines in the buffer
292 func (la *LineArray) LinesNum() int {
296 // Start returns the start of the buffer
297 func (la *LineArray) Start() Loc {
301 // End returns the location of the last character in the buffer
302 func (la *LineArray) End() Loc {
303 numlines := len(la.lines)
304 return Loc{util.CharacterCount(la.lines[numlines-1].data), numlines - 1}
307 // LineBytes returns line n as an array of bytes
308 func (la *LineArray) LineBytes(n int) []byte {
309 if n >= len(la.lines) || n < 0 {
312 return la.lines[n].data
315 // State gets the highlight state for the given line number
316 func (la *LineArray) State(lineN int) highlight.State {
317 la.lines[lineN].lock.Lock()
318 defer la.lines[lineN].lock.Unlock()
319 return la.lines[lineN].state
322 // SetState sets the highlight state at the given line number
323 func (la *LineArray) SetState(lineN int, s highlight.State) {
324 la.lines[lineN].lock.Lock()
325 defer la.lines[lineN].lock.Unlock()
326 la.lines[lineN].state = s
329 // SetMatch sets the match at the given line number
330 func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
331 la.lines[lineN].lock.Lock()
332 defer la.lines[lineN].lock.Unlock()
333 la.lines[lineN].match = m
336 // Match retrieves the match for the given line number
337 func (la *LineArray) Match(lineN int) highlight.LineMatch {
338 la.lines[lineN].lock.Lock()
339 defer la.lines[lineN].lock.Unlock()
340 return la.lines[lineN].match
343 func (la *LineArray) Rehighlight(lineN int) bool {
344 la.lines[lineN].lock.Lock()
345 defer la.lines[lineN].lock.Unlock()
346 return la.lines[lineN].rehighlight
349 func (la *LineArray) SetRehighlight(lineN int, on bool) {
350 la.lines[lineN].lock.Lock()
351 defer la.lines[lineN].lock.Unlock()
352 la.lines[lineN].rehighlight = on