-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.cs
More file actions
98 lines (79 loc) · 3.76 KB
/
Copy pathDebug.cs
File metadata and controls
98 lines (79 loc) · 3.76 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
using System;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NETMetaCoder.SyntaxEnvelope;
namespace NETMetaCoder
{
/// <summary>
/// Provides utilities meant only as a debugging tool, during development.
/// </summary>
public static class Debug
{
private const char IndentChar = ' ';
private const int IndentLevel = 4;
/// <summary>
/// Pretty prints a <see cref="SyntaxEnvelope"/>.
/// </summary>
/// <param name="result"></param>
public static void Print(SyntaxEnvelope.SyntaxEnvelope result)
{
#if DEBUG
if (result == null)
{
Console.WriteLine($"null {nameof(SyntaxEnvelope.SyntaxEnvelope)}");
return;
}
Console.WriteLine($"{nameof(result.HasSyntaxToRender)} = {result.HasSyntaxToRender}");
Print(result.NamespaceSyntaxEnvelopes, -IndentLevel);
Print(result.ClassOrStructSyntaxEnvelopes, -IndentLevel);
Console.WriteLine("Indices: [" +
result.GatherNodeIndices().Select(index => index.ToString())
.Aggregate((acc, value) => $"{acc}, {value}") +
"]");
#endif
}
private static void Print(IImmutableList<NamespaceSyntaxEnvelope> namespaceSyntaxEnvelopes, int indent)
{
foreach (var namespaceSyntaxEnvelope in namespaceSyntaxEnvelopes)
{
Print(namespaceSyntaxEnvelope, indent + IndentLevel);
}
}
private static void Print(IImmutableList<ClassOrStructSyntaxEnvelope> classOrStructSyntaxEnvelopes, int indent)
{
foreach (var classOrStructSyntaxEnvelope in classOrStructSyntaxEnvelopes)
{
Print(classOrStructSyntaxEnvelope, indent + IndentLevel);
}
}
private static void Print(NamespaceSyntaxEnvelope namespaceSyntaxEnvelope, int indent)
{
var syntax = namespaceSyntaxEnvelope.NamespaceDeclarationSyntax ??
throw new ArgumentNullException(
$"{nameof(namespaceSyntaxEnvelope.NamespaceDeclarationSyntax)} must not be null.");
Console.WriteLine(
$"{new string(IndentChar, indent)}(N{namespaceSyntaxEnvelope.NodeIndex}) {syntax.Name.ToString()}");
Print(namespaceSyntaxEnvelope.NamespaceSyntaxEnvelopes, indent + IndentLevel);
Print(namespaceSyntaxEnvelope.ClassOrStructSyntaxEnvelopes, indent + IndentLevel);
}
private static void Print(ClassOrStructSyntaxEnvelope classOrStructSyntaxEnvelope, int indent)
{
var syntax = classOrStructSyntaxEnvelope.DeclarationSyntax ??
throw new ArgumentNullException(
$"{nameof(classOrStructSyntaxEnvelope.DeclarationSyntax)} must not be null.");
var prefix = classOrStructSyntaxEnvelope.IsClassDeclarationSyntax ? "C" : "S";
Console.WriteLine($"{new string(IndentChar, indent)}({prefix}{classOrStructSyntaxEnvelope.NodeIndex}) " +
$"{syntax.Identifier.ToString()}");
foreach (var methodSyntaxEnvelope in classOrStructSyntaxEnvelope.MethodSyntaxEnvelopes)
{
Print(methodSyntaxEnvelope.MethodDeclarationSyntax, methodSyntaxEnvelope.NodeIndex,
indent + IndentLevel);
}
Print(classOrStructSyntaxEnvelope.ClassOrStructSyntaxEnvelopes, indent);
}
private static void Print(MethodDeclarationSyntax methodDeclarationSyntax, ushort nodeIndex, int indent) =>
Console.WriteLine(
$"{new string(IndentChar, indent)}(M{nodeIndex}) {methodDeclarationSyntax.Identifier.ToString()}");
}
}