/
GroupPlugin.ts
235 lines (209 loc) · 7.49 KB
/
GroupPlugin.ts
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
import {
Reflection,
ReflectionKind,
ContainerReflection,
DeclarationReflection,
} from "../../models/reflections/index";
import { ReflectionGroup } from "../../models/ReflectionGroup";
import { Component, ConverterComponent } from "../components";
import { Converter } from "../converter";
import type { Context } from "../context";
import { getSortFunction } from "../../utils/sort";
import { BindOption, removeIf } from "../../utils";
import { Comment } from "../../models";
/**
* A handler that sorts and groups the found reflections in the resolving phase.
*
* The handler sets the `groups` property of all container reflections.
*/
@Component({ name: "group" })
export class GroupPlugin extends ConverterComponent {
/**
* Define the singular name of individual reflection kinds.
*/
static SINGULARS = {
[ReflectionKind.Enum]: "Enumeration",
[ReflectionKind.EnumMember]: "Enumeration Member",
};
/**
* Define the plural name of individual reflection kinds.
*/
static PLURALS = {
[ReflectionKind.Class]: "Classes",
[ReflectionKind.Property]: "Properties",
[ReflectionKind.Enum]: "Enumerations",
[ReflectionKind.EnumMember]: "Enumeration Members",
[ReflectionKind.TypeAlias]: "Type Aliases",
};
sortFunction!: (reflections: DeclarationReflection[]) => void;
@BindOption("searchGroupBoosts")
boosts!: Record<string, number>;
usedBoosts = new Set<string>();
/**
* Create a new GroupPlugin instance.
*/
override initialize() {
this.listenTo(this.owner, {
[Converter.EVENT_RESOLVE_BEGIN]: () => {
this.sortFunction = getSortFunction(this.application.options);
},
[Converter.EVENT_RESOLVE]: this.onResolve,
[Converter.EVENT_RESOLVE_END]: this.onEndResolve,
});
}
/**
* Triggered when the converter resolves a reflection.
*
* @param context The context object describing the current state the converter is in.
* @param reflection The reflection that is currently resolved.
*/
private onResolve(_context: Context, reflection: Reflection) {
reflection.kindString = GroupPlugin.getKindSingular(reflection.kind);
if (reflection instanceof ContainerReflection) {
this.group(reflection);
}
}
/**
* Triggered when the converter has finished resolving a project.
*
* @param context The context object describing the current state the converter is in.
*/
private onEndResolve(context: Context) {
this.group(context.project);
const unusedBoosts = new Set(Object.keys(this.boosts));
for (const boost of this.usedBoosts) {
unusedBoosts.delete(boost);
}
this.usedBoosts.clear();
if (
unusedBoosts.size &&
this.application.options.isSet("searchGroupBoosts")
) {
context.logger.warn(
`Not all groups specified in searchGroupBoosts were used in the documentation.` +
` The unused groups were:\n\t${Array.from(
unusedBoosts
).join("\n\t")}`
);
}
}
private group(reflection: ContainerReflection) {
if (
reflection.children &&
reflection.children.length > 0 &&
!reflection.groups
) {
this.sortFunction(reflection.children);
reflection.groups = this.getReflectionGroups(reflection.children);
}
}
/**
* Extracts the groups for a given reflection.
*
* @privateRemarks
* If you change this, also update getCategories in CategoryPlugin accordingly.
*/
getGroups(reflection: DeclarationReflection) {
const groups = new Set<string>();
function extractGroupTags(comment: Comment | undefined) {
if (!comment) return;
removeIf(comment.blockTags, (tag) => {
if (tag.tag === "@group") {
groups.add(Comment.combineDisplayParts(tag.content).trim());
return true;
}
return false;
});
}
extractGroupTags(reflection.comment);
for (const sig of reflection.getNonIndexSignatures()) {
extractGroupTags(sig.comment);
}
if (reflection.type?.type === "reflection") {
extractGroupTags(reflection.type.declaration.comment);
for (const sig of reflection.type.declaration.getNonIndexSignatures()) {
extractGroupTags(sig.comment);
}
}
groups.delete("");
if (groups.size === 0) {
groups.add(GroupPlugin.getKindPlural(reflection.kind));
}
for (const group of groups) {
if (group in this.boosts) {
this.usedBoosts.add(group);
reflection.relevanceBoost =
(reflection.relevanceBoost ?? 1) * this.boosts[group];
}
}
return groups;
}
/**
* Create a grouped representation of the given list of reflections.
*
* Reflections are grouped by kind and sorted by weight and name.
*
* @param reflections The reflections that should be grouped.
* @returns An array containing all children of the given reflection grouped by their kind.
*/
getReflectionGroups(
reflections: DeclarationReflection[]
): ReflectionGroup[] {
const groups = new Map<string, ReflectionGroup>();
reflections.forEach((child) => {
for (const name of this.getGroups(child)) {
let group = groups.get(name);
if (!group) {
group = new ReflectionGroup(name);
groups.set(name, group);
}
group.children.push(child);
}
});
return Array.from(groups.values());
}
/**
* Transform the internal typescript kind identifier into a human readable version.
*
* @param kind The original typescript kind identifier.
* @returns A human readable version of the given typescript kind identifier.
*/
private static getKindString(kind: ReflectionKind): string {
let str = ReflectionKind[kind];
str = str.replace(
/(.)([A-Z])/g,
(_m, a, b) => a + " " + b.toLowerCase()
);
return str;
}
/**
* Return the singular name of a internal typescript kind identifier.
*
* @param kind The original internal typescript kind identifier.
* @returns The singular name of the given internal typescript kind identifier
*/
static getKindSingular(kind: ReflectionKind): string {
if (kind in GroupPlugin.SINGULARS) {
return GroupPlugin.SINGULARS[
kind as keyof typeof GroupPlugin.SINGULARS
];
} else {
return GroupPlugin.getKindString(kind);
}
}
/**
* Return the plural name of a internal typescript kind identifier.
*
* @param kind The original internal typescript kind identifier.
* @returns The plural name of the given internal typescript kind identifier
*/
static getKindPlural(kind: ReflectionKind): string {
if (kind in GroupPlugin.PLURALS) {
return GroupPlugin.PLURALS[
kind as keyof typeof GroupPlugin.PLURALS
];
} else {
return this.getKindString(kind) + "s";
}
}
}