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)
break;
}
applyFormatToSpaces(text, formatForCategory(ProfileVisualWhitespaceFormat));
formatSpaces(text);
}
} // namespace Internal
......
......@@ -83,7 +83,6 @@ static TextStyle styleForFormat(int format)
const auto f = Highlighter::TextFormatId(format);
switch (f) {
case Highlighter::Normal: return C_TEXT;
case Highlighter::VisualWhitespace: return C_VISUAL_WHITESPACE;
case Highlighter::Keyword: return C_KEYWORD;
case Highlighter::DataType: return C_TYPE;
case Highlighter::Comment: return C_COMMENT;
......@@ -250,7 +249,7 @@ void Highlighter::highlightBlock(const QString &text)
}
}
applyFormatToSpaces(text, formatForCategory(VisualWhitespace));
formatSpaces(text);
}
void Highlighter::setupDataForBlock(const QString &text)
......
......@@ -65,7 +65,6 @@ public:
enum TextFormatId {
Normal,
VisualWhitespace,
Keyword,
DataType,
Comment,
......
......@@ -48,7 +48,9 @@ class SyntaxHighlighterPrivate
public:
inline SyntaxHighlighterPrivate()
: q_ptr(0), rehighlightPending(false), inReformatBlocks(false)
{}
{
updateFormats(TextEditorSettings::fontSettings());
}
QPointer<QTextDocument> doc;
......@@ -64,7 +66,7 @@ public:
}
void applyFormatChanges(int from, int charsRemoved, int charsAdded);
void updateFormatsForCategories(const FontSettings &fontSettings);
void updateFormats(const FontSettings &fontSettings);
QVector<QTextCharFormat> formatChanges;
QTextBlock currentBlock;
......@@ -73,6 +75,7 @@ public:
TextDocumentLayout::FoldValidator foldValidator;
QVector<QTextCharFormat> formats;
QVector<std::pair<int,TextStyle>> formatCategories;
QTextCharFormat whitespaceFormat;
};
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)
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;
const int length = text.length();
while (offset < length) {
......@@ -483,7 +487,7 @@ void SyntaxHighlighter::applyFormatToSpaces(const QString &text, const QTextChar
int start = offset++;
while (offset < length && text.at(offset).isSpace())
++offset;
setFormat(start, offset - start, format);
setFormat(start, offset - start, d->whitespaceFormat);
} else {
++offset;
}
......@@ -725,7 +729,7 @@ QList<QColor> SyntaxHighlighter::generateColors(int n, const QColor &background)
void SyntaxHighlighter::setFontSettings(const FontSettings &fontSettings)
{
Q_D(SyntaxHighlighter);
d->updateFormatsForCategories(fontSettings);
d->updateFormats(fontSettings);
}
/*!
......@@ -771,7 +775,7 @@ void SyntaxHighlighter::setTextFormatCategories(const QVector<std::pair<int, Tex
d->formatCategories = categories;
const int maxCategory = Utils::maxElementOr(categories, {-1, C_TEXT}).first;
d->formats = QVector<QTextCharFormat>(maxCategory + 1);
d->updateFormatsForCategories(TextEditorSettings::fontSettings());
d->updateFormats(TextEditorSettings::fontSettings());
}
QTextCharFormat SyntaxHighlighter::formatForCategory(int category) const
......@@ -782,10 +786,11 @@ QTextCharFormat SyntaxHighlighter::formatForCategory(int category) const
return d->formats.at(category);
}
void SyntaxHighlighterPrivate::updateFormatsForCategories(const FontSettings &fontSettings)
void SyntaxHighlighterPrivate::updateFormats(const FontSettings &fontSettings)
{
for (const auto &pair : Utils::asConst(formatCategories))
formats[pair.first] = fontSettings.toTextCharFormat(pair.second);
whitespaceFormat = fontSettings.toTextCharFormat(C_VISUAL_WHITESPACE);
}
} // namespace TextEditor
......
......@@ -84,7 +84,7 @@ protected:
void setFormat(int start, int count, const QFont &font);
QTextCharFormat format(int pos) const;
void applyFormatToSpaces(const QString &text, const QTextCharFormat &format);
void formatSpaces(const QString &text);
int previousBlockState() const;
int currentBlockState() const;
......
......@@ -43,7 +43,7 @@ public:
virtual ~SyntaxHighlighter() {}
protected:
void applyFormatToSpaces(const QString &, const QTextCharFormat &)
void formatSpaces(const QString &)
{}
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