Simplify text format handling in syntax highlighters

Pass the mapping from custom enum to text style in form of a function,
which then can use a switch which is checked by compilers, and
avoids the need to lookup a different enum somewhere else to find
out what the mapping actually is.
That mapping is cached to keep performance as before.

Also, most highlighters created an enum just for the purpose of mapping
to text styles, basically creating duplicated subsets of text style like
enums everywhere. Instead provide a default, identity mapping from text
styles to text styles.

Change-Id: I2ea1ca702b99e36b8742dfda510b1b2753f0a1c2
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2017-05-05 20:34:29 +02:00
parent 762f67f729
commit cf57965ebc
21 changed files with 276 additions and 312 deletions

View File

@@ -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);