Debian Codegraph @

internal/buffer/search.go

raw
1 package buffer
2
3 import (
4 "regexp"
5 "unicode/utf8"
6
7 "github.com/zyedidia/micro/v2/internal/util"
8 )
9
10 // We want "^" and "$" to match only the beginning/end of a line, not the
11 // beginning/end of the search region if it is in the middle of a line.
12 // In that case we use padded regexps to require a rune before or after
13 // the match. (This also affects other empty-string patters like "\\b".)
14 // The following two flags indicate the padding used.
15 const (
16 padStart = 1 << iota
17 padEnd
18 )
19
20 func findLineParams(b *Buffer, start, end Loc, i int, r *regexp.Regexp) ([]byte, int, int, *regexp.Regexp) {
21 l := b.LineBytes(i)
22 charpos := 0
23 padMode := 0
24
25 if i == end.Y {
26 nchars := util.CharacterCount(l)
27 end.X = util.Clamp(end.X, 0, nchars)
28 if end.X < nchars {
29 l = util.SliceStart(l, end.X+1)
30 padMode |= padEnd
31 }
32 }
33
34 if i == start.Y {
35 nchars := util.CharacterCount(l)
36 start.X = util.Clamp(start.X, 0, nchars)
37 if start.X > 0 {
38 charpos = start.X - 1
39 l = util.SliceEnd(l, charpos)
40 padMode |= padStart
41 }
42 }
43
44 if padMode != 0 {
45 re, err := regexp.Compile(r.String() + `\E`)
46 if err == nil {
47 // r contains \Q without closing \E
48 r = re
49 }
50
51 if padMode == padStart {
52 r = regexp.MustCompile(".(?:" + r.String() + ")")
53 } else if padMode == padEnd {
54 r = regexp.MustCompile("(?:" + r.String() + ").")
55 } else {
56 // padMode == padStart|padEnd
57 r = regexp.MustCompile(".(?:" + r.String() + ").")
58 }
59 }
60
61 return l, charpos, padMode, r
62 }
63
64 func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
65 lastcn := util.CharacterCount(b.LineBytes(b.LinesNum() - 1))
66 if start.Y > b.LinesNum()-1 {
67 start.X = lastcn - 1
68 }
69 if end.Y > b.LinesNum()-1 {
70 end.X = lastcn
71 }
72 start.Y = util.Clamp(start.Y, 0, b.LinesNum()-1)
73 end.Y = util.Clamp(end.Y, 0, b.LinesNum()-1)
74
75 if start.GreaterThan(end) {
76 start, end = end, start
77 }
78
79 for i := start.Y; i <= end.Y; i++ {
80 l, charpos, padMode, rPadded := findLineParams(b, start, end, i, r)
81
82 match := rPadded.FindIndex(l)
83
84 if match != nil {
85 if padMode&padStart != 0 {
86 _, size := utf8.DecodeRune(l[match[0]:])
87 match[0] += size
88 }
89 if padMode&padEnd != 0 {
90 _, size := utf8.DecodeLastRune(l[:match[1]])
91 match[1] -= size
92 }
93 start := Loc{charpos + util.RunePos(l, match[0]), i}
94 end := Loc{charpos + util.RunePos(l, match[1]), i}
95 return [2]Loc{start, end}, true
96 }
97 }
98 return [2]Loc{}, false
99 }
100
101 func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
102 lastcn := util.CharacterCount(b.LineBytes(b.LinesNum() - 1))
103 if start.Y > b.LinesNum()-1 {
104 start.X = lastcn - 1
105 }
106 if end.Y > b.LinesNum()-1 {
107 end.X = lastcn
108 }
109 start.Y = util.Clamp(start.Y, 0, b.LinesNum()-1)
110 end.Y = util.Clamp(end.Y, 0, b.LinesNum()-1)
111
112 if start.GreaterThan(end) {
113 start, end = end, start
114 }
115
116 for i := end.Y; i >= start.Y; i-- {
117 charCount := util.CharacterCount(b.LineBytes(i))
118 from := Loc{0, i}.Clamp(start, end)
119 to := Loc{charCount, i}.Clamp(start, end)
120
121 allMatches := b.findAll(r, from, to)
122 if allMatches != nil {
123 match := allMatches[len(allMatches)-1]
124 return [2]Loc{match[0], match[1]}, true
125 }
126 }
127 return [2]Loc{}, false
128 }
129
130 func (b *Buffer) findAll(r *regexp.Regexp, start, end Loc) [][2]Loc {
131 var matches [][2]Loc
132 loc := start
133 for {
134 match, found := b.findDown(r, loc, end)
135 if !found {
136 break
137 }
138 matches = append(matches, match)
139 if match[0] != match[1] {
140 loc = match[1]
141 } else if match[1] != end {
142 loc = match[1].Move(1, b)
143 } else {
144 break
145 }
146 }
147 return matches
148 }
149
150 // FindNext finds the next occurrence of a given string in the buffer
151 // It returns the start and end location of the match (if found) and
152 // a boolean indicating if it was found
153 // May also return an error if the search regex is invalid
154 func (b *Buffer) FindNext(s string, start, end, from Loc, down bool, useRegex bool) ([2]Loc, bool, error) {
155 if s == "" {
156 return [2]Loc{}, false, nil
157 }
158
159 var r *regexp.Regexp
160 var err error
161
162 if !useRegex {
163 s = regexp.QuoteMeta(s)
164 }
165
166 if b.Settings["ignorecase"].(bool) {
167 r, err = regexp.Compile("(?i)" + s)
168 } else {
169 r, err = regexp.Compile(s)
170 }
171
172 if err != nil {
173 return [2]Loc{}, false, err
174 }
175
176 var found bool
177 var l [2]Loc
178 if down {
179 l, found = b.findDown(r, from, end)
180 if !found {
181 l, found = b.findDown(r, start, end)
182 }
183 } else {
184 l, found = b.findUp(r, from, start)
185 if !found {
186 l, found = b.findUp(r, end, start)
187 }
188 }
189 return l, found, nil
190 }
191
192 // ReplaceRegex replaces all occurrences of 'search' with 'replace' in the given area
193 // and returns the number of replacements made and the number of characters
194 // added or removed on the last line of the range
195 func (b *Buffer) ReplaceRegex(start, end Loc, search *regexp.Regexp, replace []byte, captureGroups bool) (int, int) {
196 if start.GreaterThan(end) {
197 start, end = end, start
198 }
199
200 charsEnd := util.CharacterCount(b.LineBytes(end.Y))
201 found := 0
202 var deltas []Delta
203
204 for i := start.Y; i <= end.Y; i++ {
205 l := b.LineBytes(i)
206 charCount := util.CharacterCount(l)
207 if (i == start.Y && start.X > 0) || (i == end.Y && end.X < charCount) {
208 // This replacement code works in general, but it creates a separate
209 // modification for each match. We only use it for the first and last
210 // lines, which may use padded regexps
211
212 from := Loc{0, i}.Clamp(start, end)
213 to := Loc{charCount, i}.Clamp(start, end)
214 matches := b.findAll(search, from, to)
215 found += len(matches)
216
217 for j := len(matches) - 1; j >= 0; j-- {
218 // if we counted upwards, the different deltas would interfere
219 match := matches[j]
220 var newText []byte
221 if captureGroups {
222 newText = search.ReplaceAll(b.Substr(match[0], match[1]), replace)
223 } else {
224 newText = replace
225 }
226 deltas = append(deltas, Delta{newText, match[0], match[1]})
227 }
228 } else {
229 newLine := search.ReplaceAllFunc(l, func(in []byte) []byte {
230 found++
231 var result []byte
232 if captureGroups {
233 match := search.FindSubmatchIndex(in)
234 result = search.Expand(result, replace, in, match)
235 } else {
236 result = replace
237 }
238 return result
239 })
240 deltas = append(deltas, Delta{newLine, Loc{0, i}, Loc{charCount, i}})
241 }
242 }
243
244 b.MultipleReplace(deltas)
245
246 return found, util.CharacterCount(b.LineBytes(end.Y)) - charsEnd
247 }