8 func runeToByteIndex(n int, txt []byte) int {
16 _, size := utf8.DecodeRune(txt)
29 // A LineArray simply stores and array of lines and makes it easy to insert
31 type LineArray struct {
35 // NewLineArray returns a new line array from an array of bytes
36 func NewLineArray(text []byte) *LineArray {
38 // Split the bytes into lines
39 split := bytes.Split(text, []byte("\n"))
40 la.lines = make([][]byte, len(split))
41 for i := range split {
42 la.lines[i] = make([]byte, len(split[i]))
43 copy(la.lines[i], split[i])
49 // Returns the String representation of the LineArray
50 func (la *LineArray) String() string {
51 return string(bytes.Join(la.lines, []byte("\n")))
54 // NewlineBelow adds a newline below the given line number
55 func (la *LineArray) NewlineBelow(y int) {
56 la.lines = append(la.lines, []byte(" "))
57 copy(la.lines[y+2:], la.lines[y+1:])
58 la.lines[y+1] = []byte("")
61 // inserts a byte array at a given location
62 func (la *LineArray) insert(pos Loc, value []byte) {
63 x, y := runeToByteIndex(pos.X, la.lines[pos.Y]), pos.Y
64 // x, y := pos.x, pos.y
65 for i := 0; i < len(value); i++ {
72 la.insertByte(Loc{x, y}, value[i])
77 // inserts a byte at a given location
78 func (la *LineArray) insertByte(pos Loc, value byte) {
79 la.lines[pos.Y] = append(la.lines[pos.Y], 0)
80 copy(la.lines[pos.Y][pos.X+1:], la.lines[pos.Y][pos.X:])
81 la.lines[pos.Y][pos.X] = value
84 // JoinLines joins the two lines a and b
85 func (la *LineArray) JoinLines(a, b int) {
86 la.insert(Loc{len(la.lines[a]), a}, la.lines[b])
90 // Split splits a line at a given position
91 func (la *LineArray) Split(pos Loc) {
92 la.NewlineBelow(pos.Y)
93 la.insert(Loc{0, pos.Y + 1}, la.lines[pos.Y][pos.X:])
94 la.DeleteToEnd(Loc{pos.X, pos.Y})
97 // removes from start to end
98 func (la *LineArray) remove(start, end Loc) string {
99 sub := la.Substr(start, end)
100 startX := runeToByteIndex(start.X, la.lines[start.Y])
101 endX := runeToByteIndex(end.X, la.lines[end.Y])
102 if start.Y == end.Y {
103 la.lines[start.Y] = append(la.lines[start.Y][:startX], la.lines[start.Y][endX:]...)
105 for i := start.Y + 1; i <= end.Y-1; i++ {
106 la.DeleteLine(start.Y + 1)
108 la.DeleteToEnd(Loc{startX, start.Y})
109 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
110 la.JoinLines(start.Y, start.Y+1)
115 // DeleteToEnd deletes from the end of a line to the position
116 func (la *LineArray) DeleteToEnd(pos Loc) {
117 la.lines[pos.Y] = la.lines[pos.Y][:pos.X]
120 // DeleteFromStart deletes from the start of a line to the position
121 func (la *LineArray) DeleteFromStart(pos Loc) {
122 la.lines[pos.Y] = la.lines[pos.Y][pos.X+1:]
125 // DeleteLine deletes the line number
126 func (la *LineArray) DeleteLine(y int) {
127 la.lines = la.lines[:y+copy(la.lines[y:], la.lines[y+1:])]
130 // DeleteByte deletes the byte at a position
131 func (la *LineArray) DeleteByte(pos Loc) {
132 la.lines[pos.Y] = la.lines[pos.Y][:pos.X+copy(la.lines[pos.Y][pos.X:], la.lines[pos.Y][pos.X+1:])]
135 // Substr returns the string representation between two locations
136 func (la *LineArray) Substr(start, end Loc) string {
137 startX := runeToByteIndex(start.X, la.lines[start.Y])
138 endX := runeToByteIndex(end.X, la.lines[end.Y])
139 if start.Y == end.Y {
140 return string(la.lines[start.Y][startX:endX])
143 str += string(la.lines[start.Y][startX:]) + "\n"
144 for i := start.Y + 1; i <= end.Y-1; i++ {
145 str += string(la.lines[i]) + "\n"
147 str += string(la.lines[end.Y][:endX])