Skip to content

Commit 69a2b60

Browse files
committed
Add groupOrder option
Resolves #2251
1 parent ab975f9 commit 69a2b60

File tree

6 files changed

+74
-3
lines changed

6 files changed

+74
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Added support for discovering a "module" comment on global files, #2165.
66
- Added copy code to clipboard button, #2153.
77
- Function `@returns` blocks will now be rendered with the return type, #2180.
8+
- Added `--groupOrder` option to specify the sort order of groups, #2251.
89

910
### Bug Fixes
1011

src/lib/converter/plugins/GroupPlugin.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,21 @@ export class GroupPlugin extends ConverterComponent {
2424
@BindOption("searchGroupBoosts")
2525
boosts!: Record<string, number>;
2626

27+
@BindOption("groupOrder")
28+
groupOrder!: string[];
29+
2730
usedBoosts = new Set<string>();
2831

32+
static WEIGHTS: string[] = [];
33+
2934
/**
3035
* Create a new GroupPlugin instance.
3136
*/
3237
override initialize() {
3338
this.listenTo(this.owner, {
3439
[Converter.EVENT_RESOLVE_BEGIN]: () => {
3540
this.sortFunction = getSortFunction(this.application.options);
41+
GroupPlugin.WEIGHTS = this.groupOrder;
3642
},
3743
[Converter.EVENT_RESOLVE]: this.onResolve,
3844
[Converter.EVENT_RESOLVE_END]: this.onEndResolve,
@@ -162,6 +168,30 @@ export class GroupPlugin extends ConverterComponent {
162168
}
163169
});
164170

165-
return Array.from(groups.values());
171+
return Array.from(groups.values()).sort(GroupPlugin.sortGroupCallback);
172+
}
173+
174+
/**
175+
* Callback used to sort groups by name.
176+
*/
177+
static sortGroupCallback(a: ReflectionGroup, b: ReflectionGroup): number {
178+
let aWeight = GroupPlugin.WEIGHTS.indexOf(a.title);
179+
let bWeight = GroupPlugin.WEIGHTS.indexOf(b.title);
180+
if (aWeight === -1 || bWeight === -1) {
181+
let asteriskIndex = GroupPlugin.WEIGHTS.indexOf("*");
182+
if (asteriskIndex === -1) {
183+
asteriskIndex = GroupPlugin.WEIGHTS.length;
184+
}
185+
if (aWeight === -1) {
186+
aWeight = asteriskIndex;
187+
}
188+
if (bWeight === -1) {
189+
bWeight = asteriskIndex;
190+
}
191+
}
192+
if (aWeight === bWeight) {
193+
return a.title > b.title ? 1 : -1;
194+
}
195+
return aWeight - bWeight;
166196
}
167197
}

src/lib/utils/options/declaration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export interface TypeDocOptionMap {
164164
categorizeByGroup: boolean;
165165
defaultCategory: string;
166166
categoryOrder: string[];
167+
groupOrder: string[];
167168
sort: SortStrategy[];
168169
kindSortOrder: ReflectionKind.KindString[];
169170

src/lib/utils/options/sources/typedoc.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,32 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
522522
help: "Specify the order in which categories appear. * indicates the relative order for categories not in the list.",
523523
type: ParameterType.Array,
524524
});
525+
options.addDeclaration({
526+
name: "groupOrder",
527+
help: "Specify the order in which groups appear. * indicates the relative order for groups not in the list.",
528+
type: ParameterType.Array,
529+
// Defaults to the same as the defaultKindSortOrder in sort.ts
530+
defaultValue: [
531+
ReflectionKind.Reference,
532+
// project is never a child so never added to a group
533+
ReflectionKind.Module,
534+
ReflectionKind.Namespace,
535+
ReflectionKind.Enum,
536+
ReflectionKind.EnumMember,
537+
ReflectionKind.Class,
538+
ReflectionKind.Interface,
539+
ReflectionKind.TypeAlias,
540+
541+
ReflectionKind.Constructor,
542+
ReflectionKind.Property,
543+
ReflectionKind.Variable,
544+
ReflectionKind.Function,
545+
ReflectionKind.Accessor,
546+
ReflectionKind.Method,
547+
548+
// others are never added to groups
549+
].map(ReflectionKind.pluralString),
550+
});
525551
options.addDeclaration({
526552
name: "sort",
527553
help: "Specify the sort strategy for documented values.",

src/test/behavior.c2.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,16 @@ describe("Behavior Tests", () => {
412412
const A = query(project, "A");
413413
const B = query(project, "B");
414414
const C = query(project, "C");
415+
const D = query(project, "D");
415416

416417
equal(
417418
project.groups?.map((g) => g.title),
418-
["A", "B", "With Spaces"]
419+
["Variables", "A", "B", "With Spaces"]
419420
);
420421

421422
equal(
422423
project.groups.map((g) => g.children),
423-
[[A, B], [B], [C]]
424+
[[D], [A, B], [B], [C]]
424425
);
425426
});
426427

@@ -909,4 +910,13 @@ describe("Behavior Tests", () => {
909910

910911
equal(comments, ["Bar docs", "Bar.a docs", "Foo.b docs"]);
911912
});
913+
914+
it("Allows specifying group sort order #2251", () => {
915+
app.options.setValue("groupOrder", ["B", "Variables", "A"]);
916+
const project = convert("groupTag");
917+
equal(
918+
project.groups?.map((g) => g.title),
919+
["B", "Variables", "A", "With Spaces"]
920+
);
921+
});
912922
});

src/test/converter2/behavior/groupTag.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ export const B = 123;
1313
* @group With Spaces
1414
*/
1515
export const C = 123;
16+
17+
// no group
18+
export const D = 123;

0 commit comments

Comments
 (0)