MapTo/test/MapTo.Tests/Extensions/RoslynExtensions.cs

42 lines
1.6 KiB
C#
Raw Permalink Normal View History

2021-12-24 20:06:06 +03:00
using System;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
namespace MapTo.Tests.Extensions
{
internal static class RoslynExtensions
{
internal static SyntaxTree? GetGeneratedSyntaxTree(this Compilation compilation, string className) =>
compilation.SyntaxTrees.SingleOrDefault(s => s.FilePath.EndsWith($"{className}.g.cs"));
internal static string PrintSyntaxTree(this Compilation compilation)
{
var builder = new StringBuilder();
return string.Join(
Environment.NewLine,
compilation.SyntaxTrees
.Reverse()
.Select((s, i) =>
{
builder
.Clear()
.AppendLine("----------------------------------------")
.AppendFormat("File Path: \"{0}\"", s.FilePath).AppendLine()
.AppendFormat("Index: \"{0}\"", i).AppendLine()
.AppendLine();
var lines = s.ToString().Split(Environment.NewLine);
var lineNumber = 0;
foreach (var line in lines)
{
builder.AppendFormat("{0:00}: {1}", lineNumber, line).AppendLine();
lineNumber++;
}
return builder.ToString();
}));
}
}
2021-02-07 13:46:12 +03:00
}