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

@@ -42,10 +42,7 @@ QmlJSHighlighter::QmlJSHighlighter(QTextDocument *parent)
m_inMultilineComment(false)
{
m_currentBlockParentheses.reserve(20);
static const QVector<TextStyle> categories({
C_NUMBER, C_STRING, C_TYPE, C_KEYWORD, C_FIELD, C_COMMENT, C_VISUAL_WHITESPACE
});
setTextFormatCategories(categories);
setDefaultTextFormatCategories();
}
QmlJSHighlighter::~QmlJSHighlighter()
@@ -72,11 +69,11 @@ void QmlJSHighlighter::highlightBlock(const QString &text)
switch (token.kind) {
case Token::Keyword:
setFormat(token.offset, token.length, formatForCategory(KeywordFormat));
setFormat(token.offset, token.length, formatForCategory(C_KEYWORD));
break;
case Token::String:
setFormat(token.offset, token.length, formatForCategory(StringFormat));
setFormat(token.offset, token.length, formatForCategory(C_STRING));
break;
case Token::Comment:
@@ -89,11 +86,11 @@ void QmlJSHighlighter::highlightBlock(const QString &text)
onOpeningParenthesis(QLatin1Char('+'), token.offset, index == 0);
m_inMultilineComment = true;
}
setFormat(token.offset, token.length, formatForCategory(CommentFormat));
setFormat(token.offset, token.length, formatForCategory(C_COMMENT));
break;
case Token::RegExp:
setFormat(token.offset, token.length, formatForCategory(StringFormat));
setFormat(token.offset, token.length, formatForCategory(C_STRING));
break;
case Token::LeftParenthesis:
@@ -130,7 +127,7 @@ void QmlJSHighlighter::highlightBlock(const QString &text)
// check the previous token
if (index == 0 || tokens.at(index - 1).isNot(Token::Dot)) {
if (index + 1 == tokens.size() || tokens.at(index + 1).isNot(Token::Colon)) {
setFormat(token.offset, token.length, formatForCategory(KeywordFormat));
setFormat(token.offset, token.length, formatForCategory(C_KEYWORD));
break;
}
}
@@ -138,7 +135,7 @@ void QmlJSHighlighter::highlightBlock(const QString &text)
const Token &previousToken = tokens.at(index - 1);
if (previousToken.is(Token::Identifier) && text.at(previousToken.offset) == QLatin1Char('p')
&& text.midRef(previousToken.offset, previousToken.length) == QLatin1String("property")) {
setFormat(token.offset, token.length, formatForCategory(KeywordFormat));
setFormat(token.offset, token.length, formatForCategory(C_KEYWORD));
break;
}
}
@@ -157,7 +154,7 @@ void QmlJSHighlighter::highlightBlock(const QString &text)
int previousTokenEnd = 0;
for (int index = 0; index < tokens.size(); ++index) {
const Token &token = tokens.at(index);
setFormat(previousTokenEnd, token.begin() - previousTokenEnd, formatForCategory(VisualWhitespace));
setFormat(previousTokenEnd, token.begin() - previousTokenEnd, formatForCategory(C_VISUAL_WHITESPACE));
switch (token.kind) {
case Token::Comment:
@@ -171,7 +168,7 @@ void QmlJSHighlighter::highlightBlock(const QString &text)
do {
++i;
} while (i < e && text.at(i).isSpace());
setFormat(start, i - start, formatForCategory(VisualWhitespace));
setFormat(start, i - start, formatForCategory(C_VISUAL_WHITESPACE));
} else {
++i;
}
@@ -185,7 +182,7 @@ void QmlJSHighlighter::highlightBlock(const QString &text)
previousTokenEnd = token.end();
}
setFormat(previousTokenEnd, text.length() - previousTokenEnd, formatForCategory(VisualWhitespace));
setFormat(previousTokenEnd, text.length() - previousTokenEnd, formatForCategory(C_VISUAL_WHITESPACE));
setCurrentBlockState(m_scanner.state());
onBlockEnd(m_scanner.state());

View File

@@ -42,17 +42,6 @@ public:
QmlJSHighlighter(QTextDocument *parent = 0);
virtual ~QmlJSHighlighter();
enum {
NumberFormat,
StringFormat,
TypeFormat,
KeywordFormat,
FieldFormat,
CommentFormat,
VisualWhitespace,
NumFormats
};
bool isQmlEnabled() const;
void setQmlEnabled(bool duiEnabled);