aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@qt.io>2017-05-05 20:34:29 +0200
committerEike Ziller <eike.ziller@qt.io>2017-05-23 07:47:22 +0000
commitcf57965ebc8ec913a2b2fafe352ddae3f7706b98 (patch)
tree8e793cc8ed288fe1f3156a87613e18a7e6709bf3
parent762f67f729ea01d21cb23646da69cbce89958789 (diff)
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>
-rw-r--r--src/libs/utils/algorithm.h15
-rw-r--r--src/plugins/cppeditor/cppeditorenums.h16
-rw-r--r--src/plugins/cppeditor/cpphighlighter.cpp52
-rw-r--r--src/plugins/git/githighlighters.cpp41
-rw-r--r--src/plugins/git/githighlighters.h3
-rw-r--r--src/plugins/glsleditor/glslhighlighter.cpp34
-rw-r--r--src/plugins/glsleditor/glslhighlighter.h16
-rw-r--r--src/plugins/mercurial/mercurialcommitwidget.cpp7
-rw-r--r--src/plugins/nim/editor/nimhighlighter.cpp62
-rw-r--r--src/plugins/nim/editor/nimhighlighter.h18
-rw-r--r--src/plugins/pythoneditor/pythonhighlighter.cpp42
-rw-r--r--src/plugins/qmakeprojectmanager/profilehighlighter.cpp20
-rw-r--r--src/plugins/qmljseditor/qmljshighlighter.cpp23
-rw-r--r--src/plugins/qmljseditor/qmljshighlighter.h11
-rw-r--r--src/plugins/texteditor/generichighlighter/highlighter.cpp74
-rw-r--r--src/plugins/texteditor/generichighlighter/highlighter.h3
-rw-r--r--src/plugins/texteditor/syntaxhighlighter.cpp47
-rw-r--r--src/plugins/texteditor/syntaxhighlighter.h8
-rw-r--r--src/plugins/vcsbase/baseannotationhighlighter.cpp11
-rw-r--r--src/plugins/vcsbase/diffandloghighlighter.cpp81
-rw-r--r--tests/auto/generichighlighter/highlighterengine/syntaxhighlighter.h4
21 files changed, 276 insertions, 312 deletions
diff --git a/src/libs/utils/algorithm.h b/src/libs/utils/algorithm.h
index 3a8bdbd84e..602d746574 100644
--- a/src/libs/utils/algorithm.h
+++ b/src/libs/utils/algorithm.h
@@ -153,6 +153,21 @@ decltype(auto) equal(R S::*member, T value)
return std::bind<bool>(std::equal_to<T>(), value, std::bind(member, std::placeholders::_1));
}
+//////////////////
+// max element
+//////////////////
+
+template<typename T>
+typename T::value_type maxElementOr(const T &container, typename T::value_type other)
+{
+ typename T::const_iterator end = container.end();
+ typename T::const_iterator begin = container.begin();
+
+ typename T::const_iterator it = std::max_element(begin, end);
+ if (it == end)
+ return other;
+ return *it;
+}
//////////////////
// transform
diff --git a/src/plugins/cppeditor/cppeditorenums.h b/src/plugins/cppeditor/cppeditorenums.h
index 909073c3ee..b11febefc9 100644
--- a/src/plugins/cppeditor/cppeditorenums.h
+++ b/src/plugins/cppeditor/cppeditorenums.h
@@ -33,21 +33,5 @@ enum FileType {
Source
};
-enum CppFormats {
- CppNumberFormat,
- CppStringFormat,
- CppTypeFormat,
- CppKeywordFormat,
- CppPrimitiveTypeFormat,
- CppOperatorFormat,
- CppPreprocessorFormat,
- CppLabelFormat,
- CppCommentFormat,
- CppDoxygenCommentFormat,
- CppDoxygenTagFormat,
- CppVisualWhitespace,
- NumCppFormats
-};
-
} // namespace Internal
} // namespace CppEditor
diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp
index 571d7ab9b1..020af2cf3e 100644
--- a/src/plugins/cppeditor/cpphighlighter.cpp
+++ b/src/plugins/cppeditor/cpphighlighter.cpp
@@ -42,21 +42,7 @@ using namespace CPlusPlus;
CppHighlighter::CppHighlighter(QTextDocument *document) :
SyntaxHighlighter(document)
{
- static const QVector<TextStyle> categories({
- C_NUMBER,
- C_STRING,
- C_TYPE,
- C_KEYWORD,
- C_PRIMITIVE_TYPE,
- C_OPERATOR,
- C_PREPROCESSOR,
- C_LABEL,
- C_COMMENT,
- C_DOXYGEN_COMMENT,
- C_DOXYGEN_TAG,
- C_VISUAL_WHITESPACE
- });
- setTextFormatCategories(categories);
+ setDefaultTextFormatCategories();
}
void CppHighlighter::highlightBlock(const QString &text)
@@ -90,11 +76,11 @@ void CppHighlighter::highlightBlock(const QString &text)
TextDocumentLayout::clearParentheses(currentBlock());
if (text.length()) {// the empty line can still contain whitespace
if (initialLexerState == T_COMMENT)
- highlightLine(text, 0, text.length(), formatForCategory(CppCommentFormat));
+ highlightLine(text, 0, text.length(), formatForCategory(C_COMMENT));
else if (initialLexerState == T_DOXY_COMMENT)
- highlightLine(text, 0, text.length(), formatForCategory(CppDoxygenCommentFormat));
+ highlightLine(text, 0, text.length(), formatForCategory(C_DOXYGEN_COMMENT));
else
- setFormat(0, text.length(), formatForCategory(CppVisualWhitespace));
+ setFormat(0, text.length(), formatForCategory(C_VISUAL_WHITESPACE));
}
TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
return;
@@ -121,7 +107,7 @@ void CppHighlighter::highlightBlock(const QString &text)
if (previousTokenEnd != tk.utf16charsBegin()) {
setFormat(previousTokenEnd,
tk.utf16charsBegin() - previousTokenEnd,
- formatForCategory(CppVisualWhitespace));
+ formatForCategory(C_VISUAL_WHITESPACE));
}
if (tk.is(T_LPAREN) || tk.is(T_LBRACE) || tk.is(T_LBRACKET)) {
@@ -162,12 +148,12 @@ void CppHighlighter::highlightBlock(const QString &text)
if (i == 0 && tk.is(T_POUND)) {
highlightLine(text, tk.utf16charsBegin(), tk.utf16chars(),
- formatForCategory(CppPreprocessorFormat));
+ formatForCategory(C_PREPROCESSOR));
expectPreprocessorKeyword = true;
} else if (highlightCurrentWordAsPreprocessor
&& (tk.isKeyword() || tk.is(T_IDENTIFIER))
&& isPPKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars()))) {
- setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppPreprocessorFormat));
+ setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(C_PREPROCESSOR));
const QStringRef ppKeyword = text.midRef(tk.utf16charsBegin(), tk.utf16chars());
if (ppKeyword == QLatin1String("error")
|| ppKeyword == QLatin1String("warning")
@@ -176,14 +162,14 @@ void CppHighlighter::highlightBlock(const QString &text)
}
} else if (tk.is(T_NUMERIC_LITERAL)) {
- setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppNumberFormat));
+ setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(C_NUMBER));
} else if (tk.isStringLiteral() || tk.isCharLiteral()) {
- highlightLine(text, tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppStringFormat));
+ highlightLine(text, tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(C_STRING));
} else if (tk.isComment()) {
const int startPosition = initialLexerState ? previousTokenEnd : tk.utf16charsBegin();
if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT)) {
highlightLine(text, startPosition, tk.utf16charsEnd() - startPosition,
- formatForCategory(CppCommentFormat));
+ formatForCategory(C_COMMENT));
}
else // a doxygen comment
@@ -211,14 +197,14 @@ void CppHighlighter::highlightBlock(const QString &text)
|| (m_languageFeatures.qtKeywordsEnabled
&& CppTools::isQtKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars())))
|| (m_languageFeatures.objCEnabled && tk.isObjCAtKeyword())) {
- setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppKeywordFormat));
+ setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(C_KEYWORD));
} else if (tk.isPrimitiveType()) {
setFormat(tk.utf16charsBegin(), tk.utf16chars(),
- formatForCategory(CppPrimitiveTypeFormat));
+ formatForCategory(C_PRIMITIVE_TYPE));
} else if (tk.isOperator()) {
- setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppOperatorFormat));
+ setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(C_OPERATOR));
} else if (i == 0 && tokens.size() > 1 && tk.is(T_IDENTIFIER) && tokens.at(1).is(T_COLON)) {
- setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppLabelFormat));
+ setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(C_LABEL));
} else if (tk.is(T_IDENTIFIER)) {
highlightWord(text.midRef(tk.utf16charsBegin(), tk.utf16chars()), tk.utf16charsBegin(),
tk.utf16chars());
@@ -228,7 +214,7 @@ void CppHighlighter::highlightBlock(const QString &text)
// mark the trailing white spaces
const int lastTokenEnd = tokens.last().utf16charsEnd();
if (text.length() > lastTokenEnd)
- highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, formatForCategory(CppVisualWhitespace));
+ highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, formatForCategory(C_VISUAL_WHITESPACE));
if (!initialLexerState && lexerState && !tokens.isEmpty()) {
const Token &lastToken = tokens.last();
@@ -360,7 +346,7 @@ bool CppHighlighter::isPPKeyword(const QStringRef &text) const
void CppHighlighter::highlightLine(const QString &text, int position, int length,
const QTextCharFormat &format)
{
- QTextCharFormat visualSpaceFormat = formatForCategory(CppVisualWhitespace);
+ QTextCharFormat visualSpaceFormat = formatForCategory(C_VISUAL_WHITESPACE);
visualSpaceFormat.setBackground(format.background());
const int end = position + length;
@@ -394,7 +380,7 @@ void CppHighlighter::highlightWord(QStringRef word, int position, int length)
return;
}
- setFormat(position, length, formatForCategory(CppTypeFormat));
+ setFormat(position, length, formatForCategory(C_TYPE));
}
}
}
@@ -406,8 +392,8 @@ void CppHighlighter::highlightDoxygenComment(const QString &text, int position,
const QChar *uc = text.unicode();
const QChar *it = uc + position;
- const QTextCharFormat &format = formatForCategory(CppDoxygenCommentFormat);
- const QTextCharFormat &kwFormat = formatForCategory(CppDoxygenTagFormat);
+ const QTextCharFormat &format = formatForCategory(C_DOXYGEN_COMMENT);
+ const QTextCharFormat &kwFormat = formatForCategory(C_DOXYGEN_TAG);
while (!it->isNull()) {
if (it->unicode() == QLatin1Char('\\') ||
diff --git a/src/plugins/git/githighlighters.cpp b/src/plugins/git/githighlighters.cpp
index f6cea509ec..fbc72dae95 100644
--- a/src/plugins/git/githighlighters.cpp
+++ b/src/plugins/git/githighlighters.cpp
@@ -38,9 +38,7 @@ static const char CHANGE_PATTERN[] = "\\b[a-f0-9]{7,40}\\b";
GitSubmitHighlighter::GitSubmitHighlighter(QTextEdit * parent) :
TextEditor::SyntaxHighlighter(parent)
{
- static const QVector<TextEditor::TextStyle> categories({TextEditor::C_COMMENT});
-
- setTextFormatCategories(categories);
+ setDefaultTextFormatCategories();
m_keywordPattern.setPattern("^[\\w-]+:");
m_hashChar = '#';
QTC_CHECK(m_keywordPattern.isValid());
@@ -56,7 +54,7 @@ void GitSubmitHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(state);
return;
} else if (text.startsWith(m_hashChar)) {
- setFormat(0, text.size(), formatForCategory(Format_Comment));
+ setFormat(0, text.size(), formatForCategory(TextEditor::C_COMMENT));
setCurrentBlockState(state);
return;
} else if (state == None) {
@@ -92,23 +90,34 @@ GitRebaseHighlighter::RebaseAction::RebaseAction(const QString &regexp,
{
}
+static TextEditor::TextStyle styleForFormat(int format)
+{
+ using namespace TextEditor;
+ const auto f = Format(format);
+ switch (f) {
+ case Format_Comment: return C_COMMENT;
+ case Format_Change: return C_DOXYGEN_COMMENT;
+ case Format_Description: return C_STRING;
+ case Format_Pick: return C_KEYWORD;
+ case Format_Reword: return C_FIELD;
+ case Format_Edit: return C_TYPE;
+ case Format_Squash: return C_ENUMERATION;
+ case Format_Fixup: return C_NUMBER;
+ case Format_Exec: return C_LABEL;
+ case Format_Count:
+ QTC_CHECK(false); // should never get here
+ return C_TEXT;
+ }
+ QTC_CHECK(false); // should never get here
+ return C_TEXT;
+}
+
GitRebaseHighlighter::GitRebaseHighlighter(QTextDocument *parent) :
TextEditor::SyntaxHighlighter(parent),
m_hashChar('#'),
m_changeNumberPattern(CHANGE_PATTERN)
{
- static const QVector<TextEditor::TextStyle> categories({
- TextEditor::C_COMMENT,
- TextEditor::C_DOXYGEN_COMMENT,
- TextEditor::C_STRING,
- TextEditor::C_KEYWORD,
- TextEditor::C_FIELD,
- TextEditor::C_TYPE,
- TextEditor::C_ENUMERATION,
- TextEditor::C_NUMBER,
- TextEditor::C_LABEL
- });
- setTextFormatCategories(categories);
+ setTextFormatCategories(Format_Count, styleForFormat);
m_actions << RebaseAction("^(p|pick)\\b", Format_Pick);
m_actions << RebaseAction("^(r|reword)\\b", Format_Reword);
diff --git a/src/plugins/git/githighlighters.h b/src/plugins/git/githighlighters.h
index 16325423bf..055d3721ee 100644
--- a/src/plugins/git/githighlighters.h
+++ b/src/plugins/git/githighlighters.h
@@ -41,7 +41,8 @@ enum Format {
Format_Edit,
Format_Squash,
Format_Fixup,
- Format_Exec
+ Format_Exec,
+ Format_Count
};
// Highlighter for git submit messages. Make the first line bold, indicates
diff --git a/src/plugins/glsleditor/glslhighlighter.cpp b/src/plugins/glsleditor/glslhighlighter.cpp
index 23f9c9d550..1faf60d2c0 100644
--- a/src/plugins/glsleditor/glslhighlighter.cpp
+++ b/src/plugins/glsleditor/glslhighlighter.cpp
@@ -34,26 +34,14 @@
using namespace TextEditor;
+const TextStyle GLSLReservedKeyword = C_REMOVED_LINE;
+
namespace GlslEditor {
namespace Internal {
GlslHighlighter::GlslHighlighter()
{
- static const QVector<TextStyle> categories({
- C_NUMBER,
- C_STRING,
- C_TYPE,
- C_KEYWORD,
- C_OPERATOR,
- C_PREPROCESSOR,
- C_LABEL,
- C_COMMENT,
- C_DOXYGEN_COMMENT,
- C_DOXYGEN_TAG,
- C_VISUAL_WHITESPACE,
- C_REMOVED_LINE
- });
- setTextFormatCategories(categories);
+ setDefaultTextFormatCategories();
}
void GlslHighlighter::highlightBlock(const QString &text)
@@ -99,7 +87,7 @@ void GlslHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(previousState);
TextDocumentLayout::clearParentheses(currentBlock());
if (text.length()) // the empty line can still contain whitespace
- setFormat(0, text.length(), formatForCategory(GLSLVisualWhitespace));
+ setFormat(0, text.length(), formatForCategory(C_VISUAL_WHITESPACE));
TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
return;
}
@@ -123,7 +111,7 @@ void GlslHighlighter::highlightBlock(const QString &text)
if (previousTokenEnd != tk.begin()) {
setFormat(previousTokenEnd, tk.begin() - previousTokenEnd,
- formatForCategory(GLSLVisualWhitespace));
+ formatForCategory(C_VISUAL_WHITESPACE));
}
if (tk.is(GLSL::Parser::T_LEFT_PAREN) || tk.is(GLSL::Parser::T_LEFT_BRACE) || tk.is(GLSL::Parser::T_LEFT_BRACKET)) {
@@ -160,17 +148,17 @@ void GlslHighlighter::highlightBlock(const QString &text)
highlightAsPreprocessor = false;
if (false /* && i == 0 && tk.is(GLSL::Parser::T_POUND)*/) {
- highlightLine(text, tk.begin(), tk.length, formatForCategory(GLSLPreprocessorFormat));
+ highlightLine(text, tk.begin(), tk.length, formatForCategory(C_PREPROCESSOR));
highlightAsPreprocessor = true;
} else if (highlightCurrentWordAsPreprocessor && isPPKeyword(text.midRef(tk.begin(), tk.length))) {
- setFormat(tk.begin(), tk.length, formatForCategory(GLSLPreprocessorFormat));
+ setFormat(tk.begin(), tk.length, formatForCategory(C_PREPROCESSOR));
} else if (tk.is(GLSL::Parser::T_NUMBER)) {
- setFormat(tk.begin(), tk.length, formatForCategory(GLSLNumberFormat));
+ setFormat(tk.begin(), tk.length, formatForCategory(C_NUMBER));
} else if (tk.is(GLSL::Parser::T_COMMENT)) {
- highlightLine(text, tk.begin(), tk.length, formatForCategory(GLSLCommentFormat));
+ highlightLine(text, tk.begin(), tk.length, formatForCategory(C_COMMENT));
// we need to insert a close comment parenthesis, if
// - the line starts in a C Comment (initalState != 0)
@@ -195,7 +183,7 @@ void GlslHighlighter::highlightBlock(const QString &text)
if (kind == GLSL::Parser::T_RESERVED)
setFormat(tk.position, tk.length, formatForCategory(GLSLReservedKeyword));
else if (kind != GLSL::Parser::T_IDENTIFIER)
- setFormat(tk.position, tk.length, formatForCategory(GLSLKeywordFormat));
+ setFormat(tk.position, tk.length, formatForCategory(C_KEYWORD));
}
}
@@ -248,7 +236,7 @@ void GlslHighlighter::highlightBlock(const QString &text)
void GlslHighlighter::highlightLine(const QString &text, int position, int length,
const QTextCharFormat &format)
{
- const QTextCharFormat visualSpaceFormat = formatForCategory(GLSLVisualWhitespace);
+ const QTextCharFormat visualSpaceFormat = formatForCategory(C_VISUAL_WHITESPACE);
const int end = position + length;
int index = position;
diff --git a/src/plugins/glsleditor/glslhighlighter.h b/src/plugins/glsleditor/glslhighlighter.h
index afbe882a41..0a8215c0f5 100644
--- a/src/plugins/glsleditor/glslhighlighter.h
+++ b/src/plugins/glsleditor/glslhighlighter.h
@@ -35,22 +35,6 @@ class GlslHighlighter : public TextEditor::SyntaxHighlighter
Q_OBJECT
public:
- enum Formats {
- GLSLNumberFormat,
- GLSLStringFormat,
- GLSLTypeFormat,
- GLSLKeywordFormat,
- GLSLOperatorFormat,
- GLSLPreprocessorFormat,
- GLSLLabelFormat,
- GLSLCommentFormat,
- GLSLDoxygenCommentFormat,
- GLSLDoxygenTagFormat,
- GLSLVisualWhitespace,
- GLSLReservedKeyword,
- NumGLSLFormats
- };
-
GlslHighlighter();
protected:
diff --git a/src/plugins/mercurial/mercurialcommitwidget.cpp b/src/plugins/mercurial/mercurialcommitwidget.cpp
index c8d8f262dc..231badd04a 100644
--- a/src/plugins/mercurial/mercurialcommitwidget.cpp
+++ b/src/plugins/mercurial/mercurialcommitwidget.cpp
@@ -52,7 +52,6 @@ public:
private:
enum State { None = -1, Header, Other };
- enum Format { Format_Comment };
QRegExp m_keywordPattern;
};
@@ -60,10 +59,8 @@ MercurialSubmitHighlighter::MercurialSubmitHighlighter(QTextEdit *parent) :
TextEditor::SyntaxHighlighter(parent),
m_keywordPattern(QLatin1String("^\\w+:"))
{
- static const QVector<TextEditor::TextStyle> categories({TextEditor::C_COMMENT});
-
- setTextFormatCategories(categories);
QTC_CHECK(m_keywordPattern.isValid());
+ setDefaultTextFormatCategories();
}
void MercurialSubmitHighlighter::highlightBlock(const QString &text)
@@ -71,7 +68,7 @@ void MercurialSubmitHighlighter::highlightBlock(const QString &text)
// figure out current state
State state = static_cast<State>(previousBlockState());
if (text.startsWith(QLatin1String("HG:"))) {
- setFormat(0, text.size(), formatForCategory(Format_Comment));
+ setFormat(0, text.size(), formatForCategory(TextEditor::C_COMMENT));
setCurrentBlockState(state);
return;
}
diff --git a/src/plugins/nim/editor/nimhighlighter.cpp b/src/plugins/nim/editor/nimhighlighter.cpp
index f9cb055a0d..cc74d9c628 100644
--- a/src/plugins/nim/editor/nimhighlighter.cpp
+++ b/src/plugins/nim/editor/nimhighlighter.cpp
@@ -35,7 +35,7 @@ namespace Nim {
NimHighlighter::NimHighlighter()
{
- initTextFormats();
+ setDefaultTextFormatCategories();
}
void NimHighlighter::highlightBlock(const QString &text)
@@ -43,56 +43,36 @@ void NimHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(highlightLine(text, previousBlockState()));
}
-void NimHighlighter::initTextFormats()
-{
- static QMap<Category, TextEditor::TextStyle> categoryStyle = {
- {TextCategory, TextEditor::C_TEXT},
- {KeywordCategory, TextEditor::C_KEYWORD},
- {CommentCategory, TextEditor::C_COMMENT},
- {DocumentationCategory, TextEditor::C_DOXYGEN_COMMENT},
- {TypeCategory, TextEditor::C_TYPE},
- {StringCategory, TextEditor::C_STRING},
- {NumberCategory, TextEditor::C_NUMBER},
- {OperatorCategory, TextEditor::C_OPERATOR},
- {FunctionCategory, TextEditor::C_FUNCTION},
- };
-
- QVector<TextEditor::TextStyle> formats;
- for (const auto &category : categoryStyle.keys())
- formats << categoryStyle[category];
- setTextFormatCategories(formats);
-}
-
-NimHighlighter::Category NimHighlighter::categoryForToken(const NimLexer::Token &token,
- const QString &tokenValue)
+TextEditor::TextStyle NimHighlighter::styleForToken(const NimLexer::Token &token,
+ const QString &tokenValue)
{
switch (token.type) {
case NimLexer::TokenType::Keyword:
- return KeywordCategory;
+ return TextEditor::C_KEYWORD;
case NimLexer::TokenType::Identifier:
- return categoryForIdentifier(token, tokenValue);
+ return styleForIdentifier(token, tokenValue);
case NimLexer::TokenType::Comment:
- return CommentCategory;
+ return TextEditor::C_COMMENT;
case NimLexer::TokenType::Documentation:
- return DocumentationCategory;
+ return TextEditor::C_DOXYGEN_COMMENT;
case NimLexer::TokenType::StringLiteral:
- return StringCategory;
+ return TextEditor::C_STRING;
case NimLexer::TokenType::MultiLineStringLiteral:
- return StringCategory;
+ return TextEditor::C_STRING;
case NimLexer::TokenType::Operator:
- return OperatorCategory;
+ return TextEditor::C_OPERATOR;
case NimLexer::TokenType::Number:
- return NumberCategory;
+ return TextEditor::C_NUMBER;
default:
- return TextCategory;
+ return TextEditor::C_TEXT;
}
}
-NimHighlighter::Category NimHighlighter::categoryForIdentifier(const NimLexer::Token &token,
- const QString &tokenValue)
+TextEditor::TextStyle NimHighlighter::styleForIdentifier(const NimLexer::Token &token,
+ const QString &tokenValue)
{
Q_UNUSED(token)
- QTC_ASSERT(token.type == NimLexer::TokenType::Identifier, return TextCategory);
+ QTC_ASSERT(token.type == NimLexer::TokenType::Identifier, return TextEditor::C_TEXT);
static QSet<QString> nimBuiltInValues {
"true", "false"
@@ -111,12 +91,12 @@ NimHighlighter::Category NimHighlighter::categoryForIdentifier(const NimLexer::T
};
if (nimBuiltInFunctions.contains(tokenValue))
- return TypeCategory;
+ return TextEditor::C_TYPE;
if (nimBuiltInValues.contains(tokenValue))
- return KeywordCategory;
+ return TextEditor::C_KEYWORD;
if (nimBuiltInTypes.contains(tokenValue))
- return TypeCategory;
- return TextCategory;
+ return TextEditor::C_TYPE;
+ return TextEditor::C_TEXT;
}
int NimHighlighter::highlightLine(const QString &text, int initialState)
@@ -127,8 +107,8 @@ int NimHighlighter::highlightLine(const QString &text, int initialState)
NimLexer::Token tk;
while ((tk = lexer.next()).type != NimLexer::TokenType::EndOfText) {
- int category = categoryForToken(tk, text.mid(tk.begin, tk.length));
- setFormat(tk.begin, tk.length, formatForCategory(category));
+ TextEditor::TextStyle style = styleForToken(tk, text.mid(tk.begin, tk.length));
+ setFormat(tk.begin, tk.length, formatForCategory(style));
}
return lexer.state();
diff --git a/src/plugins/nim/editor/nimhighlighter.h b/src/plugins/nim/editor/nimhighlighter.h
index 2ae1003325..fabf961c42 100644
--- a/src/plugins/nim/editor/nimhighlighter.h
+++ b/src/plugins/nim/editor/nimhighlighter.h
@@ -35,18 +35,6 @@ class NimHighlighter : public TextEditor::SyntaxHighlighter
{
Q_OBJECT
- enum Category {
- TextCategory = 0,
- KeywordCategory,
- CommentCategory,
- DocumentationCategory,
- TypeCategory,
- StringCategory,
- NumberCategory,
- OperatorCategory,
- FunctionCategory
- };
-
public:
NimHighlighter();
@@ -54,10 +42,8 @@ protected:
void highlightBlock(const QString &text) override;
private:
- void initTextFormats();
-
- Category categoryForToken(const NimLexer::Token &token, const QString &tokenValue);
- Category categoryForIdentifier(const NimLexer::Token &token, const QString &tokenValue);
+ TextEditor::TextStyle styleForToken(const NimLexer::Token &token, const QString &tokenValue);
+ TextEditor::TextStyle styleForIdentifier(const NimLexer::Token &token, const QString &tokenValue);
int highlightLine(const QString &text, int initialState);
};
diff --git a/src/plugins/pythoneditor/pythonhighlighter.cpp b/src/plugins/pythoneditor/pythonhighlighter.cpp
index dcc5291b64..cd3c489e74 100644
--- a/src/plugins/pythoneditor/pythonhighlighter.cpp
+++ b/src/plugins/pythoneditor/pythonhighlighter.cpp
@@ -38,6 +38,7 @@
#include <texteditor/textdocument.h>
#include <texteditor/texteditorconstants.h>
+#include <utils/qtcassert.h>
namespace PythonEditor {
namespace Internal {
@@ -60,23 +61,34 @@ namespace Internal {
* @endcode
*/
+static TextEditor::TextStyle styleForFormat(int format)
+{
+ using namespace TextEditor;
+ const auto f = Format(format);
+ switch (f) {
+ case Format_Number: return C_NUMBER;
+ case Format_String: return C_STRING;
+ case Format_Keyword: return C_KEYWORD;
+ case Format_Type: return C_TYPE;
+ case Format_ClassField: return C_FIELD;
+ case Format_MagicAttr: return C_JS_SCOPE_VAR;
+ case Format_Operator: return C_OPERATOR;
+ case Format_Comment: return C_COMMENT;
+ case Format_Doxygen: return C_DOXYGEN_COMMENT;
+ case Format_Identifier: return C_TEXT;
+ case Format_Whitespace: return C_VISUAL_WHITESPACE;
+ case Format_ImportedModule: return C_STRING;
+ case Format_FormatsAmount:
+ QTC_CHECK(false); // should never get here
+ return C_TEXT;
+ }
+ QTC_CHECK(false); // should never get here
+ return C_TEXT;
+}
+
PythonHighlighter::PythonHighlighter()
{
- static const QVector<TextEditor::TextStyle> categories = {
- TextEditor::C_NUMBER,
- TextEditor::C_STRING,
- TextEditor::C_KEYWORD,
- TextEditor::C_TYPE,
- TextEditor::C_FIELD,
- TextEditor::C_JS_SCOPE_VAR,
- TextEditor::C_OPERATOR,
- TextEditor::C_COMMENT,
- TextEditor::C_DOXYGEN_COMMENT,
- TextEditor::C_TEXT,
- TextEditor::C_VISUAL_WHITESPACE,
- TextEditor::C_STRING
- };
- setTextFormatCategories(categories);
+ setTextFormatCategories(Format_FormatsAmount, styleForFormat);
}
/**
diff --git a/src/plugins/qmakeprojectmanager/profilehighlighter.cpp b/src/plugins/qmakeprojectmanager/profilehighlighter.cpp
index c93d7af9d7..57f7937f03 100644
--- a/src/plugins/qmakeprojectmanager/profilehighlighter.cpp
+++ b/src/plugins/qmakeprojectmanager/profilehighlighter.cpp
@@ -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)
diff --git a/src/plugins/qmljseditor/qmljshighlighter.cpp b/src/plugins/qmljseditor/qmljshighlighter.cpp
index 2a34509ae8..d494f4e5e5 100644
--- a/src/plugins/qmljseditor/qmljshighlighter.cpp
+++ b/src/plugins/qmljseditor/qmljshighlighter.cpp
@@ -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());
diff --git a/src/plugins/qmljseditor/qmljshighlighter.h b/src/plugins/qmljseditor/qmljshighlighter.h
index 1b221aa181..c0a034707b 100644
--- a/src/plugins/qmljseditor/qmljshighlighter.h
+++ b/src/plugins/qmljseditor/qmljshighlighter.h
@@ -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);
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);
diff --git a/src/plugins/texteditor/syntaxhighlighter.cpp b/src/plugins/texteditor/syntaxhighlighter.cpp
index 952dc25611..05c7ee453c 100644
--- a/src/plugins/texteditor/syntaxhighlighter.cpp
+++ b/src/plugins/texteditor/syntaxhighlighter.cpp
@@ -30,6 +30,7 @@
#include "fontsettings.h"
#include <utils/algorithm.h>
+#include <utils/asconst.h>
#include <utils/qtcassert.h>
#include <QTextDocument>
@@ -71,7 +72,7 @@ public:
bool inReformatBlocks;
TextDocumentLayout::FoldValidator foldValidator;
QVector<QTextCharFormat> formats;
- QVector<TextStyle> formatCategories;
+ QVector<std::pair<int,TextStyle>> formatCategories;
};
static bool adjustRange(QTextLayout::FormatRange &range, int from, int charsRemoved, int charsAdded) {
@@ -727,10 +728,49 @@ void SyntaxHighlighter::setFontSettings(const FontSettings &fontSettings)
d->updateFormatsForCategories(fontSettings);
}
-void SyntaxHighlighter::setTextFormatCategories(const QVector<TextStyle> &categories)
+/*!
+ Creates text format categories for the text styles themselves, so the highlighter can
+ use \c{formatForCategory(C_COMMENT)} and similar, and avoid creating its own format enum.
+ \sa setTextFormatCategories()
+*/
+void SyntaxHighlighter::setDefaultTextFormatCategories()
+{
+ // map all text styles to themselves
+ setTextFormatCategories(C_LAST_STYLE_SENTINEL, [](int i) { return TextStyle(i); });
+}
+
+/*!
+ Uses the \a formatMapping function to create a mapping from the custom formats (the ints)
+ to text styles. The \a formatMapping must handle all values from 0 to \a count.
+
+ \sa setDefaultTextFormatCategories()
+ \sa setTextFormatCategories()
+*/
+void SyntaxHighlighter::setTextFormatCategories(int count,
+ std::function<TextStyle(int)> formatMapping)
+{
+ QVector<std::pair<int, TextStyle>> categories;
+ categories.reserve(count);
+ for (int i = 0; i < count; ++i)
+ categories.append({i, formatMapping(i)});
+ setTextFormatCategories(categories);
+}
+
+/*!
+ Creates a mapping between custom format enum values (the int values in the pairs) to
+ text styles. Afterwards \c{formatForCategory(MyCustomFormatEnumValue)} can be used to
+ efficiently retrieve the text style for a value.
+
+ Note that this creates a vector with a size of the maximum int value in \a categories.
+
+ \sa setDefaultTextFormatCategories()
+*/
+void SyntaxHighlighter::setTextFormatCategories(const QVector<std::pair<int, TextStyle>> &categories)
{
Q_D(SyntaxHighlighter);
d->formatCategories = categories;
+ const int maxCategory = Utils::maxElementOr(categories, {-1, C_TEXT}).first;
+ d->formats = QVector<QTextCharFormat>(maxCategory + 1);
d->updateFormatsForCategories(TextEditorSettings::fontSettings());
}
@@ -744,7 +784,8 @@ QTextCharFormat SyntaxHighlighter::formatForCategory(int category) const
void SyntaxHighlighterPrivate::updateFormatsForCategories(const FontSettings &fontSettings)
{
- formats = fontSettings.toTextCharFormats(formatCategories);
+ for (const auto &pair : Utils::asConst(formatCategories))
+ formats[pair.first] = fontSettings.toTextCharFormat(pair.second);
}
} // namespace TextEditor
diff --git a/src/plugins/texteditor/syntaxhighlighter.h b/src/plugins/texteditor/syntaxhighlighter.h
index 064c5aaa77..35c75b50f3 100644
--- a/src/plugins/texteditor/syntaxhighlighter.h
+++ b/src/plugins/texteditor/syntaxhighlighter.h
@@ -26,10 +26,14 @@
#pragma once
#include "texteditor_global.h"
+
#include <texteditor/texteditorconstants.h>
+
#include <QObject>
#include <QTextLayout>
+#include <functional>
+
QT_BEGIN_NAMESPACE
class QTextDocument;
class QSyntaxHighlighterPrivate;
@@ -70,7 +74,8 @@ public slots:
void rehighlightBlock(const QTextBlock &block);
protected:
- void setTextFormatCategories(const QVector<TextEditor::TextStyle> &categories);
+ void setDefaultTextFormatCategories();
+ void setTextFormatCategories(int count, std::function<TextStyle(int)> formatMapping);
QTextCharFormat formatForCategory(int categoryIndex) const;
virtual void highlightBlock(const QString &text) = 0;
@@ -91,6 +96,7 @@ protected:
QTextBlock currentBlock() const;
private:
+ void setTextFormatCategories(const QVector<std::pair<int, TextStyle>> &categories);
void reformatBlocks(int from, int charsRemoved, int charsAdded);
void delayedRehighlight();
diff --git a/src/plugins/vcsbase/baseannotationhighlighter.cpp b/src/plugins/vcsbase/baseannotationhighlighter.cpp
index 389510fb20..461dfe74bb 100644
--- a/src/plugins/vcsbase/baseannotationhighlighter.cpp
+++ b/src/plugins/vcsbase/baseannotationhighlighter.cpp
@@ -52,10 +52,6 @@ namespace VcsBase {
class BaseAnnotationHighlighterPrivate
{
public:
- enum Formats {
- BackgroundFormat // C_TEXT
- };
-
BaseAnnotationHighlighterPrivate(BaseAnnotationHighlighter *q_) : q(q_) { }
void updateOtherFormats();
@@ -67,7 +63,8 @@ public:
void BaseAnnotationHighlighterPrivate::updateOtherFormats()
{
- m_background = q->formatForCategory(BackgroundFormat).brushProperty(QTextFormat::BackgroundBrush).color();
+ m_background = q->formatForCategory(TextEditor::C_TEXT)
+ .brushProperty(QTextFormat::BackgroundBrush).color();
q->setChangeNumbers(m_changeNumberMap.keys().toSet());
}
@@ -76,9 +73,7 @@ BaseAnnotationHighlighter::BaseAnnotationHighlighter(const ChangeNumbers &change
TextEditor::SyntaxHighlighter(document),
d(new BaseAnnotationHighlighterPrivate(this))
{
- static const QVector<TextEditor::TextStyle> categories({TextEditor::C_TEXT});
-
- setTextFormatCategories(categories);
+ setDefaultTextFormatCategories();
d->updateOtherFormats();
setChangeNumbers(changeNumbers);
diff --git a/src/plugins/vcsbase/diffandloghighlighter.cpp b/src/plugins/vcsbase/diffandloghighlighter.cpp
index f3c5b910e8..50b17d8f83 100644
--- a/src/plugins/vcsbase/diffandloghighlighter.cpp
+++ b/src/plugins/vcsbase/diffandloghighlighter.cpp
@@ -67,16 +67,6 @@ static const int LOCATION_LEVEL = 2;
namespace VcsBase {
namespace Internal {
-// Formats used by DiffAndLogHighlighter
-enum DiffFormats {
- DiffTextFormat,
- DiffInFormat,
- DiffOutFormat,
- DiffFileFormat,
- DiffLocationFormat,
- ChangeTextFormat
-};
-
enum FoldingState {
StartOfFile,
Header,
@@ -111,7 +101,7 @@ public:
QTC_CHECK(filePattern.isValid());
}
- Internal::DiffFormats analyzeLine(const QString &block) const;
+ TextEditor::TextStyle analyzeLine(const QString &block) const;
void updateOtherFormats();
DiffAndLogHighlighter *const q;
@@ -126,27 +116,27 @@ public:
Internal::FoldingState m_foldingState;
};
-Internal::DiffFormats DiffAndLogHighlighterPrivate::analyzeLine(const QString &text) const
+TextEditor::TextStyle DiffAndLogHighlighterPrivate::analyzeLine(const QString &text) const
{
// Do not match on git "--- a/" as a deleted line, check
// file first
if (m_filePattern.indexIn(text) == 0)
- return Internal::DiffFileFormat;
+ return TextEditor::C_DIFF_FILE;
if (m_changePattern.indexIn(text) == 0)
- return Internal::ChangeTextFormat;
+ return TextEditor::C_LOG_CHANGE_LINE;
if (text.startsWith(m_diffInIndicator))
- return Internal::DiffInFormat;
+ return TextEditor::C_ADDED_LINE;
if (text.startsWith(m_diffOutIndicator))
- return Internal::DiffOutFormat;
+ return TextEditor::C_REMOVED_LINE;
if (text.startsWith(m_locationIndicator))
- return Internal::DiffLocationFormat;
- return Internal::DiffTextFormat;
+ return TextEditor::C_DIFF_LOCATION;
+ return TextEditor::C_TEXT;
}
void DiffAndLogHighlighterPrivate::updateOtherFormats()
{
m_addedTrailingWhiteSpaceFormat =
- invertedColorFormat(q->formatForCategory(Internal::DiffInFormat));
+ invertedColorFormat(q->formatForCategory(TextEditor::C_ADDED_LINE));
}
@@ -155,15 +145,7 @@ DiffAndLogHighlighter::DiffAndLogHighlighter(const QRegExp &filePattern, const Q
TextEditor::SyntaxHighlighter(static_cast<QTextDocument *>(0)),
d(new DiffAndLogHighlighterPrivate(this, filePattern, changePattern))
{
- static const QVector<TextEditor::TextStyle> categories({
- TextEditor::C_TEXT,
- TextEditor::C_ADDED_LINE,
- TextEditor::C_REMOVED_LINE,
- TextEditor::C_DIFF_FILE,
- TextEditor::C_DIFF_LOCATION,
- TextEditor::C_LOG_CHANGE_LINE
- });
- setTextFormatCategories(categories);
+ setDefaultTextFormatCategories();
d->updateOtherFormats();
}
@@ -193,21 +175,16 @@ void DiffAndLogHighlighter::highlightBlock(const QString &text)
return;
const int length = text.length();
- const Internal::DiffFormats format = d->analyzeLine(text);
- switch (format) {
- case Internal::DiffTextFormat:
- break;
- case Internal::DiffInFormat: {
+ const TextEditor::TextStyle format = d->analyzeLine(text);
+
+ if (format == TextEditor::C_ADDED_LINE) {
// Mark trailing whitespace.
const int trimmedLen = trimmedLength(text);
setFormat(0, trimmedLen, formatForCategory(format));
if (trimmedLen != length)
setFormat(trimmedLen, length - trimmedLen, d->m_addedTrailingWhiteSpaceFormat);
- }
- break;
- default:
+ } else if (format != TextEditor::C_TEXT) {
setFormat(0, length, formatForCategory(format));
- break;
}
// codefolding:
@@ -220,47 +197,35 @@ void DiffAndLogHighlighter::highlightBlock(const QString &text)
switch (d->m_foldingState) {
case Internal::StartOfFile:
case Internal::Header:
- switch (format) {
- case Internal::DiffFileFormat:
+ if (format == TextEditor::C_DIFF_FILE) {
d->m_foldingState = Internal::File;
TextEditor::TextDocumentLayout::setFoldingIndent(currentBlock(), BASE_LEVEL);
- break;
- case Internal::DiffLocationFormat:
+ } else if (format == TextEditor::C_DIFF_LOCATION) {
d->m_foldingState = Internal::Location;
TextEditor::TextDocumentLayout::setFoldingIndent(currentBlock(), FILE_LEVEL);
- break;
- default:
+ } else {
d->m_foldingState = Internal::Header;
TextEditor::TextDocumentLayout::setFoldingIndent(currentBlock(), BASE_LEVEL);
- break;
}
break;
case Internal::File:
- switch (format) {
- case Internal::DiffFileFormat:
+ if (format == TextEditor::C_DIFF_FILE) {
TextEditor::TextDocumentLayout::setFoldingIndent(currentBlock(), FILE_LEVEL);
- break;
- case Internal::DiffLocationFormat:
+ } else if (format == TextEditor::C_DIFF_LOCATION) {
d->m_foldingState = Internal::Location;
TextEditor::TextDocumentLayout::setFoldingIndent(currentBlock(), FILE_LEVEL);
- break;
- default:
+ } else {
TextEditor::TextDocumentLayout::setFoldingIndent(currentBlock(), FILE_LEVEL);
- break;
}
break;
case Internal::Location:
- switch (format) {
- case Internal::DiffFileFormat:
+ if (format == TextEditor::C_DIFF_FILE) {
d->m_foldingState = Internal::File;
TextEditor::TextDocumentLayout::setFoldingIndent(currentBlock(), BASE_LEVEL);
- break;
- case Internal::DiffLocationFormat:
+ } else if (format == TextEditor::C_DIFF_LOCATION) {
TextEditor::TextDocumentLayout::setFoldingIndent(currentBlock(), FILE_LEVEL);
- break;
- default:
+ } else {
TextEditor::TextDocumentLayout::setFoldingIndent(currentBlock(), LOCATION_LEVEL);
- break;
}
break;
}
diff --git a/tests/auto/generichighlighter/highlighterengine/syntaxhighlighter.h b/tests/auto/generichighlighter/highlighterengine/syntaxhighlighter.h
index 45122612c8..d98d0e8c8b 100644
--- a/tests/auto/generichighlighter/highlighterengine/syntaxhighlighter.h
+++ b/tests/auto/generichighlighter/highlighterengine/syntaxhighlighter.h
@@ -32,6 +32,8 @@
#include <QSyntaxHighlighter>
#include <texteditor/texteditorconstants.h>
+#include <functional>
+
namespace TextEditor {
class SyntaxHighlighter : public QSyntaxHighlighter
@@ -43,7 +45,7 @@ public:
protected:
void applyFormatToSpaces(const QString &, const QTextCharFormat &)
{}
- void setTextFormatCategories(const QVector<TextEditor::TextStyle> &)
+ void setTextFormatCategories(int, std::function<TextStyle(int)>)
{}
QTextCharFormat formatForCategory(int categoryIndex) const;