Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
284 lines (256 loc) · 6.85 KB
/
Copy pathcontext.go
File metadata and controls
284 lines (256 loc) · 6.85 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
package dap
import (
"fmt"
"os"
"strings"
godap "github.com/google/go-dap"
)
const (
maxStackFrames = 20
sourceContext = 2 // lines before/after current line
maxStringLen = 200
maxCollectionItems = 5
)
// variableFilters maps adapter IDs to functions that decide if a variable should be skipped.
// Returns true if the variable should be filtered OUT.
var variableFilters = map[string]func(string) bool{
"debugpy": func(name string) bool {
// Skip debugpy's synthetic variable groups and dunder names
switch name {
case "special variables", "function variables", "class variables", "return values":
return true
}
return len(name) > 4 && name[:2] == "__" && name[len(name)-2:] == "__"
},
"pwa-node": func(name string) bool {
return name == "__proto__"
},
"lldb-dap": func(name string) bool {
return strings.HasPrefix(name, "[raw]")
},
// "go": nil — show all variables
}
// getFullContext aggregates stack, source, locals, and output into a ContextResult.
// It also updates d.frameID with the target frame for subsequent eval calls.
// contextLines overrides the default source context window; 0 means use default.
func getFullContext(d *Daemon, threadID, frameID, contextLines int) (*ContextResult, error) {
result := &ContextResult{}
// 1. Get stack trace
if err := d.client.StackTraceRequest(threadID, 0, maxStackFrames); err != nil {
return nil, fmt.Errorf("stack trace request: %w", err)
}
var frames []godap.StackFrame
for {
msg, err := d.readExpected()
if err != nil {
return nil, fmt.Errorf("reading stack trace: %w", err)
}
if resp, ok := msg.(*godap.StackTraceResponse); ok {
if !resp.Success {
return nil, fmt.Errorf("stack trace failed: %s", resp.Message)
}
frames = resp.Body.StackFrames
break
}
if errResp, ok := msg.(*godap.ErrorResponse); ok {
return nil, fmt.Errorf("stack trace error: %s", errorMessage(errResp))
}
}
// 2. Build stack and location from frames; record DAP frame IDs for eval
d.frameIDs = make([]int, len(frames))
for i, f := range frames {
d.frameIDs[i] = f.Id
sf := StackFrame{
Frame: i,
Function: f.Name,
}
if f.Source != nil {
sf.File = f.Source.Path
sf.Line = f.Line
}
result.Stack = append(result.Stack, sf)
}
if len(frames) > 0 {
top := frames[0]
result.Location = &Location{
Function: top.Name,
Line: top.Line,
}
if top.Source != nil {
result.Location.File = top.Source.Path
}
}
// 3. Read source file around current line
ctxLines := sourceContext
if contextLines > 0 {
ctxLines = contextLines
}
if result.Location != nil && result.Location.File != "" {
result.Source = readSourceLines(result.Location.File, result.Location.Line, ctxLines)
}
// 4. Get scopes and variables for target frame
targetFrameID := frameID
if targetFrameID == 0 && len(frames) > 0 {
targetFrameID = frames[0].Id
}
d.frameID = targetFrameID
if err := d.client.ScopesRequest(targetFrameID); err != nil {
return nil, fmt.Errorf("scopes request: %w", err)
}
var scopes []godap.Scope
for {
msg, err := d.readExpected()
if err != nil {
return nil, fmt.Errorf("reading scopes: %w", err)
}
if resp, ok := msg.(*godap.ScopesResponse); ok {
if !resp.Success {
break // non-fatal
}
scopes = resp.Body.Scopes
break
}
if _, ok := msg.(*godap.ErrorResponse); ok {
break // non-fatal
}
}
// 5. Get variables for each scope (only "Locals" and "Arguments")
var filterVar func(string) bool
if d.backend != nil {
filterVar = variableFilters[d.backend.AdapterID()]
}
for _, scope := range scopes {
name := strings.ToLower(scope.Name)
if scope.VariablesReference == 0 {
continue
}
// Only fetch locals-like scopes, skip globals/builtins for brevity
if !strings.Contains(name, "local") && !strings.Contains(name, "argument") &&
name != "locals" && name != "arguments" {
continue
}
if err := d.client.VariablesRequest(scope.VariablesReference); err != nil {
continue
}
for {
msg, err := d.readExpected()
if err != nil {
break
}
if _, ok := msg.(*godap.ErrorResponse); ok {
break
}
if resp, ok := msg.(*godap.VariablesResponse); ok {
if !resp.Success {
break
}
for _, v := range resp.Body.Variables {
if filterVar != nil && filterVar(v.Name) {
continue
}
result.Locals = append(result.Locals, Variable{
Name: v.Name,
Type: v.Type,
Value: truncateString(v.Value, maxStringLen),
})
}
break
}
}
}
// 6. Include buffered output (capped at maxOutputLines) and clear
d.mu.Lock()
result.Output = d.outputString()
d.mu.Unlock()
return result, nil
}
// readSourceLines reads lines from a file around the target line.
func readSourceLines(file string, line, context int) []SourceLine {
data, err := os.ReadFile(file)
if err != nil {
return nil
}
lines := strings.Split(string(data), "\n")
start := line - context - 1
if start < 0 {
start = 0
}
end := line + context
if end > len(lines) {
end = len(lines)
}
var result []SourceLine
for i := start; i < end; i++ {
result = append(result, SourceLine{
Line: i + 1,
Text: lines[i],
Current: i+1 == line,
})
}
return result
}
// truncateString truncates a string to maxLen runes.
func truncateString(s string, maxLen int) string {
runes := []rune(s)
if len(runes) <= maxLen {
return s
}
return string(runes[:maxLen]) + "..."
}
// expandVariable recursively fetches child variables up to maxDepth.
// nodeCount tracks total nodes to prevent runaway expansion (capped at maxNodes).
func expandVariable(d *Daemon, varRef int, depth, maxDepth int, nodeCount *int, maxNodes int) []InspectResult {
if depth >= maxDepth || varRef == 0 || *nodeCount >= maxNodes {
return nil
}
if err := d.client.VariablesRequest(varRef); err != nil {
return nil
}
var filterVar func(string) bool
if d.backend != nil {
filterVar = variableFilters[d.backend.AdapterID()]
}
for {
msg, err := d.readExpected()
if err != nil {
return nil
}
if _, ok := msg.(*godap.ErrorResponse); ok {
return nil
}
resp, ok := msg.(*godap.VariablesResponse)
if !ok {
continue
}
if !resp.Success {
return nil
}
var results []InspectResult
for _, v := range resp.Body.Variables {
if *nodeCount >= maxNodes {
break
}
if filterVar != nil && filterVar(v.Name) {
continue
}
*nodeCount++
child := InspectResult{
Name: v.Name,
Type: v.Type,
Value: truncateString(v.Value, maxStringLen),
}
if v.VariablesReference > 0 {
child.Children = expandVariable(d, v.VariablesReference, depth+1, maxDepth, nodeCount, maxNodes)
}
results = append(results, child)
}
return results
}
}
// errorMessage extracts a human-readable message from an ErrorResponse.
func errorMessage(r *godap.ErrorResponse) string {
if r.Body.Error != nil && r.Body.Error.Format != "" {
return r.Body.Error.Format
}
return r.Message
}