From b41abc94bb82e300c5f646328d99a04a2339d208 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 3 Jan 2023 11:54:05 +0100 Subject: [PATCH] Fix highlighting of debugger disassembly view The disassembly view uses a custom MIME type that derives from x-asm, but that didn't trigger highlighting. Make the generic highlighter look at the parent MIME types if no highlighting was found for the MIME type itself. Generalize this "go breadth-first through the parents" that was already used for resolving editor factories. Change-Id: Ia054058a8c06b9d8849384e79ee3b83fbc12279c Reviewed-by: David Schulz --- src/libs/utils/mimetypes2/mimeutils.cpp | 30 +++++++++++++++++++ src/libs/utils/mimeutils.h | 6 ++++ .../editormanager/ieditorfactory_p.h | 26 ++-------------- src/plugins/texteditor/highlighter.cpp | 17 ++++++----- 4 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/libs/utils/mimetypes2/mimeutils.cpp b/src/libs/utils/mimetypes2/mimeutils.cpp index 7daafcd8675..10182d58122 100644 --- a/src/libs/utils/mimetypes2/mimeutils.cpp +++ b/src/libs/utils/mimetypes2/mimeutils.cpp @@ -100,4 +100,34 @@ void setMagicRulesForMimeType(const MimeType &mimeType, const QMapsetMagicRulesForMimeType(mimeType, rules); } +void visitMimeParents(const MimeType &mimeType, + const std::function &visitor) +{ + // search breadth-first through parent hierarchy, e.g. for hierarchy + // * application/x-ruby + // * application/x-executable + // * application/octet-stream + // * text/plain + QList queue; + QSet seen; + queue.append(mimeType); + seen.insert(mimeType.name()); + while (!queue.isEmpty()) { + const MimeType mt = queue.takeFirst(); + if (!visitor(mt)) + break; + // add parent mime types + const QStringList parentNames = mt.parentMimeTypes(); + for (const QString &parentName : parentNames) { + const MimeType parent = mimeTypeForName(parentName); + if (parent.isValid()) { + int seenSize = seen.size(); + seen.insert(parent.name()); + if (seen.size() != seenSize) // not seen before, so add + queue.append(parent); + } + } + } +} + } // namespace Utils diff --git a/src/libs/utils/mimeutils.h b/src/libs/utils/mimeutils.h index 15ce77d398c..597dd0ae571 100644 --- a/src/libs/utils/mimeutils.h +++ b/src/libs/utils/mimeutils.h @@ -8,6 +8,8 @@ #include #include +#include + namespace Utils { class FilePath; @@ -49,4 +51,8 @@ QTCREATOR_UTILS_EXPORT void setGlobPatternsForMimeType(const MimeType &mimeType, QTCREATOR_UTILS_EXPORT void setMagicRulesForMimeType( const MimeType &mimeType, const QMap> &rules); // priority -> rules +// visits all parents breadth-first +// visitor should return false to break the loop, true to continue +QTCREATOR_UTILS_EXPORT void visitMimeParents( + const MimeType &mimeType, const std::function &visitor); } // namespace Utils diff --git a/src/plugins/coreplugin/editormanager/ieditorfactory_p.h b/src/plugins/coreplugin/editormanager/ieditorfactory_p.h index 06802b5db94..8ec0e53726d 100644 --- a/src/plugins/coreplugin/editormanager/ieditorfactory_p.h +++ b/src/plugins/coreplugin/editormanager/ieditorfactory_p.h @@ -25,17 +25,7 @@ static void mimeTypeFactoryLookup(const Utils::MimeType &mimeType, QList *list) { QSet matches; - // search breadth-first through parent hierarchy, e.g. for hierarchy - // * application/x-ruby - // * application/x-executable - // * application/octet-stream - // * text/plain - QList queue; - QSet seen; - queue.append(mimeType); - seen.insert(mimeType.name()); - while (!queue.isEmpty()) { - Utils::MimeType mt = queue.takeFirst(); + Utils::visitMimeParents(mimeType, [&](const Utils::MimeType &mt) -> bool { // check for matching factories for (EditorTypeLike *factory : allFactories) { if (!matches.contains(factory)) { @@ -48,18 +38,8 @@ static void mimeTypeFactoryLookup(const Utils::MimeType &mimeType, } } } - // add parent mime types - const QStringList parentNames = mt.parentMimeTypes(); - for (const QString &parentName : parentNames) { - const Utils::MimeType parent = Utils::mimeTypeForName(parentName); - if (parent.isValid()) { - int seenSize = seen.size(); - seen.insert(parent.name()); - if (seen.size() != seenSize) // not seen before, so add - queue.append(parent); - } - } - } + return true; // continue + }); } } // Internal diff --git a/src/plugins/texteditor/highlighter.cpp b/src/plugins/texteditor/highlighter.cpp index 9c25925486d..07f5da83c5f 100644 --- a/src/plugins/texteditor/highlighter.cpp +++ b/src/plugins/texteditor/highlighter.cpp @@ -120,13 +120,16 @@ Highlighter::Definitions Highlighter::definitionsForDocument(const TextDocument if (definitions.isEmpty()) { const MimeType &mimeType = Utils::mimeTypeForName(document->mimeType()); if (mimeType.isValid()) { - // highlight definitions might not use the canonical name but an alias - const QStringList names = QStringList(mimeType.name()) + mimeType.aliases(); - for (const QString &name : names) { - definitions = definitionsForMimeType(name); - if (!definitions.isEmpty()) - break; - } + Utils::visitMimeParents(mimeType, [&](const MimeType &mt) -> bool { + // highlight definitions might not use the canonical name but an alias + const QStringList names = QStringList(mt.name()) + mt.aliases(); + for (const QString &name : names) { + definitions = definitionsForMimeType(name); + if (!definitions.isEmpty()) + return false; // stop + } + return true; // continue + }); } }