8 "github.com/zyedidia/micro/cmd/micro/highlight"
11 func runeToByteIndex(n int, txt []byte) int {
19 _, size := utf8.DecodeRune(txt)
36 match highlight.LineMatch
40 // A LineArray simply stores and array of lines and makes it easy to insert
42 type LineArray struct {
46 func Append(slice []Line, data ...Line) []Line {
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.
55 slice = slice[0 : l+len(data)]
56 for i, c := range data {
62 // NewLineArray returns a new line array from an array of bytes
63 func NewLineArray(size int64, reader io.Reader) *LineArray {
66 la.lines = make([]Line, 0, 1000)
68 br := bufio.NewReader(reader)
73 data, err := br.ReadBytes('\n')
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)
90 la.lines = Append(la.lines, Line{data[:], nil, nil, false})
91 // la.lines = Append(la.lines, Line{data[:len(data)]})
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})
105 // Returns the String representation of the LineArray
106 func (la *LineArray) String() string {
108 for i, l := range la.lines {
109 str += string(l.data)
110 if i != len(la.lines)-1 {
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}
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' {
135 la.insertByte(Loc{x, y}, value[i])
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
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)
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})
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:]...)
173 for i := start.Y + 1; i <= end.Y-1; i++ {
174 la.DeleteLine(start.Y + 1)
176 la.DeleteToEnd(Loc{startX, start.Y})
177 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
178 la.JoinLines(start.Y, start.Y+1)
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]
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:]
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:])]
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:])]
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])
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"
215 str += string(la.lines[end.Y].data[:endX])
219 func (la *LineArray) State(lineN int) highlight.State {
220 return la.lines[lineN].state
223 func (la *LineArray) SetState(lineN int, s highlight.State) {
224 la.lines[lineN].state = s
227 func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
228 la.lines[lineN].match = m
231 func (la *LineArray) Match(lineN int) highlight.LineMatch {
232 return la.lines[lineN].match