-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbol_search.go
More file actions
283 lines (258 loc) · 8.66 KB
/
Copy pathsymbol_search.go
File metadata and controls
283 lines (258 loc) · 8.66 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
package sight
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
// SymbolResult is a symbol found in a source file via AST-level search.
// It provides the precise source location and body text needed for
// targeted context retrieval (AutoCodeRover pattern, ISSTA 2024).
type SymbolResult struct {
File string `json:"file"`
Symbol string `json:"symbol"`
Kind string `json:"kind"` // function, method, type, class, const, var
StartLine int `json:"start_line"`
EndLine int `json:"end_line"`
Body string `json:"body,omitempty"`
}
// SearchBySymbol returns all symbols in filePath whose name contains
// nameFragment (case-insensitive substring match). An empty nameFragment
// returns all symbols. Passing a non-empty kind filters by symbol kind
// ("function", "method", "type", etc.); an empty kind matches all.
//
// This replaces flat file loading with precise symbol-level retrieval.
// AutoCodeRover (arXiv 2404.05427) demonstrated that AST-backed symbol
// search achieves 19% SWE-bench Lite — higher than whole-file retrieval —
// by reducing context noise from irrelevant code paths.
func SearchBySymbol(filePath, nameFragment, kind string) ([]SymbolResult, error) {
ext := filepath.Ext(filePath)
patterns := symbolPatternsForExt(ext)
if len(patterns) == 0 {
return nil, nil
}
lines, err := readLines(filePath)
if err != nil {
return nil, fmt.Errorf("sight.SearchBySymbol: %w", err)
}
fragLower := strings.ToLower(nameFragment)
kindLower := strings.ToLower(kind)
var results []SymbolResult
for i, line := range lines {
trimmed := strings.TrimSpace(line)
for _, pat := range patterns {
m := pat.re.FindStringSubmatch(trimmed)
if m == nil {
continue
}
sym := extractSymbolName(m, pat.kind, ext)
if fragLower != "" && !strings.Contains(strings.ToLower(sym), fragLower) {
break
}
if kindLower != "" && pat.kind != kindLower {
break
}
startLine := i + 1
endLine := estimateEndLine(lines, i)
results = append(results, SymbolResult{
File: filePath,
Symbol: sym,
Kind: pat.kind,
StartLine: startLine,
EndLine: endLine,
})
break
}
}
return results, nil
}
// GetSymbolBody returns the source text of the first symbol in filePath
// whose name matches fqn (case-insensitive). If no match is found,
// it returns an error. The Body field of the result is populated with
// the extracted source.
//
// GetSymbolBody is the primary API for precise code context retrieval:
// instead of loading a whole file, hawk passes only the relevant function
// or type body to the LLM, dramatically reducing context window pressure.
func GetSymbolBody(filePath, fqn string) (SymbolResult, error) {
results, err := SearchBySymbol(filePath, fqn, "")
if err != nil {
return SymbolResult{}, err
}
lines, err := readLines(filePath)
if err != nil {
return SymbolResult{}, fmt.Errorf("sight.GetSymbolBody: %w", err)
}
fqnLower := strings.ToLower(fqn)
for _, r := range results {
if strings.EqualFold(r.Symbol, fqnLower) || strings.HasSuffix(strings.ToLower(r.Symbol), "."+fqnLower) {
r.Body = extractBody(lines, r.StartLine-1, r.EndLine-1)
return r, nil
}
}
// Fall back to partial match
for _, r := range results {
if strings.Contains(strings.ToLower(r.Symbol), fqnLower) {
r.Body = extractBody(lines, r.StartLine-1, r.EndLine-1)
return r, nil
}
}
return SymbolResult{}, fmt.Errorf("sight.GetSymbolBody: symbol %q not found in %s", fqn, filePath)
}
// extractBody extracts lines [start, end] (0-based, inclusive) from lines.
func extractBody(lines []string, start, end int) string {
if start < 0 {
start = 0
}
if end >= len(lines) {
end = len(lines) - 1
}
if start > end {
return ""
}
return strings.Join(lines[start:end+1], "\n")
}
// estimateEndLine estimates the end line of a symbol starting at lineIdx.
// Uses brace depth for C-style languages, indent for Python.
func estimateEndLine(lines []string, startIdx int) int {
if startIdx >= len(lines) {
return startIdx + 1
}
startLine := lines[startIdx]
isPython := !strings.Contains(startLine, "{") && strings.HasSuffix(strings.TrimSpace(startLine), ":")
if isPython {
baseIndent := leadingSpaces(startLine)
for i := startIdx + 1; i < len(lines); i++ {
line := lines[i]
if strings.TrimSpace(line) == "" {
continue
}
if leadingSpaces(line) <= baseIndent {
return i
}
}
return len(lines)
}
depth := strings.Count(startLine, "{") - strings.Count(startLine, "}")
if depth <= 0 && !strings.Contains(startLine, "{") {
// Single-line or no body; look ahead for the opening brace
for i := startIdx + 1; i < len(lines) && i < startIdx+5; i++ {
depth += strings.Count(lines[i], "{") - strings.Count(lines[i], "}")
if depth > 0 {
startIdx = i
break
}
}
}
for i := startIdx + 1; i < len(lines); i++ {
depth += strings.Count(lines[i], "{") - strings.Count(lines[i], "}")
if depth <= 0 {
return i + 1
}
}
return len(lines)
}
func leadingSpaces(s string) int {
n := 0
for _, c := range s {
switch c {
case ' ':
n++
case '\t':
n += 4
default:
return n
}
}
return n
}
// ── Pattern tables ────────────────────────────────────────────────────────────
type symPattern struct {
re *regexp.Regexp
kind string
}
func symbolPatternsForExt(ext string) []symPattern {
switch strings.ToLower(ext) {
case ".go":
return goSymPatterns
case ".py":
return pySymPatterns
case ".ts", ".tsx":
return tsSymPatterns
case ".js", ".jsx":
return jsSymPatterns
case ".rs":
return rsSymPatterns
case ".java":
return javaSymPatterns
}
return nil
}
var goSymPatterns = []symPattern{
{regexp.MustCompile(`^func\s+\(\s*\w+\s+\*?(\w+)\)\s+(\w+)\s*\(`), "method"},
{regexp.MustCompile(`^func\s+(\w+)\s*\(`), "function"},
{regexp.MustCompile(`^type\s+(\w+)\s+struct\b`), "type"},
{regexp.MustCompile(`^type\s+(\w+)\s+interface\b`), "type"},
{regexp.MustCompile(`^type\s+(\w+)\s+`), "type"},
{regexp.MustCompile(`^var\s+(\w+)\s`), "var"},
{regexp.MustCompile(`^const\s+(\w+)\s`), "const"},
}
var pySymPatterns = []symPattern{
{regexp.MustCompile(`^class\s+(\w+)`), "class"},
{regexp.MustCompile(`^async\s+def\s+(\w+)`), "function"},
{regexp.MustCompile(`^\s{4}async\s+def\s+(\w+)`), "method"},
{regexp.MustCompile(`^def\s+(\w+)`), "function"},
{regexp.MustCompile(`^\s{4}def\s+(\w+)`), "method"},
}
var tsSymPatterns = []symPattern{
{regexp.MustCompile(`^(?:export\s+)?class\s+(\w+)`), "class"},
{regexp.MustCompile(`^(?:export\s+)?interface\s+(\w+)`), "type"},
{regexp.MustCompile(`^(?:export\s+)?type\s+(\w+)\s*=`), "type"},
{regexp.MustCompile(`^(?:export\s+)?(?:async\s+)?function\s+(\w+)`), "function"},
{regexp.MustCompile(`^(?:export\s+)?const\s+(\w+)\s*=\s*(?:async\s+)?\(`), "function"},
{regexp.MustCompile(`^(?:export\s+)?const\s+(\w+)`), "const"},
}
var jsSymPatterns = []symPattern{
{regexp.MustCompile(`^(?:export\s+)?class\s+(\w+)`), "class"},
{regexp.MustCompile(`^(?:export\s+)?(?:async\s+)?function\s+(\w+)`), "function"},
{regexp.MustCompile(`^(?:export\s+)?const\s+(\w+)\s*=\s*(?:async\s+)?\(`), "function"},
{regexp.MustCompile(`^(?:export\s+)?const\s+(\w+)`), "const"},
}
var rsSymPatterns = []symPattern{
{regexp.MustCompile(`^(?:pub(?:\([^)]*\))?\s+)?fn\s+(\w+)`), "function"},
{regexp.MustCompile(`^(?:pub(?:\([^)]*\))?\s+)?struct\s+(\w+)`), "type"},
{regexp.MustCompile(`^(?:pub(?:\([^)]*\))?\s+)?enum\s+(\w+)`), "type"},
{regexp.MustCompile(`^(?:pub(?:\([^)]*\))?\s+)?trait\s+(\w+)`), "type"},
{regexp.MustCompile(`^impl(?:<[^>]*>)?\s+(\w+)`), "type"},
}
var javaSymPatterns = []symPattern{
{regexp.MustCompile(`^(?:public|private|protected)?\s*(?:static\s+)?class\s+(\w+)`), "class"},
{regexp.MustCompile(`^(?:public|private|protected)?\s*interface\s+(\w+)`), "type"},
{regexp.MustCompile(`^\s+(?:public|private|protected)?\s*(?:static\s+)?(?:\w+(?:<[^>]*>)?)\s+(\w+)\s*\(`), "method"},
}
func extractSymbolName(m []string, kind, ext string) string {
if kind == "method" && strings.EqualFold(ext, ".go") && len(m) >= 3 {
return m[1] + "." + m[2]
}
if len(m) >= 2 {
return m[1]
}
return ""
}
// readLines reads a file and returns its lines.
func readLines(path string) ([]string, error) {
f, err := os.Open(path) // #nosec G304 -- path is the source file under analysis, supplied by the review tool's own file walker/CLI target; reading arbitrary project files is this tool's purpose
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
var lines []string
sc := bufio.NewScanner(f)
sc.Buffer(make([]byte, 0, 256*1024), 1024*1024)
for sc.Scan() {
lines = append(lines, sc.Text())
}
return lines, sc.Err()
}