From 6989c9bbeae1403b8c1733bdae784841369cb8ce Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Tue, 24 Mar 2020 09:17:57 +0100 Subject: [PATCH] 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 --- src/plugins/texteditor/highlighter.cpp | 28 ++++++++++++-------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/plugins/texteditor/highlighter.cpp b/src/plugins/texteditor/highlighter.cpp index 07638bb8bcf..a81d66a3af9 100644 --- a/src/plugins/texteditor/highlighter.cpp +++ b/src/plugins/texteditor/highlighter.cpp @@ -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,