forked from qt-creator/qt-creator
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 <david.schulz@qt.io>
This commit is contained in:
@@ -100,4 +100,34 @@ void setMagicRulesForMimeType(const MimeType &mimeType, const QMap<int, QList<Mi
|
||||
d->setMagicRulesForMimeType(mimeType, rules);
|
||||
}
|
||||
|
||||
void visitMimeParents(const MimeType &mimeType,
|
||||
const std::function<bool(const MimeType &mimeType)> &visitor)
|
||||
{
|
||||
// search breadth-first through parent hierarchy, e.g. for hierarchy
|
||||
// * application/x-ruby
|
||||
// * application/x-executable
|
||||
// * application/octet-stream
|
||||
// * text/plain
|
||||
QList<MimeType> queue;
|
||||
QSet<QString> 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
|
||||
|
@@ -8,6 +8,8 @@
|
||||
#include <mimemagicrule_p.h>
|
||||
#include <mimetype.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
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<int, QList<MimeMagicRule>> &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<bool(const MimeType &mimeType)> &visitor);
|
||||
} // namespace Utils
|
||||
|
@@ -25,17 +25,7 @@ static void mimeTypeFactoryLookup(const Utils::MimeType &mimeType,
|
||||
QList<EditorTypeLike *> *list)
|
||||
{
|
||||
QSet<EditorTypeLike *> matches;
|
||||
// search breadth-first through parent hierarchy, e.g. for hierarchy
|
||||
// * application/x-ruby
|
||||
// * application/x-executable
|
||||
// * application/octet-stream
|
||||
// * text/plain
|
||||
QList<Utils::MimeType> queue;
|
||||
QSet<QString> 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
|
||||
|
@@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user