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