Skip to content

Commit a0f694d

Browse files
committed
feat: sort-namespace-members-by option
fix #988
1 parent f0ba28d commit a0f694d

18 files changed

+4236
-64
lines changed

docs/mrdocs.schema.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,12 +419,12 @@
419419
},
420420
"sort-members": {
421421
"default": true,
422-
"description": "When set to `true`, sort the members of a record or namespace by name and parameters. When set to `false`, the members are included in the declaration order they are extracted.",
422+
"description": "When set to `true`, sort the members of a record by the criterion determined in the `sort-members-by` option. When set to `false`, the members are included in the declaration order they are extracted.",
423423
"enum": [
424424
true,
425425
false
426426
],
427-
"title": "Sort the members of a record or namespace",
427+
"title": "Sort the members of a record",
428428
"type": "boolean"
429429
},
430430
"sort-members-assignment-1st": {
@@ -439,12 +439,12 @@
439439
},
440440
"sort-members-by": {
441441
"default": "name",
442-
"description": "If `sort-members` is set to `true`, determine how members of a record or namespace are sorted. When set to `name`, members are sorted by name. When set to `location`, members are sorted by their primary location in the source code, considering the short name of the path and the location in the file.",
442+
"description": "If `sort-members` is set to `true`, determine how members of a record are sorted. When set to `name`, members are sorted by name. When set to `location`, members are sorted by their primary location in the source code, considering the short name of the path and the location in the file.",
443443
"enum": [
444444
"name",
445445
"location"
446446
],
447-
"title": "Determine how members of a record or namespace are sorted"
447+
"title": "Determine how members of a record are sorted"
448448
},
449449
"sort-members-conversion-last": {
450450
"default": true,
@@ -486,6 +486,15 @@
486486
"title": "Sort relational operators last",
487487
"type": "boolean"
488488
},
489+
"sort-namespace-members-by": {
490+
"default": "name",
491+
"description": "Although members of namespaces are always sorted, determine how members of a namespace are sorted. When set to `name`, members are sorted by name. When set to `location`, members are sorted by their primary location in the source code, considering the short name of the path and the location in the file.",
492+
"enum": [
493+
"name",
494+
"location"
495+
],
496+
"title": "Determine how members of a namespace are sorted"
497+
},
489498
"source-root": {
490499
"default": "<config-dir>",
491500
"description": "Path to the root directory of the source code. This path is used as a default for input files and a base for relative paths formed from absolute paths. This should typically be the root directory of the git project, as relative paths formed from it can be used to create links to these source files in the repository. Templates use the `base-url` option to create links to the source code.",

src/lib/ConfigOptions.json

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,26 @@
277277
},
278278
{
279279
"name": "sort-members",
280-
"brief": "Sort the members of a record or namespace",
281-
"details": "When set to `true`, sort the members of a record or namespace by name and parameters. When set to `false`, the members are included in the declaration order they are extracted.",
280+
"brief": "Sort the members of a record",
281+
"details": "When set to `true`, sort the members of a record by the criterion determined in the `sort-members-by` option. When set to `false`, the members are included in the declaration order they are extracted.",
282282
"type": "bool",
283283
"default": true
284284
},
285285
{
286286
"name": "sort-members-by",
287-
"brief": "Determine how members of a record or namespace are sorted",
288-
"details": "If `sort-members` is set to `true`, determine how members of a record or namespace are sorted. When set to `name`, members are sorted by name. When set to `location`, members are sorted by their primary location in the source code, considering the short name of the path and the location in the file.",
287+
"brief": "Determine how members of a record are sorted",
288+
"details": "If `sort-members` is set to `true`, determine how members of a record are sorted. When set to `name`, members are sorted by name. When set to `location`, members are sorted by their primary location in the source code, considering the short name of the path and the location in the file.",
289+
"type": "enum",
290+
"values": [
291+
"name",
292+
"location"
293+
],
294+
"default": "name"
295+
},
296+
{
297+
"name": "sort-namespace-members-by",
298+
"brief": "Determine how members of a namespace are sorted",
299+
"details": "Although members of namespaces are always sorted, determine how members of a namespace are sorted. When set to `name`, members are sorted by name. When set to `location`, members are sorted by their primary location in the source code, considering the short name of the path and the location in the file.",
289300
"type": "enum",
290301
"values": [
291302
"name",

src/lib/CorpusImpl.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -964,23 +964,27 @@ CorpusImpl::finalize()
964964
finalizer.build();
965965
}
966966

967+
// Add auto relates for member functions
967968
{
968969
report::debug(" - Finalizing auto-relates");
969970
DerivedFinalizer finalizer(*this);
970971
finalizer.build();
971972
}
972973

973-
if (config->sortMembers)
974+
// Sort members: this runs unconditionally because
975+
// the members of some symbol types are always sorted.
974976
{
975977
report::debug(" - Finalizing sorted members");
976978
SortMembersFinalizer finalizer(*this);
977979
finalizer.build();
978980
}
979981

980982
// Finalize javadoc
981-
report::debug(" - Finalizing javadoc");
982-
JavadocFinalizer finalizer(*this);
983-
finalizer.build();
983+
{
984+
report::debug(" - Finalizing javadoc");
985+
JavadocFinalizer finalizer(*this);
986+
finalizer.build();
987+
}
984988
}
985989

986990
} // clang::mrdocs

src/lib/Metadata/Finalizers/SortMembersFinalizer.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,13 @@ struct SymbolIDCompareFn
213213
}
214214

215215
// Special cases are handled, so use the configuration criteria
216-
switch (corpus_.config->sortMembersBy)
216+
Info const* P = corpus_.find(lhs.Parent);
217+
bool const isClassMember = P && P->isRecord();
218+
auto const generalSortCriteria =
219+
isClassMember
220+
? corpus_.config->sortMembersBy
221+
: corpus_.config->sortNamespaceMembersBy;
222+
switch (generalSortCriteria)
217223
{
218224
case clang::mrdocs::PublicSettings::SortSymbolBy::Name:
219225
if (auto const cmp = lhs.Name <=> rhs.Name; cmp != 0)
@@ -352,8 +358,11 @@ void
352358
SortMembersFinalizer::
353359
operator()(RecordInfo& I)
354360
{
355-
// Sort members of all tranches
356-
sortMembers(I.Interface);
361+
// Sort members of all tranches if sorting is enabled for records
362+
if (corpus_.config->sortMembers)
363+
{
364+
sortMembers(I.Interface);
365+
}
357366

358367
// Recursively sort members of child records and overloads
359368
for (RecordInfo& RI: toDerivedView<RecordInfo>(I.Interface.Public.Records, corpus_))

0 commit comments

Comments
 (0)