]> git.lizzy.rs Git - micro.git/blob - cmd/micro/lineArray.go
e34d8ae69355735cd9cc437b49bbeb2548eb6c1c
[micro.git] / cmd / micro / lineArray.go
1 package main
2
3 import (
4         "bufio"
5         "io"
6         "unicode/utf8"
7
8         "github.com/zyedidia/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 }
37
38 // A LineArray simply stores and array of lines and makes it easy to insert
39 // and delete in it
40 type LineArray struct {
41         lines []Line
42 }
43
44 // NewLineArray returns a new line array from an array of bytes
45 func NewLineArray(reader io.Reader) *LineArray {
46         la := new(LineArray)
47         br := bufio.NewReader(reader)
48
49         i := 0
50         for {
51                 data, err := br.ReadBytes('\n')
52                 if err != nil {
53                         if err == io.EOF {
54                                 la.lines = append(la.lines, Line{data[:len(data)], nil})
55                         }
56                         // Last line was read
57                         break
58                 } else {
59                         la.lines = append(la.lines, Line{data[:len(data)-1], nil})
60                 }
61                 i++
62         }
63
64         return la
65 }
66
67 // Returns the String representation of the LineArray
68 func (la *LineArray) String() string {
69         str := ""
70         for i, l := range la.lines {
71                 str += string(l.data)
72                 if i != len(la.lines)-1 {
73                         str += "\n"
74                 }
75         }
76         return str
77 }
78
79 // NewlineBelow adds a newline below the given line number
80 func (la *LineArray) NewlineBelow(y int) {
81         la.lines = append(la.lines, Line{[]byte(" "), nil})
82         copy(la.lines[y+2:], la.lines[y+1:])
83         la.lines[y+1] = Line{[]byte(""), nil}
84 }
85
86 // inserts a byte array at a given location
87 func (la *LineArray) insert(pos Loc, value []byte) {
88         x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
89         // x, y := pos.x, pos.y
90         for i := 0; i < len(value); i++ {
91                 if value[i] == '\n' {
92                         la.Split(Loc{x, y})
93                         x = 0
94                         y++
95                         continue
96                 }
97                 la.insertByte(Loc{x, y}, value[i])
98                 x++
99         }
100 }
101
102 // inserts a byte at a given location
103 func (la *LineArray) insertByte(pos Loc, value byte) {
104         la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
105         copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
106         la.lines[pos.Y].data[pos.X] = value
107 }
108
109 // JoinLines joins the two lines a and b
110 func (la *LineArray) JoinLines(a, b int) {
111         la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
112         la.DeleteLine(b)
113 }
114
115 // Split splits a line at a given position
116 func (la *LineArray) Split(pos Loc) {
117         la.NewlineBelow(pos.Y)
118         la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
119         la.lines[pos.Y+1].state = la.lines[pos.Y].state
120         la.DeleteToEnd(Loc{pos.X, pos.Y})
121 }
122
123 // removes from start to end
124 func (la *LineArray) remove(start, end Loc) string {
125         sub := la.Substr(start, end)
126         startX := runeToByteIndex(start.X, la.lines[start.Y].data)
127         endX := runeToByteIndex(end.X, la.lines[end.Y].data)
128         if start.Y == end.Y {
129                 la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
130         } else {
131                 for i := start.Y + 1; i <= end.Y-1; i++ {
132                         la.DeleteLine(start.Y + 1)
133                 }
134                 la.DeleteToEnd(Loc{startX, start.Y})
135                 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
136                 la.JoinLines(start.Y, start.Y+1)
137         }
138         return sub
139 }
140
141 // DeleteToEnd deletes from the end of a line to the position
142 func (la *LineArray) DeleteToEnd(pos Loc) {
143         la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
144 }
145
146 // DeleteFromStart deletes from the start of a line to the position
147 func (la *LineArray) DeleteFromStart(pos Loc) {
148         la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
149 }
150
151 // DeleteLine deletes the line number
152 func (la *LineArray) DeleteLine(y int) {
153         la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
154 }
155
156 // DeleteByte deletes the byte at a position
157 func (la *LineArray) DeleteByte(pos Loc) {
158         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:])]
159 }
160
161 // Substr returns the string representation between two locations
162 func (la *LineArray) Substr(start, end Loc) string {
163         startX := runeToByteIndex(start.X, la.lines[start.Y].data)
164         endX := runeToByteIndex(end.X, la.lines[end.Y].data)
165         if start.Y == end.Y {
166                 return string(la.lines[start.Y].data[startX:endX])
167         }
168         var str string
169         str += string(la.lines[start.Y].data[startX:]) + "\n"
170         for i := start.Y + 1; i <= end.Y-1; i++ {
171                 str += string(la.lines[i].data) + "\n"
172         }
173         str += string(la.lines[end.Y].data[:endX])
174         return str
175 }
176
177 func (la *LineArray) LineData() [][]byte {
178         lines := make([][]byte, len(la.lines))
179         for i, l := range la.lines {
180                 lines[i] = l.data
181         }
182         return lines
183 }
184
185 func (la *LineArray) State(lineN int) highlight.State {
186         return la.lines[lineN].state
187 }
188
189 func (la *LineArray) SetState(lineN int, s highlight.State) {
190         la.lines[lineN].state = s
191 }