]> git.lizzy.rs Git - micro.git/blob - cmd/micro/lineArray.go
Fix draw ordering
[micro.git] / cmd / micro / lineArray.go
1 package main
2
3 import (
4         "bytes"
5         "unicode/utf8"
6 )
7
8 func runeToByteIndex(n int, txt []byte) int {
9         if n == 0 {
10                 return 0
11         }
12
13         count := 0
14         i := 0
15         for len(txt) > 0 {
16                 _, size := utf8.DecodeRune(txt)
17
18                 txt = txt[size:]
19                 count += size
20                 i++
21
22                 if i == n {
23                         break
24                 }
25         }
26         return count
27 }
28
29 // A LineArray simply stores and array of lines and makes it easy to insert
30 // and delete in it
31 type LineArray struct {
32         lines [][]byte
33 }
34
35 // NewLineArray returns a new line array from an array of bytes
36 func NewLineArray(text []byte) *LineArray {
37         la := new(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])
44         }
45
46         return la
47 }
48
49 // Returns the String representation of the LineArray
50 func (la *LineArray) String() string {
51         return string(bytes.Join(la.lines, []byte("\n")))
52 }
53
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("")
59 }
60
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++ {
66                 if value[i] == '\n' {
67                         la.Split(Loc{x, y})
68                         x = 0
69                         y++
70                         continue
71                 }
72                 la.insertByte(Loc{x, y}, value[i])
73                 x++
74         }
75 }
76
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
82 }
83
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])
87         la.DeleteLine(b)
88 }
89
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})
95 }
96
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:]...)
104         } else {
105                 for i := start.Y + 1; i <= end.Y-1; i++ {
106                         la.DeleteLine(start.Y + 1)
107                 }
108                 la.DeleteToEnd(Loc{startX, start.Y})
109                 la.DeleteFromStart(Loc{endX - 1, start.Y + 1})
110                 la.JoinLines(start.Y, start.Y+1)
111         }
112         return sub
113 }
114
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]
118 }
119
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:]
123 }
124
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:])]
128 }
129
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:])]
133 }
134
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])
141         }
142         var str string
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"
146         }
147         str += string(la.lines[end.Y][:endX])
148         return str
149 }