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,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);
}
/**