add roslyn

This commit is contained in:
Matthew Parker [SSW]
2025-01-10 21:38:19 +10:00
parent 06991c51ec
commit ced3a70988
4 changed files with 50 additions and 4 deletions

View File

@@ -0,0 +1,40 @@
using System.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.MSBuild;
namespace SharpIDE.Application.Features.SolutionDiscovery;
public static class RoslynTest
{
public static async Task Analyse(string solutionFilePath)
{
var workspace = MSBuildWorkspace.Create();
var timer = Stopwatch.StartNew();
var solution = await workspace.OpenSolutionAsync(solutionFilePath);
timer.Stop();
Console.WriteLine($"Solution loaded in {timer.ElapsedMilliseconds}ms");
Console.WriteLine();
foreach (var project in solution.Projects)
{
Console.WriteLine($"Project: {project.Name}");
foreach (var document in project.Documents)
{
Console.WriteLine($"Document: {document.Name}");
var syntaxTree = await document.GetSyntaxTreeAsync();
var root = await syntaxTree!.GetRootAsync();
var classifiedSpans = await Classifier.GetClassifiedSpansAsync(document, root.FullSpan);
foreach (var span in classifiedSpans)
{
var classifiedSpan = root.GetText().GetSubText(span.TextSpan);
Console.WriteLine($"{span.TextSpan}: {span.ClassificationType}");
Console.WriteLine(classifiedSpan);
}
}
}
}
}

View File

@@ -9,6 +9,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Build" Version="17.12.6" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.7.8" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.12.0" />
<PackageReference Include="NuGet.Protocol" Version="6.12.1" />
</ItemGroup>
</Project>