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

@@ -27,6 +27,7 @@
#include "profilecompletionassist.h"
#include <extensionsystem/pluginmanager.h>
#include <utils/qtcassert.h>
#include <QTextDocument>
@@ -35,11 +36,26 @@ using namespace TextEditor;
namespace QmakeProjectManager {
namespace Internal {
static TextStyle styleForFormat(int format)
{
const auto f = ProFileHighlighter::ProfileFormats(format);
switch (f) {
case ProFileHighlighter::ProfileVariableFormat: return C_TYPE;
case ProFileHighlighter::ProfileFunctionFormat: return C_KEYWORD;
case ProFileHighlighter::ProfileCommentFormat: return C_COMMENT;
case ProFileHighlighter::ProfileVisualWhitespaceFormat: return C_VISUAL_WHITESPACE;
case ProFileHighlighter::NumProfileFormats:
QTC_CHECK(false); // should never get here
return C_TEXT;
}
QTC_CHECK(false); // should never get here
return C_TEXT;
}
ProFileHighlighter::ProFileHighlighter(const Keywords &keywords)
: m_keywords(keywords)
{
static const QVector<TextStyle> categories({C_TYPE, C_KEYWORD, C_COMMENT, C_VISUAL_WHITESPACE});
setTextFormatCategories(categories);
setTextFormatCategories(NumProfileFormats, styleForFormat);
}
void ProFileHighlighter::highlightBlock(const QString &text)