Debian Codegraph @

internal/buffer/loc.go

raw
1 package buffer
2
3 import (
4 "github.com/zyedidia/micro/v2/internal/util"
5 )
6
7 // Loc stores a location
8 type Loc struct {
9 X, Y int
10 }
11
12 // LessThan returns true if b is smaller
13 func (l Loc) LessThan(b Loc) bool {
14 if l.Y < b.Y {
15 return true
16 }
17 return l.Y == b.Y && l.X < b.X
18 }
19
20 // GreaterThan returns true if b is bigger
21 func (l Loc) GreaterThan(b Loc) bool {
22 if l.Y > b.Y {
23 return true
24 }
25 return l.Y == b.Y && l.X > b.X
26 }
27
28 // GreaterEqual returns true if b is greater than or equal to b
29 func (l Loc) GreaterEqual(b Loc) bool {
30 if l.Y > b.Y {
31 return true
32 }
33 if l.Y == b.Y && l.X > b.X {
34 return true
35 }
36 return l == b
37 }
38
39 // LessEqual returns true if b is less than or equal to b
40 func (l Loc) LessEqual(b Loc) bool {
41 if l.Y < b.Y {
42 return true
43 }
44 if l.Y == b.Y && l.X < b.X {
45 return true
46 }
47 return l == b
48 }
49
50 // Clamp clamps a loc between start and end
51 func (l Loc) Clamp(start, end Loc) Loc {
52 if l.GreaterEqual(end) {
53 return end
54 } else if l.LessThan(start) {
55 return start
56 }
57 return l
58 }
59
60 // The following functions require a buffer to know where newlines are
61
62 // Diff returns the distance between two locations
63 func DiffLA(a, b Loc, buf *LineArray) int {
64 if a.Y == b.Y {
65 if a.X > b.X {
66 return a.X - b.X
67 }
68 return b.X - a.X
69 }
70
71 // Make sure a is guaranteed to be less than b
72 if b.LessThan(a) {
73 a, b = b, a
74 }
75
76 loc := 0
77 for i := a.Y + 1; i < b.Y; i++ {
78 // + 1 for the newline
79 loc += util.CharacterCount(buf.LineBytes(i)) + 1
80 }
81 loc += util.CharacterCount(buf.LineBytes(a.Y)) - a.X + b.X + 1
82 return loc
83 }
84
85 // This moves the location one character to the right
86 func (l Loc) right(buf *LineArray) Loc {
87 if l == buf.End() {
88 return Loc{l.X + 1, l.Y}
89 }
90 var res Loc
91 if l.X < util.CharacterCount(buf.LineBytes(l.Y)) {
92 res = Loc{l.X + 1, l.Y}
93 } else {
94 res = Loc{0, l.Y + 1}
95 }
96 return res
97 }
98
99 // This moves the given location one character to the left
100 func (l Loc) left(buf *LineArray) Loc {
101 if l == buf.Start() {
102 return Loc{l.X - 1, l.Y}
103 }
104 var res Loc
105 if l.X > 0 {
106 res = Loc{l.X - 1, l.Y}
107 } else {
108 res = Loc{util.CharacterCount(buf.LineBytes(l.Y - 1)), l.Y - 1}
109 }
110 return res
111 }
112
113 // MoveLA moves the cursor n characters to the left or right
114 // It moves the cursor left if n is negative
115 func (l Loc) MoveLA(n int, buf *LineArray) Loc {
116 if n > 0 {
117 for i := 0; i < n; i++ {
118 l = l.right(buf)
119 }
120 return l
121 }
122 for i := 0; i < util.Abs(n); i++ {
123 l = l.left(buf)
124 }
125 return l
126 }
127
128 // Diff returns the difference between two locs
129 func (l Loc) Diff(b Loc, buf *Buffer) int {
130 return DiffLA(l, b, buf.LineArray)
131 }
132
133 // Move moves a loc n characters
134 func (l Loc) Move(n int, buf *Buffer) Loc {
135 return l.MoveLA(n, buf.LineArray)
136 }
137
138 // ByteOffset is just like ToCharPos except it counts bytes instead of runes
139 func ByteOffset(pos Loc, buf *Buffer) int {
140 x, y := pos.X, pos.Y
141 loc := 0
142 for i := 0; i < y; i++ {
143 // + 1 for the newline
144 loc += len(buf.Line(i)) + 1
145 }
146 loc += len(buf.Line(y)[:x])
147 return loc
148 }
149
150 // clamps a loc within a buffer
151 func clamp(pos Loc, la *LineArray) Loc {
152 return pos.Clamp(la.Start(), la.End())
153 }