9 "github.com/zyedidia/micro/pkg/highlight"
12 // Finds the byte index of the nth rune in a byte slice
13 func runeToByteIndex(n int, txt []byte) int {
21 _, size := utf8.DecodeRune(txt)
34 // A Line contains the data in bytes as well as a highlight state, match
35 // and a flag for whether the highlighting needs to be updated
40 match highlight.LineMatch
46 // Line ending file formats
47 FFAuto = 0 // Autodetect format
48 FFUnix = 1 // LF line endings (unix style '\n')
49 FFDos = 2 // CRLF line endings (dos style '\r\n')
54 // A LineArray simply stores and array of lines and makes it easy to insert
56 type LineArray struct {
62 // Append efficiently appends lines together
63 // It allocates an additional 10000 lines if the original estimate
65 func Append(slice []Line, data ...Line) []Line {
67 if l+len(data) > cap(slice) { // reallocate
68 newSlice := make([]Line, (l+len(data))+10000)
72 slice = slice[0 : l+len(data)]
73 for i, c := range data {
79 // NewLineArray returns a new line array from an array of bytes
80 func NewLineArray(size uint64, endings FileFormat, reader io.Reader) *LineArray {
83 la.lines = make([]Line, 0, 1000)
86 br := bufio.NewReader(reader)
91 data, err := br.ReadBytes('\n')
92 // Detect the line ending by checking to see if there is a '\r' char
94 // Even if the file format is set to DOS, the '\r' is removed so
95 // that all lines end with '\n'
97 if dlen > 1 && data[dlen-2] == '\r' {
98 data = append(data[:dlen-2], '\n')
99 if endings == FFAuto {
104 if endings == FFAuto {
109 // If we are loading a large file (greater than 1000) we use the file
110 // size and the length of the first 1000 lines to try to estimate
111 // how many lines will need to be allocated for the rest of the file
112 // We add an extra 10000 to the original estimate to be safe and give
113 // plenty of room for expansion
114 if n >= 1000 && loaded >= 0 {
115 totalLinesNum := int(float64(size) * (float64(n) / float64(loaded)))
116 newSlice := make([]Line, len(la.lines), totalLinesNum+10000)
117 copy(newSlice, la.lines)
122 // Counter for the number of bytes in the first 1000 lines
129 la.lines = Append(la.lines, Line{
136 // Last line was read
139 la.lines = Append(la.lines, Line{
152 // Bytes returns the string that should be written to disk when
153 // the line array is saved
154 func (la *LineArray) Bytes() []byte {
155 str := make([]byte, 0, la.initsize+1000) // initsize should provide a good estimate
156 for i, l := range la.lines {
157 str = append(str, l.data...)
158 if i != len(la.lines)-1 {
159 if la.Endings == FFDos {
160 str = append(str, '\r')
162 str = append(str, '\n')
168 // newlineBelow adds a newline below the given line number
169 func (la *LineArray) newlineBelow(y int) {
170 la.lines = append(la.lines, Line{
176 copy(la.lines[y+2:], la.lines[y+1:])
177 la.lines[y+1] = Line{
179 state: la.lines[y].state,
185 // Inserts a byte array at a given location
186 func (la *LineArray) insert(pos Loc, value []byte) {
187 x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
188 for i := 0; i < len(value); i++ {
189 if value[i] == '\n' {
195 la.insertByte(Loc{x, y}, value[i])
200 // InsertByte inserts a byte at a given location
201 func (la *LineArray) insertByte(pos Loc, value byte) {
202 la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
203 copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
204 la.lines[pos.Y].data[pos.X] = value
207 // joinLines joins the two lines a and b
208 func (la *LineArray) joinLines(a, b int) {
209 la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
213 // split splits a line at a given position
214 func (la *LineArray) split(pos Loc) {
215 la.newlineBelow(pos.Y)
216 la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
217 la.lines[pos.Y+1].state = la.lines[pos.Y].state
218 la.lines[pos.Y].state = nil
219 la.lines[pos.Y].match = nil
220 la.lines[pos.Y+1].match = nil
221 la.lines[pos.Y].rehighlight = true
222 la.deleteToEnd(Loc{pos.X, pos.Y})
225 // removes from start to end
226 func (la *LineArray) remove(start, end Loc) []byte {
227 sub := la.Substr(start, end)
228 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
229 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
230 if start.Y == end.Y {
231 la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
233 for i := start.Y + 1; i <= end.Y-1; i++ {
234 la.deleteLine(start.Y + 1)
236 la.deleteToEnd(Loc{startX, start.Y})
237 la.deleteFromStart(Loc{endX - 1, start.Y + 1})
238 la.joinLines(start.Y, start.Y+1)
243 // deleteToEnd deletes from the end of a line to the position
244 func (la *LineArray) deleteToEnd(pos Loc) {
245 la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
248 // deleteFromStart deletes from the start of a line to the position
249 func (la *LineArray) deleteFromStart(pos Loc) {
250 la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
253 // deleteLine deletes the line number
254 func (la *LineArray) deleteLine(y int) {
255 la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
258 // DeleteByte deletes the byte at a position
259 func (la *LineArray) deleteByte(pos Loc) {
260 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:])]
263 // Substr returns the string representation between two locations
264 func (la *LineArray) Substr(start, end Loc) []byte {
265 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
266 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
267 if start.Y == end.Y {
268 src := la.lines[start.Y].data[startX:endX]
269 dest := make([]byte, len(src))
273 str := make([]byte, 0, len(la.lines[start.Y+1].data)*(end.Y-start.Y))
274 str = append(str, la.lines[start.Y].data[startX:]...)
275 str = append(str, '\n')
276 for i := start.Y + 1; i <= end.Y-1; i++ {
277 str = append(str, la.lines[i].data...)
278 str = append(str, '\n')
280 str = append(str, la.lines[end.Y].data[:endX]...)
284 // LinesNum returns the number of lines in the buffer
285 func (la *LineArray) LinesNum() int {
289 // Start returns the start of the buffer
290 func (la *LineArray) Start() Loc {
294 // End returns the location of the last character in the buffer
295 func (la *LineArray) End() Loc {
296 numlines := len(la.lines)
297 return Loc{utf8.RuneCount(la.lines[numlines-1].data), numlines - 1}
300 // LineBytes returns line n as an array of bytes
301 func (la *LineArray) LineBytes(n int) []byte {
302 if n >= len(la.lines) || n < 0 {
305 return la.lines[n].data
308 // State gets the highlight state for the given line number
309 func (la *LineArray) State(lineN int) highlight.State {
310 la.lines[lineN].lock.Lock()
311 defer la.lines[lineN].lock.Unlock()
312 return la.lines[lineN].state
315 // SetState sets the highlight state at the given line number
316 func (la *LineArray) SetState(lineN int, s highlight.State) {
317 la.lines[lineN].lock.Lock()
318 defer la.lines[lineN].lock.Unlock()
319 la.lines[lineN].state = s
322 // SetMatch sets the match at the given line number
323 func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
324 la.lines[lineN].lock.Lock()
325 defer la.lines[lineN].lock.Unlock()
326 la.lines[lineN].match = m
329 // Match retrieves the match for the given line number
330 func (la *LineArray) Match(lineN int) highlight.LineMatch {
331 la.lines[lineN].lock.Lock()
332 defer la.lines[lineN].lock.Unlock()
333 return la.lines[lineN].match
336 func (la *LineArray) Rehighlight(lineN int) bool {
337 la.lines[lineN].lock.Lock()
338 defer la.lines[lineN].lock.Unlock()
339 return la.lines[lineN].rehighlight
342 func (la *LineArray) SetRehighlight(lineN int, on bool) {
343 la.lines[lineN].lock.Lock()
344 defer la.lines[lineN].lock.Unlock()
345 la.lines[lineN].rehighlight = on