Highlighter: Fix resolution of highlighting definition

We should try to find a definition based on the file path first, before
falling back to the MIME type, since MIME types are not extensible. So,
if we find a definition for a MIME type, there could be others, more
suited ones based on the file path.

Fixes: QTCREATORBUG-7906
Change-Id: I9b35efb26d287dd4c975a8944fca9a310c417394
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2020-03-24 09:17:57 +01:00
parent d0589d7f6e
commit 6989c9bbea

View File

@@ -118,22 +118,20 @@ Highlighter::Definition Highlighter::definitionForName(const QString &name)
Highlighter::Definitions Highlighter::definitionsForDocument(const TextDocument *document)
{
QTC_ASSERT(document, return {});
// First try to find definitions for the file path, only afterwards try the MIME type.
// An example where that is important is if there was a definition for "*.rb.xml", which
// cannot be referred to with a MIME type (since there is none), but there is the definition
// for XML files, which specifies a MIME type in addition to a glob pattern.
// If we check the MIME type first and then skip the pattern, the definition for "*.rb.xml" is
// never considered.
// The KSyntaxHighlighting CLI also completely ignores MIME types.
const Definitions &fileNameDefinitions = definitionsForFileName(document->filePath());
if (!fileNameDefinitions.isEmpty())
return fileNameDefinitions;
const Utils::MimeType &mimeType = Utils::mimeTypeForName(document->mimeType());
if (mimeType.isValid()) {
if (mimeType.name() == "text/plain") {
// text/plain is the base mime type for all text types so ignore it and try matching the
// file name against the pattern and only if no definition can be found for the
// file name try matching the mime type
const Definitions &fileNameDefinitions = definitionsForFileName(document->filePath());
if (!fileNameDefinitions.isEmpty())
return fileNameDefinitions;
return definitionsForMimeType(mimeType.name());
}
const Definitions &mimeTypeDefinitions = definitionsForMimeType(mimeType.name());
if (!mimeTypeDefinitions.isEmpty())
return mimeTypeDefinitions;
}
return definitionsForFileName(document->filePath());
if (!mimeType.isValid())
return fileNameDefinitions;
return definitionsForMimeType(mimeType.name());
}
static Highlighter::Definition definitionForSetting(const QString &settingsKey,