9 "github.com/zyedidia/micro/cmd/micro/highlight"
12 func lineCounter(r io.Reader) (int, error) {
13 buf := make([]byte, 32*1024)
15 lineSep := []byte{'\n'}
19 count += bytes.Count(buf[:c], lineSep)
31 func runeToByteIndex(n int, txt []byte) int {
39 _, size := utf8.DecodeRune(txt)
56 match highlight.LineMatch
60 // A LineArray simply stores and array of lines and makes it easy to insert
62 type LineArray struct {
66 // NewLineArray returns a new line array from an array of bytes
67 func NewLineArray(reader io.Reader) *LineArray {
71 tee := io.TeeReader(reader, &buf)
72 numlines, _ := lineCounter(tee)
77 la.lines = make([]Line, numlines)
79 br := bufio.NewReader(&buf)
81 for i := 0; i < numlines; i++ {
82 data, err := br.ReadBytes('\n')
85 // la.lines[i] = Line{data[:len(data)], nil, nil, false}
86 la.lines[i].data = data
91 la.lines[i].data = data[:len(data)-1]
92 // la.lines[i] = Line{data[:len(data)-1], nil, nil, false}
99 // Returns the String representation of the LineArray
100 func (la *LineArray) String() string {
102 for i, l := range la.lines {
103 str += string(l.data)
104 if i != len(la.lines)-1 {
111 // NewlineBelow adds a newline below the given line number
112 func (la *LineArray) NewlineBelow(y int) {
113 la.lines = append(la.lines, Line{[]byte(" "), nil, nil, false})
114 copy(la.lines[y+2:], la.lines[y+1:])
115 la.lines[y+1] = Line{[]byte(""), la.lines[y].state, nil, false}
118 // inserts a byte array at a given location
119 func (la *LineArray) insert(pos Loc, value []byte) {
120 x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
121 // x, y := pos.x, pos.y
122 for i := 0; i < len(value); i++ {
123 if value[i] == '\n' {
129 la.insertByte(Loc{x, y}, value[i])
134 // inserts a byte at a given location
135 func (la *LineArray) insertByte(pos Loc, value byte) {
136 la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
137 copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
138 la.lines[pos.Y].data[pos.X] = value
141 // JoinLines joins the two lines a and b
142 func (la *LineArray) JoinLines(a, b int) {
143 la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
147 // Split splits a line at a given position
148 func (la *LineArray) Split(pos Loc) {
149 la.NewlineBelow(pos.Y)
150 la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
151 la.lines[pos.Y+1].state = la.lines[pos.Y].state
152 la.lines[pos.Y].state = nil
153 la.lines[pos.Y].match = nil
154 la.lines[pos.Y+1].match = nil
155 la.lines[pos.Y].rehighlight = true
156 la.DeleteToEnd(Loc{pos.X, pos.Y})
159 // removes from start to end
160 func (la *LineArray) remove(start, end Loc) string {
161 sub := la.Substr(start, end)
162 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
163 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
164 if start.Y == end.Y {
165 la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
167 for i := start.Y + 1; i <= end.Y-1; i++ {
168 la.DeleteLine(start.Y + 1)
170 la.DeleteToEnd(Loc{startX, start.Y})
171 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
172 la.JoinLines(start.Y, start.Y+1)
177 // DeleteToEnd deletes from the end of a line to the position
178 func (la *LineArray) DeleteToEnd(pos Loc) {
179 la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
182 // DeleteFromStart deletes from the start of a line to the position
183 func (la *LineArray) DeleteFromStart(pos Loc) {
184 la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
187 // DeleteLine deletes the line number
188 func (la *LineArray) DeleteLine(y int) {
189 la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
192 // DeleteByte deletes the byte at a position
193 func (la *LineArray) DeleteByte(pos Loc) {
194 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:])]
197 // Substr returns the string representation between two locations
198 func (la *LineArray) Substr(start, end Loc) string {
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 return string(la.lines[start.Y].data[startX:endX])
205 str += string(la.lines[start.Y].data[startX:]) + "\n"
206 for i := start.Y + 1; i <= end.Y-1; i++ {
207 str += string(la.lines[i].data) + "\n"
209 str += string(la.lines[end.Y].data[:endX])
213 func (la *LineArray) State(lineN int) highlight.State {
214 return la.lines[lineN].state
217 func (la *LineArray) SetState(lineN int, s highlight.State) {
218 la.lines[lineN].state = s
221 func (la *LineArray) SetMatch(lineN int, m highlight.LineMatch) {
222 la.lines[lineN].match = m
225 func (la *LineArray) Match(lineN int) highlight.LineMatch {
226 return la.lines[lineN].match