diff --git a/autotests/syntaxrepository_test.cpp b/autotests/syntaxrepository_test.cpp --- a/autotests/syntaxrepository_test.cpp +++ b/autotests/syntaxrepository_test.cpp @@ -162,16 +162,26 @@ auto formats = def.formats(); QVERIFY(!formats.isEmpty()); + // verify that the formats are sorted, such that the order matches the order of the itemDatas in the xml files. + auto sortComparator = [](const KSyntaxHighlighting::Format & lhs, const KSyntaxHighlighting::Format & rhs) { + return lhs.id() < rhs.id(); + }; + QVERIFY(std::is_sorted(formats.begin(), formats.end(), sortComparator)); + + // check all names are listed QStringList formatNames; foreach (const auto & format, formats) { formatNames.append(format.name()); } - QVERIFY(formatNames.contains(QStringLiteral("Normal Text"))); - QVERIFY(formatNames.contains(QStringLiteral("Name"))); - QVERIFY(formatNames.contains(QStringLiteral("E-Mail"))); - QVERIFY(formatNames.contains(QStringLiteral("Date"))); - QVERIFY(formatNames.contains(QStringLiteral("Entry"))); - QVERIFY(!formatNames.contains(QStringLiteral("Does not Exist"))); + + const QStringList expectedItemDatas = { + QStringLiteral("Normal Text"), + QStringLiteral("Name"), + QStringLiteral("E-Mail"), + QStringLiteral("Date"), + QStringLiteral("Entry") + }; + QCOMPARE(formatNames, expectedItemDatas); } void testIncludedDefinitions() diff --git a/src/lib/definition.h b/src/lib/definition.h --- a/src/lib/definition.h +++ b/src/lib/definition.h @@ -188,6 +188,7 @@ /** * Returns a list of all Format items used by this definition. + * The order of the Format items equals the order of the itemDatas in the xml file. * @since 5.49 */ QVector formats() const; diff --git a/src/lib/definition.cpp b/src/lib/definition.cpp --- a/src/lib/definition.cpp +++ b/src/lib/definition.cpp @@ -196,7 +196,14 @@ QVector Definition::formats() const { d->load(); - return QVector::fromList(d->formats.values()); + + // sort formats so that the order matches the order of the itemDatas in the xml files. + auto formatList = QVector::fromList(d->formats.values()); + std::sort(formatList.begin(), formatList.end(), [](const KSyntaxHighlighting::Format & lhs, const KSyntaxHighlighting::Format & rhs){ + return lhs.id() < rhs.id(); + }); + + return formatList; } QVector Definition::includedDefinitions() const