-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSharpTypeAnalyzer.cs
125 lines (107 loc) · 3.98 KB
/
CSharpTypeAnalyzer.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using CSharpMemberAnalyzerLibrary;
namespace CSharpTypeAnalyzerLibrary
{
public class TypeInfo
{
public string Name { get; set; }
public string Namespace { get; set; }
public string TypeKeyword { get; set; }
public string Text { get; set; }
public List<MemberInfo> Members { get; set; } = new List<MemberInfo>();
}
public class DependencyInfo
{
public string From { get; set; }
public string To { get; set; }
public string Type { get; set; }
}
public class CSharpTypeAnalyzer
{
private List<TypeInfo> _types;
public CSharpTypeAnalyzer()
{
_types = new List<TypeInfo>();
}
public void Reset()
{
_types.Clear();
}
public void AnalyzeFiles(IReadOnlyDictionary<string, string> fileContents)
{
foreach (var file in fileContents)
{
AnalyzeFile(file.Value);
}
}
private void AnalyzeFile(string content)
{
var tree = CSharpSyntaxTree.ParseText(content);
var root = tree.GetCompilationUnitRoot();
var typeDeclarations = root.DescendantNodes().OfType<TypeDeclarationSyntax>();
foreach (var typeDeclaration in typeDeclarations)
{
var typeInfo = new TypeInfo
{
Name = typeDeclaration.Identifier.Text,
Namespace = GetNamespace(typeDeclaration),
TypeKeyword = GetTypeKeyword(typeDeclaration),
Text = typeDeclaration.ToString()
};
_types.Add(typeInfo);
}
// Handle enum declarations separately
var enumDeclarations = root.DescendantNodes().OfType<EnumDeclarationSyntax>();
foreach (var enumDeclaration in enumDeclarations)
{
var typeInfo = new TypeInfo
{
Name = enumDeclaration.Identifier.Text,
Namespace = GetNamespace(enumDeclaration),
TypeKeyword = "enum",
Text = enumDeclaration.ToString()
};
_types.Add(typeInfo);
}
// Handle delegate declarations
var delegateDeclarations = root.DescendantNodes().OfType<DelegateDeclarationSyntax>();
foreach (var delegateDeclaration in delegateDeclarations)
{
var typeInfo = new TypeInfo
{
Name = delegateDeclaration.Identifier.Text,
Namespace = GetNamespace(delegateDeclaration),
TypeKeyword = "delegate",
Text = delegateDeclaration.ToString()
};
_types.Add(typeInfo);
}
}
private string GetNamespace(SyntaxNode node)
{
var namespaceDeclaration = node.Ancestors().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
return namespaceDeclaration?.Name.ToString() ?? "Global Namespace";
}
private string GetTypeKeyword(TypeDeclarationSyntax typeDeclaration)
{
if (typeDeclaration is ClassDeclarationSyntax) return "class";
if (typeDeclaration is InterfaceDeclarationSyntax) return "interface";
if (typeDeclaration is StructDeclarationSyntax) return "struct";
if (typeDeclaration is RecordDeclarationSyntax recordDeclaration)
{
return recordDeclaration.ClassOrStructKeyword.Text == "struct" ? "record struct" : "record";
}
return "unknown";
}
public IReadOnlyList<TypeInfo> GetTypes()
{
return _types.AsReadOnly();
}
}
}