Skip to content

Commit 3aef633

Browse files
shiboken6/Documentation: Fully qualify class TOC entries for nested mode
Introduce a data structure for documented classes that has name/full name and file name so that the class TOC can be sorted by name and reference the full name. Refactor the fancyToc() function to be able to handle that. For the old, flat mode, the unqualified name is used as was before. Pick-to: 6.8 Change-Id: Ie7528d388faedd5a7bab58394eb6b84db0dc57e4 Reviewed-by: Cristian Maureira-Fredes <[email protected]>
1 parent a8b0954 commit 3aef633

File tree

1 file changed

+85
-47
lines changed

1 file changed

+85
-47
lines changed

sources/shiboken6/generator/qtdoc/qtdocgenerator.cpp

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
#include <algorithm>
4747
#include <limits>
48+
#include <unordered_set>
4849

4950
using namespace Qt::StringLiterals;
5051

@@ -53,9 +54,21 @@ static inline QString classScope(const AbstractMetaClassCPtr &metaClass)
5354
return metaClass->fullName();
5455
}
5556

57+
struct DocClassEntry
58+
{
59+
QString name;
60+
QString fullName;
61+
QString file;
62+
};
63+
64+
static bool classEntryLessThan(const DocClassEntry &e1, const DocClassEntry &e2)
65+
{
66+
return e1.name < e2.name;
67+
}
68+
5669
struct DocPackage
5770
{
58-
QStringList classPages;
71+
QList<DocClassEntry> classPages;
5972
QStringList decoratorPages;
6073
AbstractMetaFunctionCList globalFunctions;
6174
AbstractMetaEnumList globalEnums;
@@ -220,7 +233,8 @@ static void readExtraDoc(const QFileInfo &fi,
220233
DocPackage *docPackage, QStringList *extraTocEntries)
221234
{
222235
// Strip to "Property.rst" in output directory
223-
const QString newFileName = fi.fileName().mid(moduleName.size() + 1);
236+
const QString newFileName = fi.fileName().sliced(moduleName.size() + 1);
237+
const QString fullClassName = fi.completeBaseName().sliced(moduleName.size() + 1);
224238
QFile sourceFile(fi.absoluteFilePath());
225239
if (!sourceFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
226240
qCWarning(lcShibokenDoc, "%s", qPrintable(msgCannotOpenForReading(sourceFile)));
@@ -234,10 +248,15 @@ static void readExtraDoc(const QFileInfo &fi,
234248
return;
235249
}
236250
targetFile.write(contents);
237-
if (contents.contains("decorator::"))
251+
if (contents.contains("decorator::")) {
238252
docPackage->decoratorPages.append(newFileName);
239-
else
240-
docPackage->classPages.append(newFileName);
253+
} else {
254+
QString name = fullClassName;
255+
auto dot = name.lastIndexOf(u'.');
256+
if (dot != -1)
257+
name.remove(0, dot + 1);
258+
docPackage->classPages.append({name, fullClassName, newFileName});
259+
}
241260
extraTocEntries->append(fileNameToTocEntry(newFileName));
242261
}
243262

@@ -447,7 +466,8 @@ void QtDocGenerator::generateClassRecursion(TextStream &s, const QString &target
447466

448467
qCDebug(lcShibokenDoc, "Generating Documentation for %s", qPrintable(metaClass->fullName()));
449468

450-
m_packages[metaClass->package()].classPages << fileNameForContext(classContext);
469+
m_packages[metaClass->package()].classPages.append({metaClass->name(), metaClass->fullName(),
470+
fileNameForContext(classContext)});
451471

452472
doGenerateClass(s, targetDir, metaClass);
453473

@@ -1017,34 +1037,7 @@ void QtDocGenerator::writeFunctionDocumentation(TextStream &s, const AbstractMet
10171037
func, scope, images);
10181038
}
10191039

1020-
static QStringList fileListToToc(const QStringList &items)
1021-
{
1022-
QStringList result;
1023-
result.reserve(items.size());
1024-
std::transform(items.cbegin(), items.cend(), std::back_inserter(result),
1025-
fileNameToTocEntry);
1026-
return result;
1027-
}
1028-
1029-
static QStringList functionListToToc(const AbstractMetaFunctionCList &functions)
1030-
{
1031-
QStringList result;
1032-
result.reserve(functions.size());
1033-
for (const auto &f : functions)
1034-
result.append(f->name());
1035-
// Functions are sorted by the Metabuilder; erase overloads
1036-
result.erase(std::unique(result.begin(), result.end()), result.end());
1037-
return result;
1038-
}
1039-
1040-
static QStringList enumListToToc(const AbstractMetaEnumList &enums)
1041-
{
1042-
QStringList result;
1043-
result.reserve(enums.size());
1044-
for (const auto &e : enums)
1045-
result.append(e.name());
1046-
return result;
1047-
}
1040+
using TocMap = QMap<QChar, QStringList>;
10481041

10491042
// Sort entries for a TOC by first character, dropping the
10501043
// leading common Qt prefixes like 'Q'.
@@ -1065,19 +1058,57 @@ static QChar sortKey(const QString &key)
10651058
return idx < size ? key.at(idx).toUpper() : u'A';
10661059
}
10671060

1061+
static TocMap classEntryListToToc(const QList<DocClassEntry> &entries,
1062+
TypeSystem::DocMode docMode)
1063+
{
1064+
const bool fullyQualified = docMode == TypeSystem::DocMode::Nested;
1065+
TocMap result;
1066+
// Sort by name, use full href
1067+
for (const auto &e : entries)
1068+
result[sortKey(e.name)] << (fullyQualified ? e.fullName : e.name);
1069+
return result;
1070+
}
1071+
1072+
static TocMap fileListToToc(const QStringList &items)
1073+
{
1074+
TocMap result;
1075+
for (const auto &item : items) {
1076+
const QString entry = fileNameToTocEntry(item);
1077+
result[sortKey(entry)] << entry;
1078+
}
1079+
return result;
1080+
}
1081+
1082+
static TocMap functionListToToc(const AbstractMetaFunctionCList &functions)
1083+
{
1084+
TocMap result;
1085+
// Functions are sorted by the Metabuilder; erase overloads
1086+
std::unordered_set<QString> seenNames;
1087+
for (const auto &f : functions) {
1088+
const QString &name = f->name();
1089+
if (seenNames.find(name) == seenNames.end()) {
1090+
seenNames.insert(name);
1091+
result[sortKey(name)] << name;
1092+
}
1093+
}
1094+
return result;
1095+
}
1096+
1097+
static TocMap enumListToToc(const AbstractMetaEnumList &enums)
1098+
{
1099+
TocMap result;
1100+
for (const auto &e : enums)
1101+
result[sortKey(e.name())] << e.name();
1102+
return result;
1103+
}
1104+
10681105
static void writeFancyToc(TextStream& s, QAnyStringView title,
1069-
const QStringList& items,
1106+
const TocMap &tocMap,
10701107
QLatin1StringView referenceType)
10711108
{
1072-
using TocMap = QMap<QChar, QStringList>;
1073-
1074-
if (items.isEmpty())
1109+
if (tocMap.isEmpty())
10751110
return;
10761111

1077-
TocMap tocMap;
1078-
for (const QString &item : items)
1079-
tocMap[sortKey(item)] << item;
1080-
10811112
qsizetype maxColumnCount = 0;
10821113
for (auto it = tocMap.cbegin(), end = tocMap.cend(); it != end; ++it) {
10831114
if (it.value().size() > maxColumnCount)
@@ -1096,7 +1127,10 @@ static void writeFancyToc(TextStream& s, QAnyStringView title,
10961127
row.clear();
10971128
row << QtXmlToSphinx::TableCell(QString{});
10981129
}
1099-
const QString entry = "* :"_L1 + referenceType + ":`"_L1 + item + u'`';
1130+
QString entry = "* :"_L1 + referenceType + ":`"_L1;
1131+
if (item.contains(u'.'))
1132+
entry += u'~';
1133+
entry += item + u'`';
11001134
row << QtXmlToSphinx::TableCell(entry);
11011135
}
11021136
if (row.size() > 1) {
@@ -1194,9 +1228,10 @@ static bool imagesFromRstDocs(const QByteArray &rstDoc, const QString &scope,
11941228

11951229
void QtDocGenerator::writeModuleDocumentation()
11961230
{
1231+
auto *typeDb = TypeDatabase::instance();
11971232
for (auto it = m_packages.begin(), end = m_packages.end(); it != end; ++it) {
11981233
auto &docPackage = it.value();
1199-
std::sort(docPackage.classPages.begin(), docPackage.classPages.end());
1234+
std::sort(docPackage.classPages.begin(), docPackage.classPages.end(), classEntryLessThan);
12001235

12011236
QString key = it.key();
12021237
key.replace(u'.', u'/');
@@ -1244,8 +1279,8 @@ void QtDocGenerator::writeModuleDocumentation()
12441279
<< ":maxdepth: 1\n\n";
12451280
if (hasGlobals)
12461281
s << globalsPage << '\n';
1247-
for (const QString &className : std::as_const(docPackage.classPages))
1248-
s << className << '\n';
1282+
for (const auto &e : std::as_const(docPackage.classPages))
1283+
s << e.file << '\n';
12491284
s << "\n\n" << outdent << outdent << headline("Detailed Description");
12501285

12511286
// module doc is always wrong and C++istic, so go straight to the extra directory!
@@ -1279,7 +1314,10 @@ void QtDocGenerator::writeModuleDocumentation()
12791314
}
12801315
}
12811316

1282-
writeFancyToc(s, "List of Classes", fileListToToc(docPackage.classPages),
1317+
TypeSystemTypeEntryCPtr typeSystemEntry = typeDb->findTypeSystemType(it.key());
1318+
Q_ASSERT(typeSystemEntry);
1319+
writeFancyToc(s, "List of Classes", classEntryListToToc(docPackage.classPages,
1320+
typeSystemEntry->docMode()),
12831321
"class"_L1);
12841322
writeFancyToc(s, "List of Decorators", fileListToToc(docPackage.decoratorPages),
12851323
"deco"_L1);

0 commit comments

Comments
 (0)