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 copy(newSlice, la.lines)
103 la.lines = Append(la.lines, Line{data[:], nil, nil, false})
104 // la.lines = Append(la.lines, Line{data[:len(data)]})
106 // Last line was read
109 // la.lines = Append(la.lines, Line{data[:len(data)-1]})
110 la.lines = Append(la.lines, Line{data[:len(data)-1], nil, nil, false})
118 // Returns the String representation of the LineArray
119 func (la *LineArray) String() string {
121 for i, l := range la.lines {
122 str += string(l.data)
123 if i != len(la.lines)-1 {
130 // SaveString returns the string that should be written to disk when
131 // the line array is saved
132 // It is the same as string but uses crlf or lf line endings depending
133 func (la *LineArray) SaveString(useCrlf bool) string {
135 for i, l := range la.lines {
136 str += string(l.data)
137 if i != len(la.lines)-1 {
147 // NewlineBelow adds a newline below the given line number
148 func (la *LineArray) NewlineBelow(y int) {
149 la.lines = append(la.lines, Line{[]byte{' '}, nil, nil, false})
150 copy(la.lines[y+2:], la.lines[y+1:])
151 la.lines[y+1] = Line{[]byte{}, la.lines[y].state, nil, false}
154 // inserts a byte array at a given location
155 func (la *LineArray) insert(pos Loc, value []byte) {
156 x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
157 // x, y := pos.x, pos.y
158 for i := 0; i < len(value); i++ {
159 if value[i] == '\n' {
165 la.insertByte(Loc{x, y}, value[i])
170 // inserts a byte at a given location
171 func (la *LineArray) insertByte(pos Loc, value byte) {
172 la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
173 copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
174 la.lines[pos.Y].data[pos.X] = value
177 // JoinLines joins the two lines a and b
178 func (la *LineArray) JoinLines(a, b int) {
179 la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
183 // Split splits a line at a given position
184 func (la *LineArray) Split(pos Loc) {
185 la.NewlineBelow(pos.Y)
186 la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
187 la.lines[pos.Y+1].state = la.lines[pos.Y].state
188 la.lines[pos.Y].state = nil
189 la.lines[pos.Y].match = nil
190 la.lines[pos.Y+1].match = nil
191 la.lines[pos.Y].rehighlight = true
192 la.DeleteToEnd(Loc{pos.X, pos.Y})
195 // removes from start to end
196 func (la *LineArray) remove(start, end Loc) string {
197 sub := la.Substr(start, end)
198 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
199 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
200 if start.Y == end.Y {
201 la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
203 for i := start.Y + 1; i <= end.Y-1; i++ {
204 la.DeleteLine(start.Y + 1)
206 la.DeleteToEnd(Loc{startX, start.Y})
207 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
208 la.JoinLines(start.Y, start.Y+1)
213 // DeleteToEnd deletes from the end of a line to the position
214 func (la *LineArray) DeleteToEnd(pos Loc) {
215 la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
218 // DeleteFromStart deletes from the start of a line to the position
219 func (la *LineArray) DeleteFromStart(pos Loc) {
220 la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
223 // DeleteLine deletes the line number
224 func (la *LineArray) DeleteLine(y int) {
225 la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
228 // DeleteByte deletes the byte at a position
229 func (la *LineArray) DeleteByte(pos Loc) {
230 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:])]
233 // Substr returns the string representation between two locations
234 func (la *LineArray) Substr(start, end Loc) string {
235 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
236 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
237 if start.Y == end.Y {
238 return string(la.lines[start.Y].data[startX:endX])
241 str += string(la.lines[start.Y].data[startX:]) + "\n"
242 for i := start.Y + 1; i <= end.Y-1; i++ {
243 str += string(la.lines[i].data) + "\n"
245 str += string(la.lines[end.Y].data[:endX])
249 // State gets the highlight state for the given line number
250 func (la *LineArray) State(lineN int) highlight.State {
251 return la.lines[lineN].state
254 // SetState sets the highlight state at the given line number
255 func (la *LineArray) SetState(lineN int, s highlight.State) {
256 la.lines[lineN].state = s
259 // SetMatch sets the match at the given line number
260 func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
261 la.lines[lineN].match = m
264 // Match retrieves the match for the given line number
265 func (la *LineArray) Match(lineN int) highlight.LineMatch {
266 return la.lines[lineN].match