2021-02-07 13:46:12 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
|
|
|
|
|
|
namespace MapTo.Tests.Extensions
|
|
|
|
|
{
|
|
|
|
|
internal static class RoslynExtensions
|
|
|
|
|
{
|
2021-06-29 09:14:00 +03:00
|
|
|
|
internal static SyntaxTree? GetGeneratedSyntaxTree(this Compilation compilation, string className) =>
|
2021-04-17 10:14:39 +03:00
|
|
|
|
compilation.SyntaxTrees.SingleOrDefault(s => s.FilePath.EndsWith($"{className}.g.cs"));
|
|
|
|
|
|
2021-02-07 13:46:12 +03:00
|
|
|
|
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);
|
2021-07-02 11:24:46 +03:00
|
|
|
|
var lineNumber = 0;
|
2021-02-07 13:46:12 +03:00
|
|
|
|
foreach (var line in lines)
|
|
|
|
|
{
|
|
|
|
|
builder.AppendFormat("{0:00}: {1}", lineNumber, line).AppendLine();
|
|
|
|
|
lineNumber++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return builder.ToString();
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|