]> git.lizzy.rs Git - micro.git/blob - cmd/micro/lineArray.go
Minor optimizations
[micro.git] / cmd / micro / lineArray.go
1 package main
2
3 import (
4         "bufio"
5         "io"
6         "unicode/utf8"
7
8         "github.com/zyedidia/micro/cmd/micro/highlight"
9 )
10
11 func runeToByteIndex(n int, txt []byte) int {
12         if n == 0 {
13                 return 0
14         }
15
16         count := 0
17         i := 0
18         for len(txt) > 0 {
19                 _, size := utf8.DecodeRune(txt)
20
21                 txt = txt[size:]
22                 count += size
23                 i++
24
25                 if i == n {
26                         break
27                 }
28         }
29         return count
30 }
31
32 // A Line contains the data in bytes as well as a highlight state, match
33 // and a flag for whether the highlighting needs to be updated
34 type Line struct {
35         data []byte
36
37         state       highlight.State
38         match       highlight.LineMatch
39         rehighlight bool
40 }
41
42 // A LineArray simply stores and array of lines and makes it easy to insert
43 // and delete in it
44 type LineArray struct {
45         lines []Line
46 }
47
48 // Append efficiently appends lines together
49 // It allocates an additional 10000 lines if the original estimate
50 // is incorrect
51 func Append(slice []Line, data ...Line) []Line {
52         l := len(slice)
53         if l+len(data) > cap(slice) { // reallocate
54                 newSlice := make([]Line, (l+len(data))+10000)
55                 // The copy function is predeclared and works for any slice type.
56                 copy(newSlice, slice)
57                 slice = newSlice
58         }
59         slice = slice[0 : l+len(data)]
60         for i, c := range data {
61                 slice[l+i] = c
62         }
63         return slice
64 }
65
66 // NewLineArray returns a new line array from an array of bytes
67 func NewLineArray(size int64, reader io.Reader) *LineArray {
68         la := new(LineArray)
69
70         la.lines = make([]Line, 0, 1000)
71
72         br := bufio.NewReader(reader)
73         var loaded int
74
75         n := 0
76         for {
77                 data, err := br.ReadBytes('\n')
78                 if len(data) > 1 && data[len(data)-2] == '\r' {
79                         data = append(data[:len(data)-2], '\n')
80                         if fileformat == 0 {
81                                 fileformat = 2
82                         }
83                 } else if len(data) > 0 {
84                         if fileformat == 0 {
85                                 fileformat = 1
86                         }
87                 }
88
89                 if n >= 1000 && loaded >= 0 {
90                         totalLinesNum := int(float64(size) * (float64(n) / float64(loaded)))
91                         newSlice := make([]Line, len(la.lines), totalLinesNum+10000)
92                         copy(newSlice, la.lines)
93                         la.lines = newSlice
94                         loaded = -1
95                 }
96
97                 if loaded >= 0 {
98                         loaded += len(data)
99                 }
100
101                 if err != nil {
102                         if err == io.EOF {
103                                 la.lines = Append(la.lines, Line{data[:], nil, nil, false})
104                                 // la.lines = Append(la.lines, Line{data[:len(data)]})
105                         }
106                         // Last line was read
107                         break
108                 } else {
109                         // la.lines = Append(la.lines, Line{data[:len(data)-1]})
110                         la.lines = Append(la.lines, Line{data[:len(data)-1], nil, nil, false})
111                 }
112                 n++
113         }
114
115         return la
116 }
117
118 // Returns the String representation of the LineArray
119 func (la *LineArray) String() string {
120         str := ""
121         for i, l := range la.lines {
122                 str += string(l.data)
123                 if i != len(la.lines)-1 {
124                         str += "\n"
125                 }
126         }
127         return str
128 }
129
130 // SaveString returns the string that should be written to disk when
131 // the line array is saved
132 // It is the same as string but uses crlf or lf line endings depending
133 func (la *LineArray) SaveString(useCrlf bool) string {
134         str := ""
135         for i, l := range la.lines {
136                 str += string(l.data)
137                 if i != len(la.lines)-1 {
138                         if useCrlf {
139                                 str += "\r"
140                         }
141                         str += "\n"
142                 }
143         }
144         return str
145 }
146
147 // NewlineBelow adds a newline below the given line number
148 func (la *LineArray) NewlineBelow(y int) {
149         la.lines = append(la.lines, Line{[]byte{' '}, nil, nil, false})
150         copy(la.lines[y+2:], la.lines[y+1:])
151         la.lines[y+1] = Line{[]byte{}, la.lines[y].state, nil, false}
152 }
153
154 // inserts a byte array at a given location
155 func (la *LineArray) insert(pos Loc, value []byte) {
156         x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
157         // x, y := pos.x, pos.y
158         for i := 0; i < len(value); i++ {
159                 if value[i] == '\n' {
160                         la.Split(Loc{x, y})
161                         x = 0
162                         y++
163                         continue
164                 }
165                 la.insertByte(Loc{x, y}, value[i])
166                 x++
167         }
168 }
169
170 // inserts a byte at a given location
171 func (la *LineArray) insertByte(pos Loc, value byte) {
172         la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
173         copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
174         la.lines[pos.Y].data[pos.X] = value
175 }
176
177 // JoinLines joins the two lines a and b
178 func (la *LineArray) JoinLines(a, b int) {
179         la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
180         la.DeleteLine(b)
181 }
182
183 // Split splits a line at a given position
184 func (la *LineArray) Split(pos Loc) {
185         la.NewlineBelow(pos.Y)
186         la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
187         la.lines[pos.Y+1].state = la.lines[pos.Y].state
188         la.lines[pos.Y].state = nil
189         la.lines[pos.Y].match = nil
190         la.lines[pos.Y+1].match = nil
191         la.lines[pos.Y].rehighlight = true
192         la.DeleteToEnd(Loc{pos.X, pos.Y})
193 }
194
195 // removes from start to end
196 func (la *LineArray) remove(start, end Loc) string {
197         sub := la.Substr(start, end)
198         startX := runeToByteIndex(start.X, la.lines[start.Y].data)
199         endX := runeToByteIndex(end.X, la.lines[end.Y].data)
200         if start.Y == end.Y {
201                 la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
202         } else {
203                 for i := start.Y + 1; i <= end.Y-1; i++ {
204                         la.DeleteLine(start.Y + 1)
205                 }
206                 la.DeleteToEnd(Loc{startX, start.Y})
207                 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
208                 la.JoinLines(start.Y, start.Y+1)
209         }
210         return sub
211 }
212
213 // DeleteToEnd deletes from the end of a line to the position
214 func (la *LineArray) DeleteToEnd(pos Loc) {
215         la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
216 }
217
218 // DeleteFromStart deletes from the start of a line to the position
219 func (la *LineArray) DeleteFromStart(pos Loc) {
220         la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
221 }
222
223 // DeleteLine deletes the line number
224 func (la *LineArray) DeleteLine(y int) {
225         la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
226 }
227
228 // DeleteByte deletes the byte at a position
229 func (la *LineArray) DeleteByte(pos Loc) {
230         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:])]
231 }
232
233 // Substr returns the string representation between two locations
234 func (la *LineArray) Substr(start, end Loc) string {
235         startX := runeToByteIndex(start.X, la.lines[start.Y].data)
236         endX := runeToByteIndex(end.X, la.lines[end.Y].data)
237         if start.Y == end.Y {
238                 return string(la.lines[start.Y].data[startX:endX])
239         }
240         var str string
241         str += string(la.lines[start.Y].data[startX:]) + "\n"
242         for i := start.Y + 1; i <= end.Y-1; i++ {
243                 str += string(la.lines[i].data) + "\n"
244         }
245         str += string(la.lines[end.Y].data[:endX])
246         return str
247 }
248
249 // State gets the highlight state for the given line number
250 func (la *LineArray) State(lineN int) highlight.State {
251         return la.lines[lineN].state
252 }
253
254 // SetState sets the highlight state at the given line number
255 func (la *LineArray) SetState(lineN int, s highlight.State) {
256         la.lines[lineN].state = s
257 }
258
259 // SetMatch sets the match at the given line number
260 func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
261         la.lines[lineN].match = m
262 }
263
264 // Match retrieves the match for the given line number
265 func (la *LineArray) Match(lineN int) highlight.LineMatch {
266         return la.lines[lineN].match
267 }