diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp index 34af6a5ec48..7d5648646cc 100644 --- a/src/plugins/cppeditor/cpphighlighter.cpp +++ b/src/plugins/cppeditor/cpphighlighter.cpp @@ -214,7 +214,7 @@ void CppHighlighter::highlightBlock(const QString &text) // mark the trailing white spaces const int lastTokenEnd = tokens.last().utf16charsEnd(); if (text.length() > lastTokenEnd) - setFormatWithSpaces(text, lastTokenEnd, text.length() - lastTokenEnd, formatForCategory(C_VISUAL_WHITESPACE)); + formatSpaces(text, lastTokenEnd, text.length() - lastTokenEnd); if (!initialLexerState && lexerState && !tokens.isEmpty()) { const Token &lastToken = tokens.last(); diff --git a/src/plugins/texteditor/syntaxhighlighter.cpp b/src/plugins/texteditor/syntaxhighlighter.cpp index a9abec096b2..441cc5132bb 100644 --- a/src/plugins/texteditor/syntaxhighlighter.cpp +++ b/src/plugins/texteditor/syntaxhighlighter.cpp @@ -477,15 +477,15 @@ void SyntaxHighlighter::setFormat(int start, int count, const QFont &font) setFormat(start, count, format); } -void SyntaxHighlighter::formatSpaces(const QString &text) +void SyntaxHighlighter::formatSpaces(const QString &text, int start, int count) { Q_D(const SyntaxHighlighter); - int offset = 0; - const int length = text.length(); - while (offset < length) { + int offset = start; + const int end = std::min(start + count, text.length()); + while (offset < end) { if (text.at(offset).isSpace()) { int start = offset++; - while (offset < length && text.at(offset).isSpace()) + while (offset < end && text.at(offset).isSpace()) ++offset; setFormat(start, offset - start, d->whitespaceFormat); } else { diff --git a/src/plugins/texteditor/syntaxhighlighter.h b/src/plugins/texteditor/syntaxhighlighter.h index 734a35b2b3b..cb41dc9668a 100644 --- a/src/plugins/texteditor/syntaxhighlighter.h +++ b/src/plugins/texteditor/syntaxhighlighter.h @@ -33,6 +33,7 @@ #include #include +#include QT_BEGIN_NAMESPACE class QTextDocument; @@ -84,7 +85,7 @@ protected: void setFormat(int start, int count, const QFont &font); QTextCharFormat format(int pos) const; - void formatSpaces(const QString &text); + void formatSpaces(const QString &text, int start = 0, int count = INT_MAX); void setFormatWithSpaces(const QString &text, int start, int count, const QTextCharFormat &format);