]> git.lizzy.rs Git - micro.git/blob - cmd/micro/lineArray.go
Merge branch 'master' into view-refactor
[micro.git] / cmd / micro / lineArray.go
1 package main
2
3 import (
4         "bufio"
5         "bytes"
6         "io"
7         "unicode/utf8"
8
9         "github.com/zyedidia/micro/cmd/micro/highlight"
10 )
11
12 func lineCounter(r io.Reader) (int, error) {
13         buf := make([]byte, 32*1024)
14         count := 0
15         lineSep := []byte{'\n'}
16
17         for {
18                 c, err := r.Read(buf)
19                 count += bytes.Count(buf[:c], lineSep)
20
21                 switch {
22                 case err == io.EOF:
23                         return count, nil
24
25                 case err != nil:
26                         return count, err
27                 }
28         }
29 }
30
31 func runeToByteIndex(n int, txt []byte) int {
32         if n == 0 {
33                 return 0
34         }
35
36         count := 0
37         i := 0
38         for len(txt) > 0 {
39                 _, size := utf8.DecodeRune(txt)
40
41                 txt = txt[size:]
42                 count += size
43                 i++
44
45                 if i == n {
46                         break
47                 }
48         }
49         return count
50 }
51
52 type Line struct {
53         data []byte
54
55         state       highlight.State
56         match       highlight.LineMatch
57         rehighlight bool
58 }
59
60 // A LineArray simply stores and array of lines and makes it easy to insert
61 // and delete in it
62 type LineArray struct {
63         lines []Line
64 }
65
66 // NewLineArray returns a new line array from an array of bytes
67 func NewLineArray(reader io.Reader) *LineArray {
68         la := new(LineArray)
69
70         var buf bytes.Buffer
71         tee := io.TeeReader(reader, &buf)
72         numlines, _ := lineCounter(tee)
73         if numlines == 0 {
74                 numlines = 1
75         }
76
77         la.lines = make([]Line, numlines)
78
79         br := bufio.NewReader(&buf)
80
81         for i := 0; i < numlines; i++ {
82                 data, err := br.ReadBytes('\n')
83                 if err != nil {
84                         if err == io.EOF {
85                                 // la.lines[i] = Line{data[:len(data)], nil, nil, false}
86                                 la.lines[i].data = data
87                         }
88                         // Last line was read
89                         break
90                 } else {
91                         la.lines[i].data = data[:len(data)-1]
92                         // la.lines[i] = Line{data[:len(data)-1], nil, nil, false}
93                 }
94         }
95
96         return la
97 }
98
99 // Returns the String representation of the LineArray
100 func (la *LineArray) String() string {
101         str := ""
102         for i, l := range la.lines {
103                 str += string(l.data)
104                 if i != len(la.lines)-1 {
105                         str += "\n"
106                 }
107         }
108         return str
109 }
110
111 // NewlineBelow adds a newline below the given line number
112 func (la *LineArray) NewlineBelow(y int) {
113         la.lines = append(la.lines, Line{[]byte(" "), nil, nil, false})
114         copy(la.lines[y+2:], la.lines[y+1:])
115         la.lines[y+1] = Line{[]byte(""), la.lines[y].state, nil, false}
116 }
117
118 // inserts a byte array at a given location
119 func (la *LineArray) insert(pos Loc, value []byte) {
120         x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
121         // x, y := pos.x, pos.y
122         for i := 0; i < len(value); i++ {
123                 if value[i] == '\n' {
124                         la.Split(Loc{x, y})
125                         x = 0
126                         y++
127                         continue
128                 }
129                 la.insertByte(Loc{x, y}, value[i])
130                 x++
131         }
132 }
133
134 // inserts a byte at a given location
135 func (la *LineArray) insertByte(pos Loc, value byte) {
136         la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
137         copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
138         la.lines[pos.Y].data[pos.X] = value
139 }
140
141 // JoinLines joins the two lines a and b
142 func (la *LineArray) JoinLines(a, b int) {
143         la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
144         la.DeleteLine(b)
145 }
146
147 // Split splits a line at a given position
148 func (la *LineArray) Split(pos Loc) {
149         la.NewlineBelow(pos.Y)
150         la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
151         la.lines[pos.Y+1].state = la.lines[pos.Y].state
152         la.lines[pos.Y].state = nil
153         la.lines[pos.Y].match = nil
154         la.lines[pos.Y+1].match = nil
155         la.lines[pos.Y].rehighlight = true
156         la.DeleteToEnd(Loc{pos.X, pos.Y})
157 }
158
159 // removes from start to end
160 func (la *LineArray) remove(start, end Loc) string {
161         sub := la.Substr(start, end)
162         startX := runeToByteIndex(start.X, la.lines[start.Y].data)
163         endX := runeToByteIndex(end.X, la.lines[end.Y].data)
164         if start.Y == end.Y {
165                 la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
166         } else {
167                 for i := start.Y + 1; i <= end.Y-1; i++ {
168                         la.DeleteLine(start.Y + 1)
169                 }
170                 la.DeleteToEnd(Loc{startX, start.Y})
171                 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
172                 la.JoinLines(start.Y, start.Y+1)
173         }
174         return sub
175 }
176
177 // DeleteToEnd deletes from the end of a line to the position
178 func (la *LineArray) DeleteToEnd(pos Loc) {
179         la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
180 }
181
182 // DeleteFromStart deletes from the start of a line to the position
183 func (la *LineArray) DeleteFromStart(pos Loc) {
184         la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
185 }
186
187 // DeleteLine deletes the line number
188 func (la *LineArray) DeleteLine(y int) {
189         la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
190 }
191
192 // DeleteByte deletes the byte at a position
193 func (la *LineArray) DeleteByte(pos Loc) {
194         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:])]
195 }
196
197 // Substr returns the string representation between two locations
198 func (la *LineArray) Substr(start, end Loc) string {
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                 return string(la.lines[start.Y].data[startX:endX])
203         }
204         var str string
205         str += string(la.lines[start.Y].data[startX:]) + "\n"
206         for i := start.Y + 1; i <= end.Y-1; i++ {
207                 str += string(la.lines[i].data) + "\n"
208         }
209         str += string(la.lines[end.Y].data[:endX])
210         return str
211 }
212
213 func (la *LineArray) State(lineN int) highlight.State {
214         return la.lines[lineN].state
215 }
216
217 func (la *LineArray) SetState(lineN int, s highlight.State) {
218         la.lines[lineN].state = s
219 }
220
221 func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
222         la.lines[lineN].match = m
223 }
224
225 func (la *LineArray) Match(lineN int) highlight.LineMatch {
226         return la.lines[lineN].match
227 }