/
CategoryPlugin.ts
283 lines (261 loc) · 9.18 KB
/
CategoryPlugin.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import {
Reflection,
ContainerReflection,
DeclarationReflection,
CommentTag,
} from "../../models";
import { ReflectionCategory } from "../../models";
import { Component, ConverterComponent } from "../components";
import { Converter } from "../converter";
import type { Context } from "../context";
import { BindOption } from "../../utils";
import type { Comment } from "../../models";
/**
* A handler that sorts and categorizes the found reflections in the resolving phase.
*
* The handler sets the ´category´ property of all reflections.
*/
@Component({ name: "category" })
export class CategoryPlugin extends ConverterComponent {
@BindOption("defaultCategory")
defaultCategory!: string;
@BindOption("categoryOrder")
categoryOrder!: string[];
@BindOption("categorizeByGroup")
categorizeByGroup!: boolean;
// For use in static methods
static defaultCategory = "Other";
static WEIGHTS: string[] = [];
/**
* Create a new CategoryPlugin instance.
*/
override initialize() {
this.listenTo(
this.owner,
{
[Converter.EVENT_BEGIN]: this.onBegin,
[Converter.EVENT_RESOLVE]: this.onResolve,
[Converter.EVENT_RESOLVE_END]: this.onEndResolve,
},
undefined,
-200
);
}
/**
* Triggered when the converter begins converting a project.
*/
private onBegin(_context: Context) {
// Set up static properties
if (this.defaultCategory) {
CategoryPlugin.defaultCategory = this.defaultCategory;
}
if (this.categoryOrder) {
CategoryPlugin.WEIGHTS = this.categoryOrder;
}
}
/**
* 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) {
if (reflection instanceof ContainerReflection) {
this.categorize(
reflection,
context.getSearchOptions()?.searchCategoryBoosts ?? {}
);
}
}
/**
* 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) {
const project = context.project;
this.categorize(
project,
context.getSearchOptions()?.searchCategoryBoosts ?? {}
);
}
private categorize(
obj: ContainerReflection,
categorySearchBoosts: { [key: string]: number }
) {
if (this.categorizeByGroup) {
this.groupCategorize(obj, categorySearchBoosts);
} else {
CategoryPlugin.lumpCategorize(obj, categorySearchBoosts);
}
}
private groupCategorize(
obj: ContainerReflection,
categorySearchBoosts: { [key: string]: number }
) {
if (!obj.groups || obj.groups.length === 0) {
return;
}
obj.groups.forEach((group) => {
if (group.categories) return;
group.categories = CategoryPlugin.getReflectionCategories(
group.children,
categorySearchBoosts
);
if (group.categories && group.categories.length > 1) {
group.categories.sort(CategoryPlugin.sortCatCallback);
} else if (
group.categories.length === 1 &&
group.categories[0].title === CategoryPlugin.defaultCategory
) {
// no categories if everything is uncategorized
group.categories = undefined;
}
});
}
static lumpCategorize(
obj: ContainerReflection,
categorySearchBoosts: { [key: string]: number }
) {
if (!obj.children || obj.children.length === 0 || obj.categories) {
return;
}
obj.categories = CategoryPlugin.getReflectionCategories(
obj.children,
categorySearchBoosts
);
if (obj.categories && obj.categories.length > 1) {
obj.categories.sort(CategoryPlugin.sortCatCallback);
} else if (
obj.categories.length === 1 &&
obj.categories[0].title === CategoryPlugin.defaultCategory
) {
// no categories if everything is uncategorized
obj.categories = undefined;
}
}
/**
* Create a categorized representation of the given list of reflections.
*
* @param reflections The reflections that should be categorized.
* @param categorySearchBoosts A user-supplied map of category titles, for computing a
* relevance boost to be used when searching
* @returns An array containing all children of the given reflection categorized
*/
static getReflectionCategories(
reflections: DeclarationReflection[],
categorySearchBoosts: { [key: string]: number }
): ReflectionCategory[] {
const categories: ReflectionCategory[] = [];
let defaultCat: ReflectionCategory | undefined;
reflections.forEach((child) => {
const childCategories = CategoryPlugin.getCategories(child);
if (childCategories.size === 0) {
if (!defaultCat) {
defaultCat = categories.find(
(category) =>
category.title === CategoryPlugin.defaultCategory
);
if (!defaultCat) {
defaultCat = new ReflectionCategory(
CategoryPlugin.defaultCategory
);
categories.push(defaultCat);
}
}
defaultCat.children.push(child);
return;
}
for (const childCat of childCategories) {
let category = categories.find((cat) => cat.title === childCat);
const catBoost = categorySearchBoosts[category?.title ?? -1];
if (catBoost != undefined) {
child.relevanceBoost =
(child.relevanceBoost ?? 1) * catBoost;
}
if (category) {
category.children.push(child);
continue;
}
category = new ReflectionCategory(childCat);
category.children.push(child);
categories.push(category);
}
});
return categories;
}
/**
* Return the category of a given reflection.
*
* @param reflection The reflection.
* @returns The category the reflection belongs to
*/
static getCategories(reflection: DeclarationReflection) {
function extractCategoryTag(comment: Comment | undefined) {
const categories = new Set<string>();
if (!comment) return categories;
const tags = comment.tags;
const commentTags: CommentTag[] = [];
tags.forEach((tag) => {
if (tag.tagName !== "category") {
commentTags.push(tag);
return;
}
const text = tag.text.trim();
if (!text) {
return;
}
categories.add(text);
});
comment.tags = commentTags;
return categories;
}
let categories = new Set<string>();
if (reflection.comment) {
categories = extractCategoryTag(reflection.comment);
} else if (reflection.signatures) {
for (const sig of reflection.signatures) {
for (const cat of extractCategoryTag(sig.comment)) {
categories.add(cat);
}
}
}
if (reflection.type?.type === "reflection") {
reflection.type.declaration.comment?.removeTags("category");
reflection.type.declaration.signatures?.forEach((s) =>
s.comment?.removeTags("category")
);
}
return categories;
}
/**
* Callback used to sort categories by name.
*
* @param a The left reflection to sort.
* @param b The right reflection to sort.
* @returns The sorting weight.
*/
static sortCatCallback(
a: ReflectionCategory,
b: ReflectionCategory
): number {
let aWeight = CategoryPlugin.WEIGHTS.indexOf(a.title);
let bWeight = CategoryPlugin.WEIGHTS.indexOf(b.title);
if (aWeight === -1 || bWeight === -1) {
let asteriskIndex = CategoryPlugin.WEIGHTS.indexOf("*");
if (asteriskIndex === -1) {
asteriskIndex = CategoryPlugin.WEIGHTS.length;
}
if (aWeight === -1) {
aWeight = asteriskIndex;
}
if (bWeight === -1) {
bWeight = asteriskIndex;
}
}
if (aWeight === bWeight) {
return a.title > b.title ? 1 : -1;
}
return aWeight - bWeight;
}
}