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')
74 if len(data) > 1 && data[len(data)-2] == '\r' {
75 data = append(data[:len(data)-2], '\n')
79 } else if len(data) > 0 {
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)
100 la.lines = Append(la.lines, Line{data[:], nil, nil, false})
101 // la.lines = Append(la.lines, Line{data[:len(data)]})
103 // Last line was read
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})
115 // Returns the String representation of the LineArray
116 func (la *LineArray) String() string {
118 for i, l := range la.lines {
119 str += string(l.data)
120 if i != len(la.lines)-1 {
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 {
132 for i, l := range la.lines {
133 str += string(l.data)
134 if i != len(la.lines)-1 {
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}
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' {
162 la.insertByte(Loc{x, y}, value[i])
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
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)
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})
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:]...)
200 for i := start.Y + 1; i <= end.Y-1; i++ {
201 la.DeleteLine(start.Y + 1)
203 la.DeleteToEnd(Loc{startX, start.Y})
204 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
205 la.JoinLines(start.Y, start.Y+1)
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]
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:]
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:])]
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:])]
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])
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"
242 str += string(la.lines[end.Y].data[:endX])
246 func (la *LineArray) State(lineN int) highlight.State {
247 return la.lines[lineN].state
250 func (la *LineArray) SetState(lineN int, s highlight.State) {
251 la.lines[lineN].state = s
254 func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
255 la.lines[lineN].match = m
258 func (la *LineArray) Match(lineN int) highlight.LineMatch {
259 return la.lines[lineN].match