Repository navigation
Expand file tree
/
Copy pathWorkspaceCodeAnalysisService.cs
More file actions
130 lines (115 loc) · 5.25 KB
/
Copy pathWorkspaceCodeAnalysisService.cs
File metadata and controls
130 lines (115 loc) · 5.25 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
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace CodingSahayi;
public static class WorkspaceCodeAnalysisService
{
public static string AnalyzeStructure(string filePath)
{
if (!File.Exists(filePath)) return $"Error: File not found {filePath}";
string extension = Path.GetExtension(filePath).ToLowerInvariant();
var text = File.ReadAllText(filePath);
if (extension == ".cs")
{
var tree = CSharpSyntaxTree.ParseText(text);
var root = tree.GetRoot();
var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();
var sb = new System.Text.StringBuilder();
foreach (var c in classes)
{
var lineSpan = c.SyntaxTree.GetLineSpan(c.Span);
sb.AppendLine($"Class: {c.Identifier.Text} (Line: {lineSpan.StartLinePosition.Line + 1})");
var methods = c.DescendantNodes().OfType<MethodDeclarationSyntax>();
foreach (var m in methods)
{
var mSpan = m.SyntaxTree.GetLineSpan(m.Span);
sb.AppendLine($" - Method: {m.Identifier.Text} (Line: {mSpan.StartLinePosition.Line + 1})");
}
}
return sb.Length > 0 ? sb.ToString() : "No classes/methods found.";
}
else
{
// Generic regex fallback for functions/methods/classes
var sb = new System.Text.StringBuilder();
var lines = text.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i];
if (Regex.IsMatch(line, @"\b(class|def|function)\b\s+\w+"))
{
sb.AppendLine($"Structure Match: {line.Trim()} (Line: {i + 1})");
}
}
return sb.Length > 0 ? sb.ToString() : $"No recognized structural patterns found via generic fallback for {extension}.";
}
}
public static string VerifySyntax(string filePath)
{
if (!File.Exists(filePath)) return $"Error: File not found {filePath}";
string extension = Path.GetExtension(filePath).ToLowerInvariant();
var text = File.ReadAllText(filePath);
if (extension == ".cs")
{
var tree = CSharpSyntaxTree.ParseText(text);
var diagnostics = tree.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error).ToList();
if (diagnostics.Count == 0) return "Syntax OK";
var sb = new System.Text.StringBuilder();
foreach (var diag in diagnostics)
{
var lineSpan = diag.Location.GetLineSpan();
sb.AppendLine($"Line {lineSpan.StartLinePosition.Line + 1}, Char {lineSpan.StartLinePosition.Character + 1}: {diag.GetMessage()}");
}
return sb.ToString();
}
return $"Syntax verification is not natively supported for {extension}. (Syntax OK - Fallback)";
}
public static string ResolveSymbol(string filePath, string symbolName)
{
if (!File.Exists(filePath)) return $"Error: File not found {filePath}";
string extension = Path.GetExtension(filePath).ToLowerInvariant();
var text = File.ReadAllText(filePath);
if (extension == ".cs")
{
var tree = CSharpSyntaxTree.ParseText(text);
var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
var compilation = CSharpCompilation.Create("AnalysisComp")
.AddReferences(mscorlib)
.AddSyntaxTrees(tree);
var semanticModel = compilation.GetSemanticModel(tree);
var root = tree.GetRoot();
var nodes = root.DescendantNodes().OfType<SyntaxNode>()
.Where(n => n.ToString() == symbolName);
var sb = new System.Text.StringBuilder();
foreach (var node in nodes)
{
var symbolInfo = semanticModel.GetSymbolInfo(node);
var symbol = symbolInfo.Symbol ?? semanticModel.GetDeclaredSymbol(node);
if (symbol != null)
{
sb.AppendLine($"Symbol: {symbol.Name}");
sb.AppendLine($"Kind: {symbol.Kind}");
sb.AppendLine($"Accessibility: {symbol.DeclaredAccessibility}");
sb.AppendLine($"Type/Signature: {symbol.ToDisplayString()}");
sb.AppendLine("---");
}
}
return sb.Length > 0 ? sb.ToString() : $"Symbol '{symbolName}' not found or could not be resolved.";
}
// Generic fallback search
var matches = new System.Text.StringBuilder();
var lines = text.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains(symbolName))
{
matches.AppendLine($"Found '{symbolName}' at Line {i + 1}: {lines[i].Trim()}");
}
}
return matches.Length > 0 ? matches.ToString() : $"Symbol '{symbolName}' not found in {extension} file fallback search.";
}
}