forked from dotnet/docfx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolFormatter.cs
More file actions
200 lines (172 loc) · 8.35 KB
/
Copy pathSymbolFormatter.cs
File metadata and controls
200 lines (172 loc) · 8.35 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System.Collections.Immutable;
using Docfx.DataContracts.ManagedReference;
using Microsoft.CodeAnalysis;
using CS = Microsoft.CodeAnalysis.CSharp;
using VB = Microsoft.CodeAnalysis.VisualBasic;
namespace Docfx.Dotnet;
internal static partial class SymbolFormatter
{
private static readonly SymbolDisplayFormat s_nameFormat = new(
memberOptions:
SymbolDisplayMemberOptions.IncludeParameters |
SymbolDisplayMemberOptions.IncludeExplicitInterface,
genericsOptions:
SymbolDisplayGenericsOptions.IncludeTypeParameters,
parameterOptions:
SymbolDisplayParameterOptions.IncludeType,
miscellaneousOptions:
SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier |
SymbolDisplayMiscellaneousOptions.AllowDefaultLiteral |
(ExtractMetadataConfig.UseClrTypeNames
? SymbolDisplayMiscellaneousOptions.None
: SymbolDisplayMiscellaneousOptions.UseSpecialTypes),
extensionMethodStyle: SymbolDisplayExtensionMethodStyle.StaticMethod);
private static readonly SymbolDisplayFormat s_nameWithTypeFormat = s_nameFormat
.AddMemberOptions(SymbolDisplayMemberOptions.IncludeContainingType)
.WithTypeQualificationStyle(SymbolDisplayTypeQualificationStyle.NameAndContainingTypes);
private static readonly SymbolDisplayFormat s_qualifiedNameFormat = s_nameWithTypeFormat
.WithTypeQualificationStyle(SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces);
private static readonly SymbolDisplayFormat s_namespaceFormat = new(
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces);
private static readonly SymbolDisplayFormat s_methodNameFormat = s_nameFormat
.WithParameterOptions(SymbolDisplayParameterOptions.IncludeType | SymbolDisplayParameterOptions.IncludeParamsRefOut);
private static readonly SymbolDisplayFormat s_methodNameWithTypeFormat = s_nameWithTypeFormat
.WithParameterOptions(SymbolDisplayParameterOptions.IncludeType | SymbolDisplayParameterOptions.IncludeParamsRefOut);
private static readonly SymbolDisplayFormat s_methodQualifiedNameFormat = s_qualifiedNameFormat
.WithParameterOptions(SymbolDisplayParameterOptions.IncludeType | SymbolDisplayParameterOptions.IncludeParamsRefOut);
public static string GetName(ISymbol symbol, SyntaxLanguage language, bool nullableReferenceType = true, bool overload = false)
{
return GetNameParts(symbol, language, nullableReferenceType, overload).ToDisplayString();
}
public static ImmutableArray<SymbolDisplayPart> GetNameParts(
ISymbol symbol, SyntaxLanguage language, bool nullableReferenceType = true, bool overload = false)
{
return GetDisplayParts(symbol, language, nullableReferenceType, overload, symbol.Kind switch
{
SymbolKind.NamedType => s_nameWithTypeFormat,
SymbolKind.Namespace => s_namespaceFormat,
SymbolKind.Method => s_methodNameFormat,
_ => s_nameFormat,
});
}
public static string GetNameWithType(ISymbol symbol, SyntaxLanguage language)
{
return GetNameWithTypeParts(symbol, language).ToDisplayString();
}
public static ImmutableArray<SymbolDisplayPart> GetNameWithTypeParts(
ISymbol symbol, SyntaxLanguage language, bool nullableReferenceType = true, bool overload = false)
{
return GetDisplayParts(symbol, language, nullableReferenceType, overload, symbol.Kind switch
{
SymbolKind.Namespace => s_namespaceFormat,
SymbolKind.Method => s_methodNameWithTypeFormat,
_ => s_nameWithTypeFormat,
});
}
public static string GetQualifiedName(ISymbol symbol, SyntaxLanguage language)
{
return GetQualifiedNameParts(symbol, language).ToDisplayString();
}
public static ImmutableArray<SymbolDisplayPart> GetQualifiedNameParts(
ISymbol symbol, SyntaxLanguage language, bool nullableReferenceType = true, bool overload = false)
{
return GetDisplayParts(symbol, language, nullableReferenceType, overload, symbol.Kind switch
{
SymbolKind.Namespace => s_namespaceFormat,
SymbolKind.Method => s_methodQualifiedNameFormat,
_ => s_qualifiedNameFormat,
});
}
public static string GetSyntax(ISymbol symbol, SyntaxLanguage language, SymbolFilter filter)
{
return GetSyntaxParts(symbol, language, filter).ToDisplayString();
}
public static ImmutableArray<SymbolDisplayPart> GetSyntaxParts(ISymbol symbol, SyntaxLanguage language, SymbolFilter filter)
{
try
{
return new SyntaxFormatter { Language = language, Filter = filter }.GetSyntax(symbol);
}
catch (InvalidOperationException)
{
return ImmutableArray<SymbolDisplayPart>.Empty;
}
}
public static List<LinkItem> ToLinkItems(this ImmutableArray<SymbolDisplayPart> parts,
Compilation compilation, MemberLayout memberLayout, HashSet<IAssemblySymbol> allAssemblies, bool overload, SymbolUrlKind urlKind = SymbolUrlKind.Html)
{
var result = new List<LinkItem>();
foreach (var part in parts)
{
result.Add(ToLinkItem(part));
if (overload && part.Kind is SymbolDisplayPartKind.MethodName)
break;
}
return result;
LinkItem ToLinkItem(SymbolDisplayPart part)
{
var symbol = part.Symbol;
if (symbol is null || part.Kind is SymbolDisplayPartKind.TypeParameterName)
return new() { DisplayName = part.ToString() };
if (symbol is INamedTypeSymbol type && type.IsGenericType)
symbol = type.ConstructedFrom;
return new()
{
Name = overload ? VisitorHelper.GetOverloadId(symbol) : VisitorHelper.GetId(symbol),
DisplayName = part.ToString(),
Href = SymbolUrlResolver.GetSymbolUrl(symbol, compilation, memberLayout, urlKind, allAssemblies),
IsExternalPath = symbol.IsExtern || symbol.DeclaringSyntaxReferences.Length == 0,
};
}
}
private static ImmutableArray<SymbolDisplayPart> GetDisplayParts(
ISymbol symbol, SyntaxLanguage language, bool nullableReferenceType, bool overload, SymbolDisplayFormat format)
{
if (!nullableReferenceType)
format = format.RemoveMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier);
if (overload)
format = format.RemoveMemberOptions(SymbolDisplayMemberOptions.IncludeParameters);
try
{
var result = language switch
{
SyntaxLanguage.VB => VB.SymbolDisplay.ToDisplayParts(symbol, format),
_ => CS.SymbolDisplay.ToDisplayParts(symbol, format),
};
if (overload && language is SyntaxLanguage.CSharp && symbol.IsCastOperator())
return GetCastOperatorOverloadDisplayParts(result);
return result;
}
catch
{
return ImmutableArray<SymbolDisplayPart>.Empty;
}
static ImmutableArray<SymbolDisplayPart> GetCastOperatorOverloadDisplayParts(ImmutableArray<SymbolDisplayPart> parts)
{
// Convert from "explicit operator Bar" to "explicit operator", for lack of disabling return type in SymbolDisplay.
var endIndex = parts.Length;
while (--endIndex >= 0)
{
var part = parts[endIndex];
if (part.Kind is SymbolDisplayPartKind.Keyword && part.ToString() is "operator" or "checked")
break;
}
return parts.Take(endIndex + 1).ToImmutableArray();
}
}
private static SymbolDisplayFormat WithTypeQualificationStyle(this SymbolDisplayFormat format, SymbolDisplayTypeQualificationStyle style)
{
return new(
format.GlobalNamespaceStyle,
style,
format.GenericsOptions,
format.MemberOptions,
format.DelegateStyle,
format.ExtensionMethodStyle,
format.ParameterOptions,
format.PropertyStyle,
format.LocalOptions,
format.KindOptions,
format.MiscellaneousOptions);
}
}