8 "github.com/zyedidia/micro/cmd/micro/highlight"
11 func runeToByteIndex(n int, txt []byte) int {
19 _, size := utf8.DecodeRune(txt)
32 // A Line contains the data in bytes as well as a highlight state, match
33 // and a flag for whether the highlighting needs to be updated
38 match highlight.LineMatch
42 // A LineArray simply stores and array of lines and makes it easy to insert
44 type LineArray struct {
48 // Append efficiently appends lines together
49 // It allocates an additional 10000 lines if the original estimate
51 func Append(slice []Line, data ...Line) []Line {
53 if l+len(data) > cap(slice) { // reallocate
54 newSlice := make([]Line, (l+len(data))+10000)
55 // The copy function is predeclared and works for any slice type.
59 slice = slice[0 : l+len(data)]
60 for i, c := range data {
66 // NewLineArray returns a new line array from an array of bytes
67 func NewLineArray(size int64, reader io.Reader) *LineArray {
70 la.lines = make([]Line, 0, 1000)
72 br := bufio.NewReader(reader)
77 data, err := br.ReadBytes('\n')
78 if len(data) > 1 && data[len(data)-2] == '\r' {
79 data = append(data[:len(data)-2], '\n')
83 } else if len(data) > 0 {
89 if n >= 1000 && loaded >= 0 {
90 totalLinesNum := int(float64(size) * (float64(n) / float64(loaded)))
91 newSlice := make([]Line, len(la.lines), totalLinesNum+10000)
92 // The copy function is predeclared and works for any slice type.
93 copy(newSlice, la.lines)
104 la.lines = Append(la.lines, Line{data[:], nil, nil, false})
105 // la.lines = Append(la.lines, Line{data[:len(data)]})
107 // Last line was read
110 // la.lines = Append(la.lines, Line{data[:len(data)-1]})
111 la.lines = Append(la.lines, Line{data[:len(data)-1], nil, nil, false})
119 // Returns the String representation of the LineArray
120 func (la *LineArray) String() string {
122 for i, l := range la.lines {
123 str += string(l.data)
124 if i != len(la.lines)-1 {
131 // SaveString returns the string that should be written to disk when
132 // the line array is saved
133 // It is the same as string but uses crlf or lf line endings depending
134 func (la *LineArray) SaveString(useCrlf bool) string {
136 for i, l := range la.lines {
137 str += string(l.data)
138 if i != len(la.lines)-1 {
148 // NewlineBelow adds a newline below the given line number
149 func (la *LineArray) NewlineBelow(y int) {
150 la.lines = append(la.lines, Line{[]byte(" "), nil, nil, false})
151 copy(la.lines[y+2:], la.lines[y+1:])
152 la.lines[y+1] = Line{[]byte(""), la.lines[y].state, nil, false}
155 // inserts a byte array at a given location
156 func (la *LineArray) insert(pos Loc, value []byte) {
157 x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
158 // x, y := pos.x, pos.y
159 for i := 0; i < len(value); i++ {
160 if value[i] == '\n' {
166 la.insertByte(Loc{x, y}, value[i])
171 // inserts a byte at a given location
172 func (la *LineArray) insertByte(pos Loc, value byte) {
173 la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
174 copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
175 la.lines[pos.Y].data[pos.X] = value
178 // JoinLines joins the two lines a and b
179 func (la *LineArray) JoinLines(a, b int) {
180 la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
184 // Split splits a line at a given position
185 func (la *LineArray) Split(pos Loc) {
186 la.NewlineBelow(pos.Y)
187 la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
188 la.lines[pos.Y+1].state = la.lines[pos.Y].state
189 la.lines[pos.Y].state = nil
190 la.lines[pos.Y].match = nil
191 la.lines[pos.Y+1].match = nil
192 la.lines[pos.Y].rehighlight = true
193 la.DeleteToEnd(Loc{pos.X, pos.Y})
196 // removes from start to end
197 func (la *LineArray) remove(start, end Loc) string {
198 sub := la.Substr(start, end)
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 la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
204 for i := start.Y + 1; i <= end.Y-1; i++ {
205 la.DeleteLine(start.Y + 1)
207 la.DeleteToEnd(Loc{startX, start.Y})
208 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
209 la.JoinLines(start.Y, start.Y+1)
214 // DeleteToEnd deletes from the end of a line to the position
215 func (la *LineArray) DeleteToEnd(pos Loc) {
216 la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
219 // DeleteFromStart deletes from the start of a line to the position
220 func (la *LineArray) DeleteFromStart(pos Loc) {
221 la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
224 // DeleteLine deletes the line number
225 func (la *LineArray) DeleteLine(y int) {
226 la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
229 // DeleteByte deletes the byte at a position
230 func (la *LineArray) DeleteByte(pos Loc) {
231 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:])]
234 // Substr returns the string representation between two locations
235 func (la *LineArray) Substr(start, end Loc) string {
236 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
237 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
238 if start.Y == end.Y {
239 return string(la.lines[start.Y].data[startX:endX])
242 str += string(la.lines[start.Y].data[startX:]) + "\n"
243 for i := start.Y + 1; i <= end.Y-1; i++ {
244 str += string(la.lines[i].data) + "\n"
246 str += string(la.lines[end.Y].data[:endX])
250 // State gets the highlight state for the given line number
251 func (la *LineArray) State(lineN int) highlight.State {
252 return la.lines[lineN].state
255 // SetState sets the highlight state at the given line number
256 func (la *LineArray) SetState(lineN int, s highlight.State) {
257 la.lines[lineN].state = s
260 // SetMatch sets the match at the given line number
261 func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
262 la.lines[lineN].match = m
265 // Match retrieves the match for the given line number
266 func (la *LineArray) Match(lineN int) highlight.LineMatch {
267 return la.lines[lineN].match