aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor/syntaxhighlighter.cpp
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 /src/plugins/texteditor/syntaxhighlighter.cpp
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>
Diffstat (limited to 'src/plugins/texteditor/syntaxhighlighter.cpp')
-rw-r--r--src/plugins/texteditor/syntaxhighlighter.cpp47
1 files changed, 44 insertions, 3 deletions
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