-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsearch.go
More file actions
276 lines (247 loc) · 7.31 KB
/
search.go
File metadata and controls
276 lines (247 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package midterm
import (
"strings"
"unicode/utf8"
)
// SearchHighlight represents a highlighted range on a single row.
type SearchHighlight struct {
Col, End int // column range [Col, End)
Current bool // true = current match (distinct style)
}
// SearchMatch stores an individual match location for indexed navigation.
type SearchMatch struct {
Row, Col, End int
}
// searchState holds cached state from the previous Search() call so that
// subsequent calls with the same query can skip unchanged rows.
type searchState struct {
query string // lowercased query from last search
queryRunes int // rune length of query
changes []uint64 // snapshot of Changes[] at last search
maxY int // MaxY at last search
}
// Search finds all case-insensitive occurrences of query in Content
// and populates SearchHighlights. If the query is the same as the
// previous call, only rows that have changed since then are re-scanned.
// Returns the total match count.
func (vt *Terminal) Search(query string) int {
if query == "" {
vt.SearchClear()
return 0
}
lowerQuery := strings.ToLower(query)
queryRuneLen := utf8.RuneCountInString(lowerQuery)
// Check if we can do an incremental update.
if vt.searchCache != nil &&
vt.searchCache.query == lowerQuery &&
vt.SearchHighlights != nil {
return vt.searchIncremental(lowerQuery, queryRuneLen)
}
// Full search.
return vt.searchFull(lowerQuery, queryRuneLen)
}
// searchFull does a complete search from scratch.
func (vt *Terminal) searchFull(lowerQuery string, queryRuneLen int) int {
vt.SearchMatches = vt.SearchMatches[:0]
if vt.SearchHighlights == nil {
vt.SearchHighlights = make(map[int][]SearchHighlight)
} else {
clear(vt.SearchHighlights)
}
used := vt.MaxY + 1
for row := 0; row < used && row < len(vt.Content); row++ {
vt.searchRow(row, lowerQuery, queryRuneLen)
}
vt.snapshotSearchState(lowerQuery, queryRuneLen)
return len(vt.SearchMatches)
}
// searchIncremental re-scans only rows that have changed since the last
// search, plus any new rows that appeared.
func (vt *Terminal) searchIncremental(lowerQuery string, queryRuneLen int) int {
cache := vt.searchCache
used := vt.MaxY + 1
// Collect rows that need re-scanning.
var dirtyRows []int
limit := min(used, len(vt.Changes))
cachedLimit := min(limit, len(cache.changes))
for row := 0; row < cachedLimit; row++ {
if vt.Changes[row] != cache.changes[row] {
dirtyRows = append(dirtyRows, row)
}
}
// New rows beyond what we last saw.
for row := cache.maxY + 1; row < limit; row++ {
dirtyRows = append(dirtyRows, row)
}
if len(dirtyRows) == 0 {
return len(vt.SearchMatches)
}
// Remove old matches/highlights for dirty rows.
dirtySet := make(map[int]bool, len(dirtyRows))
for _, r := range dirtyRows {
dirtySet[r] = true
delete(vt.SearchHighlights, r)
}
// Filter out stale matches from SearchMatches.
n := 0
for _, m := range vt.SearchMatches {
if !dirtySet[m.Row] {
vt.SearchMatches[n] = m
n++
}
}
vt.SearchMatches = vt.SearchMatches[:n]
// Re-scan dirty rows and collect new matches.
var newMatches []SearchMatch
for _, row := range dirtyRows {
if row >= len(vt.Content) {
continue
}
before := len(newMatches)
newMatches = vt.searchRowInto(row, lowerQuery, queryRuneLen, newMatches)
// Also populate highlights.
for _, m := range newMatches[before:] {
vt.SearchHighlights[row] = append(vt.SearchHighlights[row], SearchHighlight{
Col: m.Col,
End: m.End,
})
}
}
// Merge new matches into SearchMatches in row order.
if len(newMatches) > 0 {
vt.SearchMatches = mergeMatches(vt.SearchMatches, newMatches)
}
vt.snapshotSearchState(lowerQuery, queryRuneLen)
return len(vt.SearchMatches)
}
// searchRow scans a single row and appends matches to SearchMatches and
// SearchHighlights.
func (vt *Terminal) searchRow(row int, lowerQuery string, queryRuneLen int) {
line := vt.Content[row]
lineStr := strings.ToLower(string(line))
searchFrom := 0
for {
idx := strings.Index(lineStr[searchFrom:], lowerQuery)
if idx < 0 {
break
}
col := utf8.RuneCountInString(lineStr[:searchFrom+idx])
end := col + queryRuneLen
vt.SearchHighlights[row] = append(vt.SearchHighlights[row], SearchHighlight{
Col: col,
End: end,
})
vt.SearchMatches = append(vt.SearchMatches, SearchMatch{
Row: row,
Col: col,
End: end,
})
searchFrom += idx + len(lowerQuery)
}
}
// searchRowInto is like searchRow but appends to an external slice instead
// of vt.SearchMatches (used during incremental merge).
func (vt *Terminal) searchRowInto(row int, lowerQuery string, queryRuneLen int, dst []SearchMatch) []SearchMatch {
line := vt.Content[row]
lineStr := strings.ToLower(string(line))
searchFrom := 0
for {
idx := strings.Index(lineStr[searchFrom:], lowerQuery)
if idx < 0 {
break
}
col := utf8.RuneCountInString(lineStr[:searchFrom+idx])
end := col + queryRuneLen
dst = append(dst, SearchMatch{
Row: row,
Col: col,
End: end,
})
searchFrom += idx + len(lowerQuery)
}
return dst
}
// snapshotSearchState captures the current Changes[] and MaxY so the next
// Search() call can detect which rows are dirty.
func (vt *Terminal) snapshotSearchState(lowerQuery string, queryRuneLen int) {
used := vt.MaxY + 1
limit := min(used, len(vt.Changes))
changes := make([]uint64, limit)
copy(changes, vt.Changes[:limit])
vt.searchCache = &searchState{
query: lowerQuery,
queryRunes: queryRuneLen,
changes: changes,
maxY: vt.MaxY,
}
}
// mergeMatches merges two sorted-by-row match slices into one.
func mergeMatches(a, b []SearchMatch) []SearchMatch {
result := make([]SearchMatch, 0, len(a)+len(b))
i, j := 0, 0
for i < len(a) && j < len(b) {
if a[i].Row < b[j].Row || (a[i].Row == b[j].Row && a[i].Col <= b[j].Col) {
result = append(result, a[i])
i++
} else {
result = append(result, b[j])
j++
}
}
result = append(result, a[i:]...)
result = append(result, b[j:]...)
return result
}
// SearchClear removes all search highlights, matches, and cached state.
func (vt *Terminal) SearchClear() {
vt.SearchHighlights = nil
vt.SearchMatches = nil
vt.searchCache = nil
}
// SearchSetCurrent marks the match at the given index as "current"
// (receives CurrentStyle). Returns the (row, col) of that match.
// If idx is out of range, clears any current highlight.
func (vt *Terminal) SearchSetCurrent(idx int) (row, col int) {
// Clear all Current flags first.
for r, hls := range vt.SearchHighlights {
for i := range hls {
if hls[i].Current {
hls[i].Current = false
vt.SearchHighlights[r] = hls
}
}
}
if idx < 0 || idx >= len(vt.SearchMatches) {
return -1, -1
}
m := vt.SearchMatches[idx]
hls := vt.SearchHighlights[m.Row]
for i := range hls {
if hls[i].Col == m.Col && hls[i].End == m.End {
hls[i].Current = true
vt.SearchHighlights[m.Row] = hls
break
}
}
return m.Row, m.Col
}
// SearchMatchCount returns the number of matches from the last Search call.
func (vt *Terminal) SearchMatchCount() int {
return len(vt.SearchMatches)
}
// SearchMatchRows returns the distinct row indices that have matches,
// in ascending order.
func (vt *Terminal) SearchMatchRows() []int {
if len(vt.SearchMatches) == 0 {
return nil
}
var rows []int
lastRow := -1
for _, m := range vt.SearchMatches {
if m.Row != lastRow {
rows = append(rows, m.Row)
lastRow = m.Row
}
}
return rows
}