-
Notifications
You must be signed in to change notification settings - Fork 868
/
DotnetApiCatalog.Toc.cs
259 lines (222 loc) · 9.65 KB
/
DotnetApiCatalog.Toc.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Docfx.Common;
using Docfx.DataContracts.ManagedReference;
using Microsoft.CodeAnalysis;
#nullable enable
namespace Docfx.Dotnet;
partial class DotnetApiCatalog
{
enum TocNodeType
{
None,
Namespace,
Class,
Struct,
Interface,
Enum,
Delegate,
Constructor,
Field,
Property,
Method,
Event,
Operator,
}
class TocNode
{
public string name { get; init; } = "";
public string? href { get; init; }
public List<TocNode>? items { get; set; }
internal TocNodeType type;
internal string? id;
internal bool containsLeafNodes;
internal List<(ISymbol symbol, Compilation compilation)> symbols = new();
}
private static List<TocNode> CreateToc(List<(IAssemblySymbol symbol, Compilation compilation)> assemblies, ExtractMetadataConfig config, DotnetApiOptions options)
{
Directory.CreateDirectory(config.OutputFolder);
var filter = new SymbolFilter(config, options);
var tocNodes = new Dictionary<string, TocNode>();
var ext = config.OutputFormat is MetadataOutputFormat.Markdown ? ".md" : ".yml";
var toc = assemblies.SelectMany(a => CreateToc(a.symbol.GlobalNamespace, a.compilation)).ToList();
SortToc(toc, root: true);
YamlUtility.Serialize(Path.Combine(config.OutputFolder, "toc.yml"), toc, YamlMime.TableOfContent);
return toc;
IEnumerable<TocNode> CreateToc(ISymbol symbol, Compilation compilation)
{
if (!filter.IncludeApi(symbol))
yield break;
switch (symbol)
{
case INamespaceSymbol ns when ns.IsGlobalNamespace:
foreach (var child in ns.GetNamespaceMembers())
foreach (var item in CreateToc(child, compilation))
yield return item;
break;
case INamespaceSymbol ns:
foreach (var item in CreateNamespaceToc(ns))
yield return item;
break;
case INamedTypeSymbol type:
foreach (var item in CreateNamedTypeToc(type))
yield return item;
break;
case IFieldSymbol or IPropertySymbol or IMethodSymbol or IEventSymbol:
foreach (var item in CreateMemberToc(symbol))
yield return item;
break;
default:
throw new NotSupportedException($"Unknown symbol {symbol}");
}
IEnumerable<TocNode> CreateNamespaceToc(INamespaceSymbol ns)
{
var idExists = true;
var id = VisitorHelper.PathFriendlyId(VisitorHelper.GetId(symbol));
if (!tocNodes.TryGetValue(id, out var node))
{
idExists = false;
tocNodes.Add(id, node = new()
{
id = id,
name = config.NamespaceLayout is NamespaceLayout.Nested ? symbol.Name : symbol.ToString() ?? "",
href = $"{id}{ext}",
type = TocNodeType.Namespace,
});
}
var existingNodeHasNoLeafNode = idExists && !node.containsLeafNodes;
node.items ??= new();
node.symbols.Add((symbol, compilation));
foreach (var child in ns.GetNamespaceMembers())
{
if (config.NamespaceLayout is NamespaceLayout.Flattened)
foreach (var item in CreateToc(child, compilation))
yield return item;
else if (config.NamespaceLayout is NamespaceLayout.Nested)
node.items.AddRange(CreateToc(child, compilation));
}
foreach (var child in ns.GetTypeMembers())
{
node.items.AddRange(CreateToc(child, compilation));
}
node.containsLeafNodes = node.items.Any(i => i.containsLeafNodes);
if (node.containsLeafNodes)
{
if (!idExists || existingNodeHasNoLeafNode)
{
yield return node;
}
}
}
IEnumerable<TocNode> CreateNamedTypeToc(INamedTypeSymbol type)
{
var idExists = true;
var id = VisitorHelper.PathFriendlyId(VisitorHelper.GetId(symbol));
if (!tocNodes.TryGetValue(id, out var node))
{
idExists = false;
tocNodes.Add(id, node = new()
{
id = id,
name = SymbolFormatter.GetName(symbol, SyntaxLanguage.CSharp),
href = $"{id}{ext}",
containsLeafNodes = true,
type = type.TypeKind switch
{
TypeKind.Class => TocNodeType.Class,
TypeKind.Interface => TocNodeType.Interface,
TypeKind.Struct => TocNodeType.Struct,
TypeKind.Delegate => TocNodeType.Delegate,
TypeKind.Enum => TocNodeType.Enum,
_ => throw new NotSupportedException($"Unknown type kind {type.TypeKind}"),
}
});
}
foreach (var child in type.GetTypeMembers())
{
foreach (var item in CreateToc(child, compilation))
yield return item;
}
if (config.MemberLayout is MemberLayout.SeparatePages && type.TypeKind is TypeKind.Class or TypeKind.Interface or TypeKind.Struct)
{
node.items ??= new();
foreach (var member in type.GetMembers())
node.items.AddRange(CreateToc(member, compilation));
}
node.symbols.Add((symbol, compilation));
if (!idExists)
{
yield return node;
}
}
IEnumerable<TocNode> CreateMemberToc(ISymbol symbol)
{
var type = symbol switch
{
IPropertySymbol => TocNodeType.Property,
IFieldSymbol => TocNodeType.Field,
IEventSymbol => TocNodeType.Event,
IMethodSymbol method when SymbolHelper.IsConstructor(method) => TocNodeType.Constructor,
IMethodSymbol method when SymbolHelper.IsMethod(method) => TocNodeType.Method,
IMethodSymbol method when SymbolHelper.IsOperator(method) => TocNodeType.Operator,
_ => TocNodeType.None,
};
if (type is TocNodeType.None)
yield break;
var id = VisitorHelper.PathFriendlyId(VisitorHelper.GetOverloadId(symbol));
if (!tocNodes.TryGetValue(id, out var node))
{
tocNodes.Add(id, node = new()
{
id = id,
name = SymbolFormatter.GetName(symbol, SyntaxLanguage.CSharp, overload: true),
href = $"{id}{ext}",
containsLeafNodes = true,
type = type,
});
yield return node;
}
node.symbols.Add((symbol, compilation));
}
}
static void SortToc(List<TocNode> items, bool root)
{
items.Sort((a, b) => a.type.CompareTo(b.type) is var r && r is 0 ? a.name.CompareTo(b.name) : r);
if (!root)
{
InsertCategory(TocNodeType.Class, "Classes");
InsertCategory(TocNodeType.Struct, "Structs");
InsertCategory(TocNodeType.Interface, "Interfaces");
InsertCategory(TocNodeType.Enum, "Enums");
InsertCategory(TocNodeType.Delegate, "Delegates");
InsertCategory(TocNodeType.Constructor, "Constructors");
InsertCategory(TocNodeType.Field, "Fields");
InsertCategory(TocNodeType.Property, "Properties");
InsertCategory(TocNodeType.Method, "Methods");
InsertCategory(TocNodeType.Event, "Events");
InsertCategory(TocNodeType.Operator, "Operators");
}
foreach (var item in items)
{
if (item.items is not null)
SortToc(item.items, root: false);
}
void InsertCategory(TocNodeType type, string name)
{
if (items.FirstOrDefault(i => i.type == type) is { } node)
items.Insert(items.IndexOf(node), new() { name = name });
}
}
}
private static IEnumerable<(string id, List<(ISymbol symbol, Compilation compilation)> symbols)> EnumerateToc(List<TocNode> items)
{
foreach (var item in items)
{
if (item.items is not null)
foreach (var i in EnumerateToc(item.items))
yield return i;
if (item.id is not null && item.symbols.Count > 0)
yield return (item.id, item.symbols);
}
}
}