diff options
author | Eike Ziller <eike.ziller@qt.io> | 2017-05-05 20:34:29 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2017-05-23 07:47:22 +0000 |
commit | cf57965ebc8ec913a2b2fafe352ddae3f7706b98 (patch) | |
tree | 8e793cc8ed288fe1f3156a87613e18a7e6709bf3 /src/plugins/texteditor/generichighlighter | |
parent | 762f67f729ea01d21cb23646da69cbce89958789 (diff) | |
download | qt-creator-cf57965ebc8ec913a2b2fafe352ddae3f7706b98.tar.gz |
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>
Diffstat (limited to 'src/plugins/texteditor/generichighlighter')
-rw-r--r-- | src/plugins/texteditor/generichighlighter/highlighter.cpp | 74 | ||||
-rw-r--r-- | src/plugins/texteditor/generichighlighter/highlighter.h | 3 |
2 files changed, 44 insertions, 33 deletions
diff --git a/src/plugins/texteditor/generichighlighter/highlighter.cpp b/src/plugins/texteditor/generichighlighter/highlighter.cpp index d3c0b47d50..b2883da315 100644 --- a/src/plugins/texteditor/generichighlighter/highlighter.cpp +++ b/src/plugins/texteditor/generichighlighter/highlighter.cpp @@ -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() diff --git a/src/plugins/texteditor/generichighlighter/highlighter.h b/src/plugins/texteditor/generichighlighter/highlighter.h index 8b4e45201c..01d91af637 100644 --- a/src/plugins/texteditor/generichighlighter/highlighter.h +++ b/src/plugins/texteditor/generichighlighter/highlighter.h @@ -91,7 +91,8 @@ public: Import, Others, Identifier, - Documentation + Documentation, + TextFormatIdCount }; void setTabSettings(const TabSettings &ts); |