-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeStructureWalker.cs
More file actions
315 lines (276 loc) · 12.4 KB
/
CodeStructureWalker.cs
File metadata and controls
315 lines (276 loc) · 12.4 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
309
310
311
312
313
314
315
// CodeStructureWalker.cs
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq; // For parsing XML comments
namespace RoslynCodeAnalyzer;
public class CodeStructureWalker : CSharpSyntaxWalker
{
// ... (Existing fields and constructor remain the same) ...
private readonly SemanticModel _semanticModel;
private readonly Dictionary<string, CodeNode> _nodes = new();
private readonly List<CodeEdge> _edges = new();
private readonly string _filePath;
private readonly Stack<string> _containerIdStack = new();
private static readonly SymbolDisplayFormat FullyQualifiedFormat = new(/* ... existing format options ... */);
public CodeStructureWalker(SemanticModel semanticModel, string filePath)
{
_semanticModel = semanticModel ?? throw new ArgumentNullException(nameof(semanticModel));
_filePath = filePath;
}
// --- Public Methods ---
public (Dictionary<string, CodeNode> Nodes, List<CodeEdge> Edges) GetResults()
{
return (_nodes, _edges);
}
private void AddNode(CodeNode node)
{
_nodes.TryAdd(node.Id, node);
if (_containerIdStack.TryPeek(out var containerId))
{
AddEdge(new CodeEdge(containerId, node.Id, "CONTAINS"));
}
}
private void AddEdge(CodeEdge edge)
{
_edges.Add(edge);
}
private LocationInfo GetLocation(SyntaxNode node)
{
var lineSpan = node.GetLocation().GetLineSpan();
return new LocationInfo(
_filePath,
lineSpan.StartLinePosition.Line + 1,
lineSpan.EndLinePosition.Line + 1
);
}
private string? GetSummaryComment(SyntaxNode node)
{
var xmlTrivia = node.GetLeadingTrivia()
.Select(i => i.GetStructure())
.OfType<DocumentationCommentTriviaSyntax>()
.FirstOrDefault();
if (xmlTrivia != null)
{
try
{
var xmlContent = xmlTrivia.Content.ToString();
var xmlDoc = XDocument.Parse("<root>" + xmlContent + "</root>", LoadOptions.PreserveWhitespace);
return xmlDoc.Descendants("summary").FirstOrDefault()?.Value.Trim();
}
catch (Exception ex)
{
Console.Error.WriteLine($"Warning: Failed to parse XML comment for node near line {GetLocation(node).StartLine}. Error: {ex.Message}");
return null;
}
}
return null;
}
// --- Visit Methods ---
public override void VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
{
var symbol = _semanticModel.GetDeclaredSymbol(node);
if (symbol != null)
{
string id = symbol.ToDisplayString(FullyQualifiedFormat);
var location = GetLocation(node);
// Typically don't need full code snippet for namespace declaration itself
var codeNode = new CodeNode(id, "Namespace", symbol.Name, location.FilePath, location.StartLine, location.EndLine);
AddNode(codeNode);
_containerIdStack.Push(id);
base.VisitNamespaceDeclaration(node);
_containerIdStack.Pop();
}
else
{
base.VisitNamespaceDeclaration(node);
}
}
public override void VisitFileScopedNamespaceDeclaration(FileScopedNamespaceDeclarationSyntax node)
{
var symbol = _semanticModel.GetDeclaredSymbol(node);
if (symbol != null)
{
string id = symbol.ToDisplayString(FullyQualifiedFormat);
var location = GetLocation(node);
// Typically don't need full code snippet for namespace declaration itself
var codeNode = new CodeNode(id, "Namespace", symbol.Name, location.FilePath, location.StartLine, location.EndLine);
AddNode(codeNode);
_containerIdStack.Push(id);
base.VisitFileScopedNamespaceDeclaration(node);
_containerIdStack.Pop();
}
else
{
base.VisitFileScopedNamespaceDeclaration(node);
}
}
public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
var symbol = _semanticModel.GetDeclaredSymbol(node);
if (symbol != null)
{
string id = symbol.ToDisplayString(FullyQualifiedFormat);
var location = GetLocation(node);
var comment = GetSummaryComment(node);
string codeSnippet = node.ToString();
// Properties don't have a separate "Signature" field in our model
var codeNode = new CodeNode(id, "Property", symbol.Name, location.FilePath, location.StartLine, location.EndLine, Comment: comment, CodeSnippet: codeSnippet);
AddNode(codeNode);
// Visit accessor methods (get/set) if needed for deeper analysis later
base.VisitPropertyDeclaration(node);
}
else
{
base.VisitPropertyDeclaration(node);
}
}
public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
{
// A single FieldDeclaration can declare multiple variables (e.g., public int x, y;)
// We need to create a node for each variable declarator.
var comment = GetSummaryComment(node); // Comment applies to the whole declaration
string fullSnippet = node.ToString(); // Snippet for the whole declaration line
foreach (VariableDeclaratorSyntax variableDeclarator in node.Declaration.Variables)
{
var symbol = _semanticModel.GetDeclaredSymbol(variableDeclarator);
if (symbol != null && symbol is IFieldSymbol fieldSymbol) // Ensure it's a field symbol
{
string id = fieldSymbol.ToDisplayString(FullyQualifiedFormat);
// Use the location of the specific variable declarator if possible,
// otherwise fallback to the whole declaration's location.
var location = GetLocation(variableDeclarator);
// Use the variable name, associate comment/snippet from parent declaration
var codeNode = new CodeNode(id, "Field", fieldSymbol.Name, location.FilePath, location.StartLine, location.EndLine, Comment: comment, CodeSnippet: fullSnippet);
AddNode(codeNode);
}
}
// Don't call base.VisitFieldDeclaration(node) unless you want to analyze initializers etc.
}
public override void VisitEnumDeclaration(EnumDeclarationSyntax node)
{
var symbol = _semanticModel.GetDeclaredSymbol(node);
if (symbol != null)
{
string id = symbol.ToDisplayString(FullyQualifiedFormat);
var location = GetLocation(node);
var comment = GetSummaryComment(node);
string codeSnippet = node.ToString();
var codeNode = new CodeNode(id, "Enum", symbol.Name, location.FilePath, location.StartLine, location.EndLine, Comment: comment, CodeSnippet: codeSnippet);
AddNode(codeNode);
_containerIdStack.Push(id); // Push Enum onto stack for containing members
base.VisitEnumDeclaration(node); // Visit Enum Members
_containerIdStack.Pop(); // Pop Enum from stack
}
else
{
base.VisitEnumDeclaration(node);
}
}
public override void VisitEnumMemberDeclaration(EnumMemberDeclarationSyntax node)
{
var symbol = _semanticModel.GetDeclaredSymbol(node);
if (symbol != null) // Enum members are field symbols
{
string id = symbol.ToDisplayString(FullyQualifiedFormat);
var location = GetLocation(node);
var comment = GetSummaryComment(node); // Get comment specific to the member
string codeSnippet = node.ToString();
var codeNode = new CodeNode(id, "EnumMember", symbol.Name, location.FilePath, location.StartLine, location.EndLine, Comment: comment, CodeSnippet: codeSnippet);
AddNode(codeNode);
// Don't call base.VisitEnumMemberDeclaration unless analyzing attributes etc.
}
else
{
base.VisitEnumMemberDeclaration(node);
}
}
public override void VisitClassDeclaration(ClassDeclarationSyntax node)
{
var symbol = _semanticModel.GetDeclaredSymbol(node);
if (symbol != null)
{
string id = symbol.ToDisplayString(FullyQualifiedFormat);
var location = GetLocation(node);
var comment = GetSummaryComment(node);
string codeSnippet = node.ToString(); // <<< GET THE CODE SNIPPET
// <<< PASS SNIPPET TO CodeNode CONSTRUCTOR
var codeNode = new CodeNode(id, "Class", symbol.Name, location.FilePath, location.StartLine, location.EndLine, Comment: comment, CodeSnippet: codeSnippet);
AddNode(codeNode);
// Handle Inheritance (remains the same)
if (symbol.BaseType != null && symbol.BaseType.SpecialType != SpecialType.System_Object)
{
string baseTypeId = symbol.BaseType.ToDisplayString(FullyQualifiedFormat);
AddEdge(new CodeEdge(id, baseTypeId, "INHERITS_FROM"));
}
foreach (var iface in symbol.Interfaces)
{
string interfaceId = iface.ToDisplayString(FullyQualifiedFormat);
AddEdge(new CodeEdge(id, interfaceId, "IMPLEMENTS"));
}
_containerIdStack.Push(id);
base.VisitClassDeclaration(node);
_containerIdStack.Pop();
}
else
{
base.VisitClassDeclaration(node);
}
}
public override void VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
{
var symbol = _semanticModel.GetDeclaredSymbol(node);
if (symbol != null)
{
string id = symbol.ToDisplayString(FullyQualifiedFormat);
var location = GetLocation(node);
var comment = GetSummaryComment(node);
string codeSnippet = node.ToString(); // <<< GET THE CODE SNIPPET
// <<< PASS SNIPPET TO CodeNode CONSTRUCTOR
var codeNode = new CodeNode(id, "Interface", symbol.Name, location.FilePath, location.StartLine, location.EndLine, Comment: comment, CodeSnippet: codeSnippet);
AddNode(codeNode);
// Handle Interface Inheritance (remains the same)
foreach (var iface in symbol.Interfaces)
{
string baseInterfaceId = iface.ToDisplayString(FullyQualifiedFormat);
AddEdge(new CodeEdge(id, baseInterfaceId, "INHERITS_FROM"));
}
_containerIdStack.Push(id);
base.VisitInterfaceDeclaration(node);
_containerIdStack.Pop();
}
else
{
base.VisitInterfaceDeclaration(node);
}
}
public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
{
var symbol = _semanticModel.GetDeclaredSymbol(node);
if (symbol != null)
{
string id = symbol.ToDisplayString(FullyQualifiedFormat);
var location = GetLocation(node);
var comment = GetSummaryComment(node);
var signature = symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat
.WithMemberOptions(SymbolDisplayMemberOptions.IncludeParameters)
.WithGenericsOptions(SymbolDisplayGenericsOptions.IncludeTypeParameters)
.WithParameterOptions(SymbolDisplayParameterOptions.IncludeType | SymbolDisplayParameterOptions.IncludeName | SymbolDisplayParameterOptions.IncludeParamsRefOut)
);
string codeSnippet = node.ToString(); // <<< GET THE CODE SNIPPET
// <<< PASS SNIPPET TO CodeNode CONSTRUCTOR
var codeNode = new CodeNode(id, "Method", symbol.Name, location.FilePath, location.StartLine, location.EndLine, Comment: comment, Signature: signature, CodeSnippet: codeSnippet);
AddNode(codeNode);
base.VisitMethodDeclaration(node); // Visit method body (e.g., for future call analysis)
}
else
{
base.VisitMethodDeclaration(node);
}
}
// TODO: Add VisitInvocationExpression for CALLS edges (more complex)
// TODO: Add Visit Struct, Enum, Property, Field etc. and include CodeSnippet as needed
}