Repository navigation
Expand file tree
/
Copy pathsearch.go
More file actions
308 lines (278 loc) · 8.15 KB
/
Copy pathsearch.go
File metadata and controls
308 lines (278 loc) · 8.15 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package contexting
import (
"encoding/json"
"fmt"
"path/filepath"
"sort"
"strings"
"time"
)
type SearchResult struct {
Path string `json:"path"`
Type string `json:"type"`
Score int `json:"score"`
Matches []string `json:"matches"`
Breakdown []string `json:"breakdown,omitempty"`
}
type SearchResponse struct {
Source string `json:"source"`
IndexGeneratedAt *time.Time `json:"index_generated_at,omitempty"`
Fallback *bool `json:"fallback,omitempty"`
Results []SearchResult `json:"results"`
}
type SearchOptions struct {
Limit int
MinScore int
TypeFilter string
IncludeDebug bool
ContentFallback bool // enable content-based fallback via ripgrep
ContentMatchScore int // score for grep-only files (defaults to 1)
ContentRoot string // project root for rg (empty defaults to index.RootPath)
}
func SearchHints(index *ContextIndex, query string, limit int) []SearchResult {
return SearchHintsWithOptions(index, query, SearchOptions{Limit: limit})
}
func SearchHintsWithOptions(index *ContextIndex, query string, opts SearchOptions) []SearchResult {
if index == nil || index.Tree == nil {
return nil
}
if opts.Limit <= 0 {
opts.Limit = 5
}
if opts.TypeFilter == "" {
opts.TypeFilter = "all"
}
tokens := tokenize(query)
tokenGroups := make(map[string][]string)
for _, primary := range tokenizeBase(query) {
for _, variant := range tokenize(primary) {
tokenGroups[variant] = append(tokenGroups[variant], primary)
}
}
if len(tokens) == 0 {
return nil
}
results := make([]SearchResult, 0)
root := index.Tree.FullPath
walkTree(index.Tree, func(node *Node) {
if node == index.Tree || !passesTypeFilter(node, opts.TypeFilter) {
return
}
relPath := node.FullPath
if rel, err := filepath.Rel(root, node.FullPath); err == nil {
relPath = rel
}
relLower := strings.ToLower(filepath.ToSlash(relPath))
baseNameLower := strings.ToLower(filepath.Base(node.FullPath))
segments := strings.Split(relLower, "/")
score := 0
covered := make(map[string]bool)
matches := make([]string, 0)
breakdown := make([]string, 0)
for _, token := range tokens {
if token == "" {
continue
}
before := score
if relLower == token || baseNameLower == token {
score += 12
matches = append(matches, "exact:"+token)
breakdown = append(breakdown, "exact match +12: "+token)
} else if strings.Contains(baseNameLower, token) {
score += 7
matches = append(matches, "basename:"+token)
breakdown = append(breakdown, "basename contains +7: "+token)
} else if hasSegmentPrefix(segments, token) {
score += 5
matches = append(matches, "segment-prefix:"+token)
breakdown = append(breakdown, "segment prefix +5: "+token)
} else if strings.Contains(relLower, token) {
score += 4
matches = append(matches, "path:"+token)
breakdown = append(breakdown, "path contains +4: "+token)
}
synonymScore, synonymMatches, synonymBreakdown := scoreSynonymToken(node.Synonyms, token)
symbolScore, symbolMatches, symbolBreakdown := scoreSymbolToken(node.Symbols, token)
score += synonymScore + symbolScore
matches = append(matches, synonymMatches...)
matches = append(matches, symbolMatches...)
breakdown = append(breakdown, synonymBreakdown...)
breakdown = append(breakdown, symbolBreakdown...)
if score > before {
for _, primary := range tokenGroups[token] {
covered[primary] = true
}
}
}
if len(covered) > 1 {
bonus := 4 * (len(covered) - 1)
score += bonus
breakdown = append(breakdown, fmt.Sprintf("query coverage +%d: %d distinct terms", bonus, len(covered)))
}
// Exact basename match — query is the filename (with or without extension)
stemLower := strings.ToLower(strings.TrimSuffix(baseNameLower, filepath.Ext(baseNameLower)))
queryLower := strings.ToLower(query)
if queryLower == baseNameLower || queryLower == stemLower {
score += 15
matches = append(matches, "exact-basename:"+baseNameLower)
breakdown = append(breakdown, "exact basename +15: "+baseNameLower)
}
if node.Type == "file" && score > 0 {
score += 1
breakdown = append(breakdown, "file bias +1")
}
if score <= 0 || score < opts.MinScore {
return
}
result := SearchResult{
Path: relPath,
Type: node.Type,
Score: score,
Matches: dedupeStrings(matches),
}
if opts.IncludeDebug {
result.Breakdown = dedupeStrings(breakdown)
}
results = append(results, result)
})
sort.Slice(results, func(i, j int) bool {
if results[i].Score != results[j].Score {
return results[i].Score > results[j].Score
}
if results[i].Type != results[j].Type {
return results[i].Type < results[j].Type
}
return results[i].Path < results[j].Path
})
// Apply confidence gap heuristic to reduce noise
if len(results) > 1 {
literalMatch := false
for _, match := range results[0].Matches {
if strings.HasPrefix(match, "exact-basename:") || strings.HasPrefix(match, "sym-exact:") {
literalMatch = true
break
}
}
cutoff := len(results)
for i := 1; i < len(results); i++ {
// If score drops by more than half from previous result, cut here
if results[i-1].Score > 0 && results[i].Score > 0 && results[i].Score*2 < results[i-1].Score {
cutoff = i
break
}
}
// Conceptual queries benefit from alternatives; literal lookups can stop
// at the first clear gap without padding the response to three results.
if !literalMatch && cutoff < 3 && len(results) >= 3 {
cutoff = 3
}
if cutoff < len(results) {
results = results[:cutoff]
}
}
if opts.ContentFallback && passesTypeFilter(&Node{Type: "file"}, opts.TypeFilter) {
existing := make(map[string]bool, len(results))
for _, result := range results {
existing[filepath.Clean(result.Path)] = true
}
for _, result := range searchIndexedContent(index, tokens, opts) {
if !existing[filepath.Clean(result.Path)] {
existing[filepath.Clean(result.Path)] = true
results = append(results, result)
}
}
sort.Slice(results, func(i, j int) bool {
if results[i].Score != results[j].Score {
return results[i].Score > results[j].Score
}
return results[i].Path < results[j].Path
})
}
if len(results) > opts.Limit {
results = results[:opts.Limit]
}
return results
}
func hasSegmentPrefix(segments []string, token string) bool {
for _, seg := range segments {
if strings.HasPrefix(seg, token) {
return true
}
}
return false
}
func passesTypeFilter(node *Node, filter string) bool {
switch strings.ToLower(strings.TrimSpace(filter)) {
case "", "all":
return true
case "files", "file":
return node.Type == "file"
case "dirs", "dir", "directories", "directory":
return node.Type == "directory"
default:
return true
}
}
func tokenizeBase(input string) []string {
cleaned := strings.ToLower(input)
cleaned = strings.NewReplacer(
",", " ",
".", " ",
"/", " ",
"_", " ",
"-", " ",
":", " ",
";", " ",
"(", " ",
")", " ",
"[", " ",
"]", " ",
"{", " ",
"}", " ",
"\n", " ",
"\t", " ",
).Replace(cleaned)
parts := strings.Fields(cleaned)
if len(parts) == 0 {
return nil
}
base := dedupeStrings(parts)
filteredBase := make([]string, 0, len(base))
for _, token := range base {
if isLowSignalToken(token) || isSourceExtension(token) {
continue
}
filteredBase = append(filteredBase, token)
}
if len(filteredBase) == 0 {
return nil
}
return filteredBase
}
func tokenize(input string) []string {
filteredBase := tokenizeBase(input)
expanded := make([]string, 0, len(filteredBase)*2)
for _, token := range filteredBase {
expanded = append(expanded, token)
if strings.HasSuffix(token, "s") && len(token) > 3 {
stem := strings.TrimSuffix(token, "s")
if !isLowSignalToken(stem) {
expanded = append(expanded, stem)
}
}
if strings.HasSuffix(token, "ing") && len(token) > 5 {
stem := strings.TrimSuffix(token, "ing")
if !isLowSignalToken(stem) {
expanded = append(expanded, stem)
}
}
}
return dedupeStrings(expanded)
}
func searchResponseToJSON(resp SearchResponse) (string, error) {
bytes, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return "", err
}
return string(bytes), nil
}