]> git.lizzy.rs Git - micro.git/blob - cmd/micro/lineArray.go
Initial support for terminal within micro
[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                         // The copy function is predeclared and works for any slice type.
93                         copy(newSlice, la.lines)
94                         la.lines = newSlice
95                         loaded = -1
96                 }
97
98                 if loaded >= 0 {
99                         loaded += len(data)
100                 }
101
102                 if err != nil {
103                         if err == io.EOF {
104                                 la.lines = Append(la.lines, Line{data[:], nil, nil, false})
105                                 // la.lines = Append(la.lines, Line{data[:len(data)]})
106                         }
107                         // Last line was read
108                         break
109                 } else {
110                         // la.lines = Append(la.lines, Line{data[:len(data)-1]})
111                         la.lines = Append(la.lines, Line{data[:len(data)-1], nil, nil, false})
112                 }
113                 n++
114         }
115
116         return la
117 }
118
119 // Returns the String representation of the LineArray
120 func (la *LineArray) String() string {
121         str := ""
122         for i, l := range la.lines {
123                 str += string(l.data)
124                 if i != len(la.lines)-1 {
125                         str += "\n"
126                 }
127         }
128         return str
129 }
130
131 // SaveString returns the string that should be written to disk when
132 // the line array is saved
133 // It is the same as string but uses crlf or lf line endings depending
134 func (la *LineArray) SaveString(useCrlf bool) string {
135         str := ""
136         for i, l := range la.lines {
137                 str += string(l.data)
138                 if i != len(la.lines)-1 {
139                         if useCrlf {
140                                 str += "\r"
141                         }
142                         str += "\n"
143                 }
144         }
145         return str
146 }
147
148 // NewlineBelow adds a newline below the given line number
149 func (la *LineArray) NewlineBelow(y int) {
150         la.lines = append(la.lines, Line{[]byte(" "), nil, nil, false})
151         copy(la.lines[y+2:], la.lines[y+1:])
152         la.lines[y+1] = Line{[]byte(""), la.lines[y].state, nil, false}
153 }
154
155 // inserts a byte array at a given location
156 func (la *LineArray) insert(pos Loc, value []byte) {
157         x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
158         // x, y := pos.x, pos.y
159         for i := 0; i < len(value); i++ {
160                 if value[i] == '\n' {
161                         la.Split(Loc{x, y})
162                         x = 0
163                         y++
164                         continue
165                 }
166                 la.insertByte(Loc{x, y}, value[i])
167                 x++
168         }
169 }
170
171 // inserts a byte at a given location
172 func (la *LineArray) insertByte(pos Loc, value byte) {
173         la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
174         copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
175         la.lines[pos.Y].data[pos.X] = value
176 }
177
178 // JoinLines joins the two lines a and b
179 func (la *LineArray) JoinLines(a, b int) {
180         la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
181         la.DeleteLine(b)
182 }
183
184 // Split splits a line at a given position
185 func (la *LineArray) Split(pos Loc) {
186         la.NewlineBelow(pos.Y)
187         la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
188         la.lines[pos.Y+1].state = la.lines[pos.Y].state
189         la.lines[pos.Y].state = nil
190         la.lines[pos.Y].match = nil
191         la.lines[pos.Y+1].match = nil
192         la.lines[pos.Y].rehighlight = true
193         la.DeleteToEnd(Loc{pos.X, pos.Y})
194 }
195
196 // removes from start to end
197 func (la *LineArray) remove(start, end Loc) string {
198         sub := la.Substr(start, end)
199         startX := runeToByteIndex(start.X, la.lines[start.Y].data)
200         endX := runeToByteIndex(end.X, la.lines[end.Y].data)
201         if start.Y == end.Y {
202                 la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
203         } else {
204                 for i := start.Y + 1; i <= end.Y-1; i++ {
205                         la.DeleteLine(start.Y + 1)
206                 }
207                 la.DeleteToEnd(Loc{startX, start.Y})
208                 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
209                 la.JoinLines(start.Y, start.Y+1)
210         }
211         return sub
212 }
213
214 // DeleteToEnd deletes from the end of a line to the position
215 func (la *LineArray) DeleteToEnd(pos Loc) {
216         la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
217 }
218
219 // DeleteFromStart deletes from the start of a line to the position
220 func (la *LineArray) DeleteFromStart(pos Loc) {
221         la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
222 }
223
224 // DeleteLine deletes the line number
225 func (la *LineArray) DeleteLine(y int) {
226         la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
227 }
228
229 // DeleteByte deletes the byte at a position
230 func (la *LineArray) DeleteByte(pos Loc) {
231         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:])]
232 }
233
234 // Substr returns the string representation between two locations
235 func (la *LineArray) Substr(start, end Loc) string {
236         startX := runeToByteIndex(start.X, la.lines[start.Y].data)
237         endX := runeToByteIndex(end.X, la.lines[end.Y].data)
238         if start.Y == end.Y {
239                 return string(la.lines[start.Y].data[startX:endX])
240         }
241         var str string
242         str += string(la.lines[start.Y].data[startX:]) + "\n"
243         for i := start.Y + 1; i <= end.Y-1; i++ {
244                 str += string(la.lines[i].data) + "\n"
245         }
246         str += string(la.lines[end.Y].data[:endX])
247         return str
248 }
249
250 // State gets the highlight state for the given line number
251 func (la *LineArray) State(lineN int) highlight.State {
252         return la.lines[lineN].state
253 }
254
255 // SetState sets the highlight state at the given line number
256 func (la *LineArray) SetState(lineN int, s highlight.State) {
257         la.lines[lineN].state = s
258 }
259
260 // SetMatch sets the match at the given line number
261 func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
262         la.lines[lineN].match = m
263 }
264
265 // Match retrieves the match for the given line number
266 func (la *LineArray) Match(lineN int) highlight.LineMatch {
267         return la.lines[lineN].match
268 }