Simplify text format handling in syntax highlighters

Pass the mapping from custom enum to text style in form of a function,
which then can use a switch which is checked by compilers, and
avoids the need to lookup a different enum somewhere else to find
out what the mapping actually is.
That mapping is cached to keep performance as before.

Also, most highlighters created an enum just for the purpose of mapping
to text styles, basically creating duplicated subsets of text style like
enums everywhere. Instead provide a default, identity mapping from text
styles to text styles.

Change-Id: I2ea1ca702b99e36b8742dfda510b1b2753f0a1c2
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2017-05-05 20:34:29 +02:00
parent 762f67f729
commit cf57965ebc
21 changed files with 276 additions and 312 deletions

View File

@@ -34,6 +34,7 @@
#include "tabsettings.h"
#include <coreplugin/messagemanager.h>
#include <utils/qtcassert.h>
#include <QCoreApplication>
@@ -77,6 +78,46 @@ HighlighterCodeFormatterData *formatterData(const QTextBlock &block)
return data;
}
static TextStyle styleForFormat(int format)
{
const auto f = Highlighter::TextFormatId(format);
switch (f) {
case Highlighter::Normal: return C_TEXT;
case Highlighter::VisualWhitespace: return C_VISUAL_WHITESPACE;
case Highlighter::Keyword: return C_KEYWORD;
case Highlighter::DataType: return C_TYPE;
case Highlighter::Comment: return C_COMMENT;
case Highlighter::Decimal: return C_NUMBER;
case Highlighter::BaseN: return C_NUMBER;
case Highlighter::Float: return C_NUMBER;
case Highlighter::Char: return C_STRING;
case Highlighter::SpecialChar: return C_STRING;
case Highlighter::String: return C_STRING;
case Highlighter::Alert: return C_WARNING;
case Highlighter::Information: return C_TEXT;
case Highlighter::Warning: return C_WARNING;
case Highlighter::Error: return C_ERROR;
case Highlighter::Function: return C_FUNCTION;
case Highlighter::RegionMarker: return C_TEXT;
case Highlighter::BuiltIn: return C_PREPROCESSOR;
case Highlighter::Extension: return C_PRIMITIVE_TYPE;
case Highlighter::Operator: return C_OPERATOR;
case Highlighter::Variable: return C_LOCAL;
case Highlighter::Attribute: return C_LABEL;
case Highlighter::Annotation: return C_TEXT;
case Highlighter::CommentVar: return C_COMMENT;
case Highlighter::Import: return C_PREPROCESSOR;
case Highlighter::Others: return C_TEXT;
case Highlighter::Identifier: return C_LOCAL;
case Highlighter::Documentation: return C_DOXYGEN_COMMENT;
case Highlighter::TextFormatIdCount:
QTC_CHECK(false); // should never get here
return C_TEXT;
}
QTC_CHECK(false); // should never get here
return C_TEXT;
}
Highlighter::Highlighter(QTextDocument *parent) :
SyntaxHighlighter(parent),
m_regionDepth(0),
@@ -86,38 +127,7 @@ Highlighter::Highlighter(QTextDocument *parent) :
m_dynamicContextsCounter(0),
m_isBroken(false)
{
static const QVector<TextStyle> categories({
C_TEXT, // Normal
C_VISUAL_WHITESPACE, // VisualWhitespace
C_KEYWORD, // Keyword
C_TYPE, // DataType
C_COMMENT, // Comment
C_NUMBER, // Decimal
C_NUMBER, // BaseN
C_NUMBER, // Float
C_STRING, // Char
C_STRING, // SpecialChar
C_STRING, // String
C_WARNING, // Alert
C_TEXT, // Information
C_WARNING, // Warning
C_ERROR, // Error
C_FUNCTION, // Function
C_TEXT, // RegionMarker
C_PREPROCESSOR, // BuiltIn
C_PRIMITIVE_TYPE, // Extension
C_OPERATOR, // Operator
C_LOCAL, // Variable
C_LABEL, // Attribute
C_TEXT, // Annotation
C_COMMENT, // CommentVar
C_PREPROCESSOR, // Import
C_TEXT, // Others
C_LOCAL, // Identifier
C_DOXYGEN_COMMENT // Documentation
});
setTextFormatCategories(categories);
setTextFormatCategories(TextFormatIdCount, styleForFormat);
}
Highlighter::~Highlighter()