-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-ast.py
More file actions
121 lines (105 loc) · 3.02 KB
/
debug-ast.py
File metadata and controls
121 lines (105 loc) · 3.02 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
#!/usr/bin/env python3
"""Debug AST structure to fix tests"""
import subprocess
import json
import os
def send_request(process, method, params=None):
request = {
"jsonrpc": "2.0",
"id": 1,
"method": method,
"params": params or {}
}
process.stdin.write(json.dumps(request) + '\n')
process.stdin.flush()
response_line = process.stdout.readline()
try:
response = json.loads(response_line)
if "error" in response and response["error"] is not None:
print(f"ERROR: {response['error']}")
return None
result = response.get("result", {})
if isinstance(result, dict) and "content" in result:
content = result["content"]
if content and len(content) > 0 and content[0].get("type") == "text":
try:
return json.loads(content[0]["text"])
except:
return content[0]["text"]
return result
except json.JSONDecodeError as e:
print(f"Failed to parse response: {e}")
return None
# Start server
process = subprocess.Popen(
["dotnet", "run", "--project", "src/Spelunk.Server/Spelunk.Server.csproj"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1
)
import time
time.sleep(2)
# Initialize
send_request(process, "initialize", {
"protocolVersion": "0.1.0",
"capabilities": {"tools": {}},
"clientInfo": {"name": "debug-ast", "version": "1.0.0"}
})
# Create test file
test_code = """namespace TestNamespace
{
public class TestClass
{
public void TestMethod()
{
var x = 1 + 2;
}
}
}"""
with open("test-ast-debug.cs", "w") as f:
f.write(test_code)
# Load workspace (use test file)
send_request(process, "tools/call", {
"name": "spelunk-load-workspace",
"arguments": {
"path": os.path.abspath("test-workspace/TestProject.csproj")
}
})
# Get AST at the position
print("\nGetting AST structure around '1' in '1 + 2'...")
result = send_request(process, "tools/call", {
"name": "spelunk-get-ast",
"arguments": {
"file": os.path.abspath("test-ast-debug.cs"),
"depth": 5
}
})
if result:
print("\nAST Structure:")
print("Result type:", type(result))
if isinstance(result, dict) and "ast" in result:
print(json.dumps(result["ast"], indent=2))
else:
print("Unexpected result:", result)
# Navigate from position
print("\nNavigating from position of '1'...")
result = send_request(process, "tools/call", {
"name": "spelunk-navigate",
"arguments": {
"from": {
"file": os.path.abspath("test-ast-debug.cs"),
"line": 7,
"column": 21 # Position of "1"
},
"path": "parent",
"returnPath": True
}
})
if result and "navigatedTo" in result:
print("\nParent of '1':")
print(json.dumps(result["navigatedTo"], indent=2))
# Clean up
process.terminate()
os.remove("test-ast-debug.cs")