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