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