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