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)
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
......
......@@ -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
......@@ -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('\\') ||
......
......@@ -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);
......
......@@ -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
......
......@@ -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;
......
......@@ -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:
......
......@@ -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;
}
......
......@@ -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();
......
......@@ -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);
};
......
......@@ -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);
}
/**
......
......@@ -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)
......
......@@ -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;
}