8 "github.com/zyedidia/highlight"
11 func runeToByteIndex(n int, txt []byte) int {
19 _, size := utf8.DecodeRune(txt)
38 // A LineArray simply stores and array of lines and makes it easy to insert
40 type LineArray struct {
44 // NewLineArray returns a new line array from an array of bytes
45 func NewLineArray(reader io.Reader) *LineArray {
47 br := bufio.NewReader(reader)
51 data, err := br.ReadBytes('\n')
54 la.lines = append(la.lines, Line{data[:len(data)], nil})
59 la.lines = append(la.lines, Line{data[:len(data)-1], nil})
67 // Returns the String representation of the LineArray
68 func (la *LineArray) String() string {
70 for i, l := range la.lines {
72 if i != len(la.lines)-1 {
79 // NewlineBelow adds a newline below the given line number
80 func (la *LineArray) NewlineBelow(y int) {
81 la.lines = append(la.lines, Line{[]byte(" "), nil})
82 copy(la.lines[y+2:], la.lines[y+1:])
83 la.lines[y+1] = Line{[]byte(""), nil}
86 // inserts a byte array at a given location
87 func (la *LineArray) insert(pos Loc, value []byte) {
88 x, y := runeToByteIndex(pos.X, la.lines[pos.Y].data), pos.Y
89 // x, y := pos.x, pos.y
90 for i := 0; i < len(value); i++ {
97 la.insertByte(Loc{x, y}, value[i])
102 // inserts a byte at a given location
103 func (la *LineArray) insertByte(pos Loc, value byte) {
104 la.lines[pos.Y].data = append(la.lines[pos.Y].data, 0)
105 copy(la.lines[pos.Y].data[pos.X+1:], la.lines[pos.Y].data[pos.X:])
106 la.lines[pos.Y].data[pos.X] = value
109 // JoinLines joins the two lines a and b
110 func (la *LineArray) JoinLines(a, b int) {
111 la.insert(Loc{len(la.lines[a].data), a}, la.lines[b].data)
115 // Split splits a line at a given position
116 func (la *LineArray) Split(pos Loc) {
117 la.NewlineBelow(pos.Y)
118 la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y].data[pos.X:])
119 la.lines[pos.Y+1].state = la.lines[pos.Y].state
120 la.DeleteToEnd(Loc{pos.X, pos.Y})
123 // removes from start to end
124 func (la *LineArray) remove(start, end Loc) string {
125 sub := la.Substr(start, end)
126 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
127 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
128 if start.Y == end.Y {
129 la.lines[start.Y].data = append(la.lines[start.Y].data[:startX], la.lines[start.Y].data[endX:]...)
131 for i := start.Y + 1; i <= end.Y-1; i++ {
132 la.DeleteLine(start.Y + 1)
134 la.DeleteToEnd(Loc{startX, start.Y})
135 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
136 la.JoinLines(start.Y, start.Y+1)
141 // DeleteToEnd deletes from the end of a line to the position
142 func (la *LineArray) DeleteToEnd(pos Loc) {
143 la.lines[pos.Y].data = la.lines[pos.Y].data[:pos.X]
146 // DeleteFromStart deletes from the start of a line to the position
147 func (la *LineArray) DeleteFromStart(pos Loc) {
148 la.lines[pos.Y].data = la.lines[pos.Y].data[pos.X+1:]
151 // DeleteLine deletes the line number
152 func (la *LineArray) DeleteLine(y int) {
153 la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
156 // DeleteByte deletes the byte at a position
157 func (la *LineArray) DeleteByte(pos Loc) {
158 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:])]
161 // Substr returns the string representation between two locations
162 func (la *LineArray) Substr(start, end Loc) string {
163 startX := runeToByteIndex(start.X, la.lines[start.Y].data)
164 endX := runeToByteIndex(end.X, la.lines[end.Y].data)
165 if start.Y == end.Y {
166 return string(la.lines[start.Y].data[startX:endX])
169 str += string(la.lines[start.Y].data[startX:]) + "\n"
170 for i := start.Y + 1; i <= end.Y-1; i++ {
171 str += string(la.lines[i].data) + "\n"
173 str += string(la.lines[end.Y].data[:endX])
177 func (la *LineArray) LineData() [][]byte {
178 lines := make([][]byte, len(la.lines))
179 for i, l := range la.lines {
185 func (la *LineArray) State(lineN int) highlight.State {
186 return la.lines[lineN].state
189 func (la *LineArray) SetState(lineN int, s highlight.State) {
190 la.lines[lineN].state = s