Commit f51fbd68 authored by Eike Ziller's avatar Eike Ziller

SyntaxHighligher: Remove the need to specify format for spaces

It has all means to know itself.

Change-Id: I464c195c5ee47e5fc58414a280c166e4a332c588
Reviewed-by: David Schulz's avatarDavid Schulz <david.schulz@qt.io>
parent cf57965e
...@@ -99,7 +99,7 @@ void ProFileHighlighter::highlightBlock(const QString &text) ...@@ -99,7 +99,7 @@ void ProFileHighlighter::highlightBlock(const QString &text)
break; break;
} }
applyFormatToSpaces(text, formatForCategory(ProfileVisualWhitespaceFormat)); formatSpaces(text);
} }
} // namespace Internal } // namespace Internal
......
...@@ -83,7 +83,6 @@ static TextStyle styleForFormat(int format) ...@@ -83,7 +83,6 @@ static TextStyle styleForFormat(int format)
const auto f = Highlighter::TextFormatId(format); const auto f = Highlighter::TextFormatId(format);
switch (f) { switch (f) {
case Highlighter::Normal: return C_TEXT; case Highlighter::Normal: return C_TEXT;
case Highlighter::VisualWhitespace: return C_VISUAL_WHITESPACE;
case Highlighter::Keyword: return C_KEYWORD; case Highlighter::Keyword: return C_KEYWORD;
case Highlighter::DataType: return C_TYPE; case Highlighter::DataType: return C_TYPE;
case Highlighter::Comment: return C_COMMENT; case Highlighter::Comment: return C_COMMENT;
...@@ -250,7 +249,7 @@ void Highlighter::highlightBlock(const QString &text) ...@@ -250,7 +249,7 @@ void Highlighter::highlightBlock(const QString &text)
} }
} }
applyFormatToSpaces(text, formatForCategory(VisualWhitespace)); formatSpaces(text);
} }
void Highlighter::setupDataForBlock(const QString &text) void Highlighter::setupDataForBlock(const QString &text)
......
...@@ -65,7 +65,6 @@ public: ...@@ -65,7 +65,6 @@ public:
enum TextFormatId { enum TextFormatId {
Normal, Normal,
VisualWhitespace,
Keyword, Keyword,
DataType, DataType,
Comment, Comment,
......
...@@ -48,7 +48,9 @@ class SyntaxHighlighterPrivate ...@@ -48,7 +48,9 @@ class SyntaxHighlighterPrivate
public: public:
inline SyntaxHighlighterPrivate() inline SyntaxHighlighterPrivate()
: q_ptr(0), rehighlightPending(false), inReformatBlocks(false) : q_ptr(0), rehighlightPending(false), inReformatBlocks(false)
{} {
updateFormats(TextEditorSettings::fontSettings());
}
QPointer<QTextDocument> doc; QPointer<QTextDocument> doc;
...@@ -64,7 +66,7 @@ public: ...@@ -64,7 +66,7 @@ public:
} }
void applyFormatChanges(int from, int charsRemoved, int charsAdded); void applyFormatChanges(int from, int charsRemoved, int charsAdded);
void updateFormatsForCategories(const FontSettings &fontSettings); void updateFormats(const FontSettings &fontSettings);
QVector<QTextCharFormat> formatChanges; QVector<QTextCharFormat> formatChanges;
QTextBlock currentBlock; QTextBlock currentBlock;
...@@ -73,6 +75,7 @@ public: ...@@ -73,6 +75,7 @@ public:
TextDocumentLayout::FoldValidator foldValidator; TextDocumentLayout::FoldValidator foldValidator;
QVector<QTextCharFormat> formats; QVector<QTextCharFormat> formats;
QVector<std::pair<int,TextStyle>> formatCategories; QVector<std::pair<int,TextStyle>> formatCategories;
QTextCharFormat whitespaceFormat;
}; };
static bool adjustRange(QTextLayout::FormatRange &range, int from, int charsRemoved, int charsAdded) { static bool adjustRange(QTextLayout::FormatRange &range, int from, int charsRemoved, int charsAdded) {
...@@ -474,8 +477,9 @@ void SyntaxHighlighter::setFormat(int start, int count, const QFont &font) ...@@ -474,8 +477,9 @@ void SyntaxHighlighter::setFormat(int start, int count, const QFont &font)
setFormat(start, count, format); setFormat(start, count, format);
} }
void SyntaxHighlighter::applyFormatToSpaces(const QString &text, const QTextCharFormat &format) void SyntaxHighlighter::formatSpaces(const QString &text)
{ {
Q_D(const SyntaxHighlighter);
int offset = 0; int offset = 0;
const int length = text.length(); const int length = text.length();
while (offset < length) { while (offset < length) {
...@@ -483,7 +487,7 @@ void SyntaxHighlighter::applyFormatToSpaces(const QString &text, const QTextChar ...@@ -483,7 +487,7 @@ void SyntaxHighlighter::applyFormatToSpaces(const QString &text, const QTextChar
int start = offset++; int start = offset++;
while (offset < length && text.at(offset).isSpace()) while (offset < length && text.at(offset).isSpace())
++offset; ++offset;
setFormat(start, offset - start, format); setFormat(start, offset - start, d->whitespaceFormat);
} else { } else {
++offset; ++offset;
} }
...@@ -725,7 +729,7 @@ QList<QColor> SyntaxHighlighter::generateColors(int n, const QColor &background) ...@@ -725,7 +729,7 @@ QList<QColor> SyntaxHighlighter::generateColors(int n, const QColor &background)
void SyntaxHighlighter::setFontSettings(const FontSettings &fontSettings) void SyntaxHighlighter::setFontSettings(const FontSettings &fontSettings)
{ {
Q_D(SyntaxHighlighter); Q_D(SyntaxHighlighter);
d->updateFormatsForCategories(fontSettings); d->updateFormats(fontSettings);
} }
/*! /*!
...@@ -771,7 +775,7 @@ void SyntaxHighlighter::setTextFormatCategories(const QVector<std::pair<int, Tex ...@@ -771,7 +775,7 @@ void SyntaxHighlighter::setTextFormatCategories(const QVector<std::pair<int, Tex
d->formatCategories = categories; d->formatCategories = categories;
const int maxCategory = Utils::maxElementOr(categories, {-1, C_TEXT}).first; const int maxCategory = Utils::maxElementOr(categories, {-1, C_TEXT}).first;
d->formats = QVector<QTextCharFormat>(maxCategory + 1); d->formats = QVector<QTextCharFormat>(maxCategory + 1);
d->updateFormatsForCategories(TextEditorSettings::fontSettings()); d->updateFormats(TextEditorSettings::fontSettings());
} }
QTextCharFormat SyntaxHighlighter::formatForCategory(int category) const QTextCharFormat SyntaxHighlighter::formatForCategory(int category) const
...@@ -782,10 +786,11 @@ QTextCharFormat SyntaxHighlighter::formatForCategory(int category) const ...@@ -782,10 +786,11 @@ QTextCharFormat SyntaxHighlighter::formatForCategory(int category) const
return d->formats.at(category); return d->formats.at(category);
} }
void SyntaxHighlighterPrivate::updateFormatsForCategories(const FontSettings &fontSettings) void SyntaxHighlighterPrivate::updateFormats(const FontSettings &fontSettings)
{ {
for (const auto &pair : Utils::asConst(formatCategories)) for (const auto &pair : Utils::asConst(formatCategories))
formats[pair.first] = fontSettings.toTextCharFormat(pair.second); formats[pair.first] = fontSettings.toTextCharFormat(pair.second);
whitespaceFormat = fontSettings.toTextCharFormat(C_VISUAL_WHITESPACE);
} }
} // namespace TextEditor } // namespace TextEditor
......
...@@ -84,7 +84,7 @@ protected: ...@@ -84,7 +84,7 @@ protected:
void setFormat(int start, int count, const QFont &font); void setFormat(int start, int count, const QFont &font);
QTextCharFormat format(int pos) const; QTextCharFormat format(int pos) const;
void applyFormatToSpaces(const QString &text, const QTextCharFormat &format); void formatSpaces(const QString &text);
int previousBlockState() const; int previousBlockState() const;
int currentBlockState() const; int currentBlockState() const;
......
...@@ -43,7 +43,7 @@ public: ...@@ -43,7 +43,7 @@ public:
virtual ~SyntaxHighlighter() {} virtual ~SyntaxHighlighter() {}
protected: protected:
void applyFormatToSpaces(const QString &, const QTextCharFormat &) void formatSpaces(const QString &)
{} {}
void setTextFormatCategories(int, std::function<TextStyle(int)>) void setTextFormatCategories(int, std::function<TextStyle(int)>)
{} {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment