Commit cf57965e authored by Eike Ziller's avatar Eike Ziller

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's avatarDavid Schulz <david.schulz@qt.io>
parent 762f67f7
...@@ -153,6 +153,21 @@ decltype(auto) equal(R S::*member, T value) ...@@ -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)); 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 // transform
......
...@@ -33,21 +33,5 @@ enum FileType { ...@@ -33,21 +33,5 @@ enum FileType {
Source Source
}; };
enum CppFormats {
CppNumberFormat,
CppStringFormat,
CppTypeFormat,
CppKeywordFormat,
CppPrimitiveTypeFormat,
CppOperatorFormat,
CppPreprocessorFormat,
CppLabelFormat,
CppCommentFormat,
CppDoxygenCommentFormat,
CppDoxygenTagFormat,
CppVisualWhitespace,
NumCppFormats
};
} // namespace Internal } // namespace Internal
} // namespace CppEditor } // namespace CppEditor
...@@ -42,21 +42,7 @@ using namespace CPlusPlus; ...@@ -42,21 +42,7 @@ using namespace CPlusPlus;
CppHighlighter::CppHighlighter(QTextDocument *document) : CppHighlighter::CppHighlighter(QTextDocument *document) :
SyntaxHighlighter(document) SyntaxHighlighter(document)
{ {
static const QVector<TextStyle> categories({ setDefaultTextFormatCategories();
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);
} }
void CppHighlighter::highlightBlock(const QString &text) void CppHighlighter::highlightBlock(const QString &text)
...@@ -90,11 +76,11 @@ void CppHighlighter::highlightBlock(const QString &text) ...@@ -90,11 +76,11 @@ void CppHighlighter::highlightBlock(const QString &text)
TextDocumentLayout::clearParentheses(currentBlock()); TextDocumentLayout::clearParentheses(currentBlock());
if (text.length()) {// the empty line can still contain whitespace if (text.length()) {// the empty line can still contain whitespace
if (initialLexerState == T_COMMENT) 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) else if (initialLexerState == T_DOXY_COMMENT)
highlightLine(text, 0, text.length(), formatForCategory(CppDoxygenCommentFormat)); highlightLine(text, 0, text.length(), formatForCategory(C_DOXYGEN_COMMENT));
else else
setFormat(0, text.length(), formatForCategory(CppVisualWhitespace)); setFormat(0, text.length(), formatForCategory(C_VISUAL_WHITESPACE));
} }
TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent); TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
return; return;
...@@ -121,7 +107,7 @@ void CppHighlighter::highlightBlock(const QString &text) ...@@ -121,7 +107,7 @@ void CppHighlighter::highlightBlock(const QString &text)
if (previousTokenEnd != tk.utf16charsBegin()) { if (previousTokenEnd != tk.utf16charsBegin()) {
setFormat(previousTokenEnd, setFormat(previousTokenEnd,
tk.utf16charsBegin() - previousTokenEnd, tk.utf16charsBegin() - previousTokenEnd,
formatForCategory(CppVisualWhitespace)); formatForCategory(C_VISUAL_WHITESPACE));
} }
if (tk.is(T_LPAREN) || tk.is(T_LBRACE) || tk.is(T_LBRACKET)) { if (tk.is(T_LPAREN) || tk.is(T_LBRACE) || tk.is(T_LBRACKET)) {
...@@ -162,12 +148,12 @@ void CppHighlighter::highlightBlock(const QString &text) ...@@ -162,12 +148,12 @@ void CppHighlighter::highlightBlock(const QString &text)
if (i == 0 && tk.is(T_POUND)) { if (i == 0 && tk.is(T_POUND)) {
highlightLine(text, tk.utf16charsBegin(), tk.utf16chars(), highlightLine(text, tk.utf16charsBegin(), tk.utf16chars(),
formatForCategory(CppPreprocessorFormat)); formatForCategory(C_PREPROCESSOR));
expectPreprocessorKeyword = true; expectPreprocessorKeyword = true;
} else if (highlightCurrentWordAsPreprocessor } else if (highlightCurrentWordAsPreprocessor
&& (tk.isKeyword() || tk.is(T_IDENTIFIER)) && (tk.isKeyword() || tk.is(T_IDENTIFIER))
&& isPPKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars()))) { && 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()); const QStringRef ppKeyword = text.midRef(tk.utf16charsBegin(), tk.utf16chars());
if (ppKeyword == QLatin1String("error") if (ppKeyword == QLatin1String("error")
|| ppKeyword == QLatin1String("warning") || ppKeyword == QLatin1String("warning")
...@@ -176,14 +162,14 @@ void CppHighlighter::highlightBlock(const QString &text) ...@@ -176,14 +162,14 @@ void CppHighlighter::highlightBlock(const QString &text)
} }
} else if (tk.is(T_NUMERIC_LITERAL)) { } 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()) { } 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()) { } else if (tk.isComment()) {
const int startPosition = initialLexerState ? previousTokenEnd : tk.utf16charsBegin(); const int startPosition = initialLexerState ? previousTokenEnd : tk.utf16charsBegin();
if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT)) { if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT)) {
highlightLine(text, startPosition, tk.utf16charsEnd() - startPosition, highlightLine(text, startPosition, tk.utf16charsEnd() - startPosition,
formatForCategory(CppCommentFormat)); formatForCategory(C_COMMENT));
} }
else // a doxygen comment else // a doxygen comment
...@@ -211,14 +197,14 @@ void CppHighlighter::highlightBlock(const QString &text) ...@@ -211,14 +197,14 @@ void CppHighlighter::highlightBlock(const QString &text)
|| (m_languageFeatures.qtKeywordsEnabled || (m_languageFeatures.qtKeywordsEnabled
&& CppTools::isQtKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars()))) && CppTools::isQtKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars())))
|| (m_languageFeatures.objCEnabled && tk.isObjCAtKeyword())) { || (m_languageFeatures.objCEnabled && tk.isObjCAtKeyword())) {
setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppKeywordFormat)); setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(C_KEYWORD));
} else if (tk.isPrimitiveType()) { } else if (tk.isPrimitiveType()) {
setFormat(tk.utf16charsBegin(), tk.utf16chars(), setFormat(tk.utf16charsBegin(), tk.utf16chars(),
formatForCategory(CppPrimitiveTypeFormat)); formatForCategory(C_PRIMITIVE_TYPE));
} else if (tk.isOperator()) { } 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)) { } 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)) { } else if (tk.is(T_IDENTIFIER)) {
highlightWord(text.midRef(tk.utf16charsBegin(), tk.utf16chars()), tk.utf16charsBegin(), highlightWord(text.midRef(tk.utf16charsBegin(), tk.utf16chars()), tk.utf16charsBegin(),
tk.utf16chars()); tk.utf16chars());
...@@ -228,7 +214,7 @@ void CppHighlighter::highlightBlock(const QString &text) ...@@ -228,7 +214,7 @@ void CppHighlighter::highlightBlock(const QString &text)
// mark the trailing white spaces // mark the trailing white spaces
const int lastTokenEnd = tokens.last().utf16charsEnd(); const int lastTokenEnd = tokens.last().utf16charsEnd();
if (text.length() > lastTokenEnd) 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()) { if (!initialLexerState && lexerState && !tokens.isEmpty()) {
const Token &lastToken = tokens.last(); const Token &lastToken = tokens.last();
...@@ -360,7 +346,7 @@ bool CppHighlighter::isPPKeyword(const QStringRef &text) const ...@@ -360,7 +346,7 @@ bool CppHighlighter::isPPKeyword(const QStringRef &text) const
void CppHighlighter::highlightLine(const QString &text, int position, int length, void CppHighlighter::highlightLine(const QString &text, int position, int length,
const QTextCharFormat &format) const QTextCharFormat &format)
{ {
QTextCharFormat visualSpaceFormat = formatForCategory(CppVisualWhitespace); QTextCharFormat visualSpaceFormat = formatForCategory(C_VISUAL_WHITESPACE);
visualSpaceFormat.setBackground(format.background()); visualSpaceFormat.setBackground(format.background());
const int end = position + length; const int end = position + length;
...@@ -394,7 +380,7 @@ void CppHighlighter::highlightWord(QStringRef word, int position, int length) ...@@ -394,7 +380,7 @@ void CppHighlighter::highlightWord(QStringRef word, int position, int length)
return; return;
} }
setFormat(position, length, formatForCategory(CppTypeFormat)); setFormat(position, length, formatForCategory(C_TYPE));
} }
} }
} }
...@@ -406,8 +392,8 @@ void CppHighlighter::highlightDoxygenComment(const QString &text, int position, ...@@ -406,8 +392,8 @@ void CppHighlighter::highlightDoxygenComment(const QString &text, int position,
const QChar *uc = text.unicode(); const QChar *uc = text.unicode();
const QChar *it = uc + position; const QChar *it = uc + position;
const QTextCharFormat &format = formatForCategory(CppDoxygenCommentFormat); const QTextCharFormat &format = formatForCategory(C_DOXYGEN_COMMENT);
const QTextCharFormat &kwFormat = formatForCategory(CppDoxygenTagFormat); const QTextCharFormat &kwFormat = formatForCategory(C_DOXYGEN_TAG);
while (!it->isNull()) { while (!it->isNull()) {
if (it->unicode() == QLatin1Char('\\') || if (it->unicode() == QLatin1Char('\\') ||
......
...@@ -38,9 +38,7 @@ static const char CHANGE_PATTERN[] = "\\b[a-f0-9]{7,40}\\b"; ...@@ -38,9 +38,7 @@ static const char CHANGE_PATTERN[] = "\\b[a-f0-9]{7,40}\\b";
GitSubmitHighlighter::GitSubmitHighlighter(QTextEdit * parent) : GitSubmitHighlighter::GitSubmitHighlighter(QTextEdit * parent) :
TextEditor::SyntaxHighlighter(parent) TextEditor::SyntaxHighlighter(parent)
{ {
static const QVector<TextEditor::TextStyle> categories({TextEditor::C_COMMENT}); setDefaultTextFormatCategories();
setTextFormatCategories(categories);
m_keywordPattern.setPattern("^[\\w-]+:"); m_keywordPattern.setPattern("^[\\w-]+:");
m_hashChar = '#'; m_hashChar = '#';
QTC_CHECK(m_keywordPattern.isValid()); QTC_CHECK(m_keywordPattern.isValid());
...@@ -56,7 +54,7 @@ void GitSubmitHighlighter::highlightBlock(const QString &text) ...@@ -56,7 +54,7 @@ void GitSubmitHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(state); setCurrentBlockState(state);
return; return;
} else if (text.startsWith(m_hashChar)) { } else if (text.startsWith(m_hashChar)) {
setFormat(0, text.size(), formatForCategory(Format_Comment)); setFormat(0, text.size(), formatForCategory(TextEditor::C_COMMENT));
setCurrentBlockState(state); setCurrentBlockState(state);
return; return;
} else if (state == None) { } else if (state == None) {
...@@ -92,23 +90,34 @@ GitRebaseHighlighter::RebaseAction::RebaseAction(const QString &regexp, ...@@ -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) : GitRebaseHighlighter::GitRebaseHighlighter(QTextDocument *parent) :
TextEditor::SyntaxHighlighter(parent), TextEditor::SyntaxHighlighter(parent),
m_hashChar('#'), m_hashChar('#'),
m_changeNumberPattern(CHANGE_PATTERN) m_changeNumberPattern(CHANGE_PATTERN)
{ {
static const QVector<TextEditor::TextStyle> categories({ setTextFormatCategories(Format_Count, styleForFormat);
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);
m_actions << RebaseAction("^(p|pick)\\b", Format_Pick); m_actions << RebaseAction("^(p|pick)\\b", Format_Pick);
m_actions << RebaseAction("^(r|reword)\\b", Format_Reword); m_actions << RebaseAction("^(r|reword)\\b", Format_Reword);
......
...@@ -41,7 +41,8 @@ enum Format { ...@@ -41,7 +41,8 @@ enum Format {
Format_Edit, Format_Edit,
Format_Squash, Format_Squash,
Format_Fixup, Format_Fixup,
Format_Exec Format_Exec,
Format_Count
}; };
// Highlighter for git submit messages. Make the first line bold, indicates // Highlighter for git submit messages. Make the first line bold, indicates
......
...@@ -34,26 +34,14 @@ ...@@ -34,26 +34,14 @@
using namespace TextEditor; using namespace TextEditor;
const TextStyle GLSLReservedKeyword = C_REMOVED_LINE;
namespace GlslEditor { namespace GlslEditor {
namespace Internal { namespace Internal {
GlslHighlighter::GlslHighlighter() GlslHighlighter::GlslHighlighter()
{ {
static const QVector<TextStyle> categories({ setDefaultTextFormatCategories();
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);
} }
void GlslHighlighter::highlightBlock(const QString &text) void GlslHighlighter::highlightBlock(const QString &text)
...@@ -99,7 +87,7 @@ void GlslHighlighter::highlightBlock(const QString &text) ...@@ -99,7 +87,7 @@ void GlslHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(previousState); setCurrentBlockState(previousState);
TextDocumentLayout::clearParentheses(currentBlock()); TextDocumentLayout::clearParentheses(currentBlock());
if (text.length()) // the empty line can still contain whitespace 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); TextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
return; return;
} }
...@@ -123,7 +111,7 @@ void GlslHighlighter::highlightBlock(const QString &text) ...@@ -123,7 +111,7 @@ void GlslHighlighter::highlightBlock(const QString &text)
if (previousTokenEnd != tk.begin()) { if (previousTokenEnd != tk.begin()) {
setFormat(previousTokenEnd, tk.begin() - previousTokenEnd, 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)) { 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) ...@@ -160,17 +148,17 @@ void GlslHighlighter::highlightBlock(const QString &text)
highlightAsPreprocessor = false; highlightAsPreprocessor = false;
if (false /* && i == 0 && tk.is(GLSL::Parser::T_POUND)*/) { 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; highlightAsPreprocessor = true;
} else if (highlightCurrentWordAsPreprocessor && isPPKeyword(text.midRef(tk.begin(), tk.length))) { } 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)) { } 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)) { } 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 // we need to insert a close comment parenthesis, if
// - the line starts in a C Comment (initalState != 0) // - the line starts in a C Comment (initalState != 0)
...@@ -195,7 +183,7 @@ void GlslHighlighter::highlightBlock(const QString &text) ...@@ -195,7 +183,7 @@ void GlslHighlighter::highlightBlock(const QString &text)
if (kind == GLSL::Parser::T_RESERVED) if (kind == GLSL::Parser::T_RESERVED)
setFormat(tk.position, tk.length, formatForCategory(GLSLReservedKeyword)); setFormat(tk.position, tk.length, formatForCategory(GLSLReservedKeyword));
else if (kind != GLSL::Parser::T_IDENTIFIER) 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) ...@@ -248,7 +236,7 @@ void GlslHighlighter::highlightBlock(const QString &text)
void GlslHighlighter::highlightLine(const QString &text, int position, int length, void GlslHighlighter::highlightLine(const QString &text, int position, int length,
const QTextCharFormat &format) const QTextCharFormat &format)
{ {
const QTextCharFormat visualSpaceFormat = formatForCategory(GLSLVisualWhitespace); const QTextCharFormat visualSpaceFormat = formatForCategory(C_VISUAL_WHITESPACE);
const int end = position + length; const int end = position + length;
int index = position; int index = position;
......
...@@ -35,22 +35,6 @@ class GlslHighlighter : public TextEditor::SyntaxHighlighter ...@@ -35,22 +35,6 @@ class GlslHighlighter : public TextEditor::SyntaxHighlighter
Q_OBJECT Q_OBJECT
public: public:
enum Formats {
GLSLNumberFormat,
GLSLStringFormat,
GLSLTypeFormat,
GLSLKeywordFormat,
GLSLOperatorFormat,
GLSLPreprocessorFormat,
GLSLLabelFormat,
GLSLCommentFormat,
GLSLDoxygenCommentFormat,
GLSLDoxygenTagFormat,
GLSLVisualWhitespace,
GLSLReservedKeyword,
NumGLSLFormats
};
GlslHighlighter(); GlslHighlighter();
protected: protected:
......
...@@ -52,7 +52,6 @@ public: ...@@ -52,7 +52,6 @@ public:
private: private:
enum State { None = -1, Header, Other }; enum State { None = -1, Header, Other };
enum Format { Format_Comment };
QRegExp m_keywordPattern; QRegExp m_keywordPattern;
}; };
...@@ -60,10 +59,8 @@ MercurialSubmitHighlighter::MercurialSubmitHighlighter(QTextEdit *parent) : ...@@ -60,10 +59,8 @@ MercurialSubmitHighlighter::MercurialSubmitHighlighter(QTextEdit *parent) :
TextEditor::SyntaxHighlighter(parent), TextEditor::SyntaxHighlighter(parent),
m_keywordPattern(QLatin1String("^\\w+:")) m_keywordPattern(QLatin1String("^\\w+:"))
{ {
static const QVector<TextEditor::TextStyle> categories({TextEditor::C_COMMENT});
setTextFormatCategories(categories);
QTC_CHECK(m_keywordPattern.isValid()); QTC_CHECK(m_keywordPattern.isValid());
setDefaultTextFormatCategories();
} }
void MercurialSubmitHighlighter::highlightBlock(const QString &text) void MercurialSubmitHighlighter::highlightBlock(const QString &text)
...@@ -71,7 +68,7 @@ void MercurialSubmitHighlighter::highlightBlock(const QString &text) ...@@ -71,7 +68,7 @@ void MercurialSubmitHighlighter::highlightBlock(const QString &text)
// figure out current state // figure out current state
State state = static_cast<State>(previousBlockState()); State state = static_cast<State>(previousBlockState());
if (text.startsWith(QLatin1String("HG:"))) { if (text.startsWith(QLatin1String("HG:"))) {
setFormat(0, text.size(), formatForCategory(Format_Comment)); setFormat(0, text.size(), formatForCategory(TextEditor::C_COMMENT));
setCurrentBlockState(state); setCurrentBlockState(state);
return; return;
} }
......
...@@ -35,7 +35,7 @@ namespace Nim { ...@@ -35,7 +35,7 @@ namespace Nim {
NimHighlighter::NimHighlighter() NimHighlighter::NimHighlighter()
{ {
initTextFormats(); setDefaultTextFormatCategories();
} }
void NimHighlighter::highlightBlock(const QString &text) void NimHighlighter::highlightBlock(const QString &text)
...@@ -43,56 +43,36 @@ void NimHighlighter::highlightBlock(const QString &text) ...@@ -43,56 +43,36 @@ void NimHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(highlightLine(text, previousBlockState())); setCurrentBlockState(highlightLine(text, previousBlockState()));
} }
void NimHighlighter::initTextFormats() TextEditor::TextStyle NimHighlighter::styleForToken(const NimLexer::Token &token,
{ const QString &tokenValue)
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)
{ {
switch (token.type) { switch (token.type) {
case NimLexer::TokenType::Keyword: case NimLexer::TokenType::Keyword:
return KeywordCategory; return TextEditor::C_KEYWORD;
case NimLexer::TokenType::Identifier: case NimLexer::TokenType::Identifier:
return categoryForIdentifier(token, tokenValue); return styleForIdentifier(token, tokenValue);
case NimLexer::TokenType::Comment: case NimLexer::TokenType::Comment:
return CommentCategory; return TextEditor::C_COMMENT;
case NimLexer::TokenType::Documentation: case NimLexer::TokenType::Documentation:
return DocumentationCategory; return TextEditor::C_DOXYGEN_COMMENT;
case NimLexer::TokenType::StringLiteral: case NimLexer::TokenType::StringLiteral:
return StringCategory; return TextEditor::C_STRING;
case NimLexer::TokenType::MultiLineStringLiteral: case NimLexer::TokenType::MultiLineStringLiteral:
return StringCategory; return TextEditor::C_STRING;
case NimLexer::TokenType::Operator: case NimLexer::TokenType::Operator:
return OperatorCategory; return TextEditor::C_OPERATOR;
case NimLexer::TokenType::Number: case NimLexer::TokenType::Number:
return NumberCategory; return TextEditor::C_NUMBER;
default: default:
return TextCategory; return TextEditor::C_TEXT;
} }
} }
NimHighlighter::Category NimHighlighter::categoryForIdentifier(const NimLexer::Token &token, TextEditor::TextStyle NimHighlighter::styleForIdentifier(const NimLexer::Token &token,
const QString &tokenValue) const QString &tokenValue)
{ {
Q_UNUSED(token) 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 { static QSet<QString> nimBuiltInValues {
"true", "false" "true", "false"
...@@ -111,12 +91,12 @@ NimHighlighter::Category NimHighlighter::categoryForIdentifier(const NimLexer::T ...@@ -111,12 +91,12 @@ NimHighlighter::Category NimHighlighter::categoryForIdentifier(const NimLexer::T
}; };
if (nimBuiltInFunctions.contains(tokenValue)) if (nimBuiltInFunctions.contains(tokenValue))
return TypeCategory; return TextEditor::C_TYPE;
if (nimBuiltInValues.contains(tokenValue)) if (nimBuiltInValues.contains(tokenValue))
return KeywordCategory;