-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.cs
85 lines (69 loc) · 2.35 KB
/
Utilities.cs
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
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace NetJsonAOT.Generators;
public static class Utilities
{
public static readonly Type[] NonInterfaceDeclarationTypes =
{
typeof(ClassDeclarationSyntax),
typeof(StructDeclarationSyntax),
typeof(RecordDeclarationSyntax)
};
public static bool TryGetNamespace(ISymbol symbol, List<INamespaceSymbol> namespaces, StringBuilder sb,
[NotNullWhen(true)] out string? namespaceString)
{
var nsp = symbol.ContainingNamespace;
if (nsp == null || nsp.IsGlobalNamespace)
{
Console.Error.WriteLine(
$"Namespace not found for {symbol.Name}");
namespaceString = null;
return false;
}
var nspString = nsp.ToString();
while (true)
{
namespaces.Add(nsp);
var newNsp = nsp.ContainingNamespace;
if (newNsp == null)
break;
if (newNsp.IsGlobalNamespace)
break;
var newNspString = newNsp.ToString();
if (nspString == newNspString)
break;
nsp = newNsp;
nspString = newNspString;
}
for (int i = namespaces.Count - 1; i >= 0; i--)
{
sb.Append(namespaces[i]).Append('.');
namespaces.RemoveAt(i);
}
namespaceString = sb.ToString(0, sb.Length - 1);
sb.Clear();
return true;
}
public static SyntaxKind GetScope(TypeDeclarationSyntax syntax)
{
// get private/public/protected/internal
var modifiers = syntax.Modifiers;
foreach (var modifier in modifiers)
{
var kind = modifier.Kind() switch
{
SyntaxKind.PrivateKeyword => SyntaxKind.InternalKeyword,
SyntaxKind.ProtectedKeyword => SyntaxKind.ProtectedKeyword,
SyntaxKind.PublicKeyword => SyntaxKind.PublicKeyword,
SyntaxKind.InternalKeyword => SyntaxKind.InternalKeyword,
_ => SyntaxKind.None
};
if (kind != SyntaxKind.None)
return kind;
}
return SyntaxKind.InternalKeyword;
}
}