aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/cpphighlighter.cpp
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@nokia.com>2012-08-22 15:24:40 +0200
committerLeandro Melo <leandro.melo@nokia.com>2012-08-29 11:24:42 +0200
commita92694596e804b2fc551d82f62d4ab5ab797130b (patch)
tree22ce131819ba53d7a630abf2ec1434f3274465d3 /src/plugins/cppeditor/cpphighlighter.cpp
parentef3c61fae57d6125a4efcc6f2a03d606ccd6bbe9 (diff)
Editor: Fix visual whitespace highlighting
in comments and strings Change-Id: I94cb478419afde77299b3be1ec252a21c17a1658 Reviewed-by: Leandro Melo <leandro.melo@nokia.com>
Diffstat (limited to 'src/plugins/cppeditor/cpphighlighter.cpp')
-rw-r--r--src/plugins/cppeditor/cpphighlighter.cpp38
1 files changed, 18 insertions, 20 deletions
diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp
index 928e664f74..16a6d7213d 100644
--- a/src/plugins/cppeditor/cpphighlighter.cpp
+++ b/src/plugins/cppeditor/cpphighlighter.cpp
@@ -29,10 +29,11 @@
**************************************************************************/
#include "cpphighlighter.h"
-#include <cpptools/cppdoxygen.h>
#include <Token.h>
#include <cplusplus/SimpleLexer.h>
+#include <cplusplus/Lexer.h>
+#include <cpptools/cppdoxygen.h>
#include <cpptools/cpptoolsreuse.h>
#include <texteditor/basetextdocumentlayout.h>
@@ -79,10 +80,12 @@ void CppHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(previousState);
BaseTextDocumentLayout::clearParentheses(currentBlock());
if (text.length()) {// the empty line can still contain whitespace
- if (!initialState)
- setFormat(0, text.length(), m_formats[CppVisualWhitespace]);
+ if (initialState == Lexer::State_MultiLineComment)
+ highlightLine(text, 0, text.length(), m_formats[CppCommentFormat]);
+ else if (initialState == Lexer::State_MultiLineDoxyComment)
+ highlightLine(text, 0, text.length(), m_formats[CppDoxygenCommentFormat]);
else
- setFormat(0, text.length(), m_formats[CppCommentFormat]);
+ setFormat(0, text.length(), m_formats[CppVisualWhitespace]);
}
BaseTextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
return;
@@ -106,12 +109,8 @@ void CppHighlighter::highlightBlock(const QString &text)
tokens.at(i - 1).length();
}
- if (previousTokenEnd != tk.begin()) {
- if (initialState && tk.isComment())
- setFormat(previousTokenEnd, tk.begin() - previousTokenEnd, m_formats[CppCommentFormat]);
- else
- setFormat(previousTokenEnd, tk.begin() - previousTokenEnd, m_formats[CppVisualWhitespace]);
- }
+ if (previousTokenEnd != tk.begin())
+ setFormat(previousTokenEnd, tk.begin() - previousTokenEnd, m_formats[CppVisualWhitespace]);
if (tk.is(T_LPAREN) || tk.is(T_LBRACE) || tk.is(T_LBRACKET)) {
const QChar c = text.at(tk.begin());
@@ -166,14 +165,15 @@ void CppHighlighter::highlightBlock(const QString &text)
setFormat(tk.begin(), tk.length(), m_formats[CppNumberFormat]);
else if (tk.isStringLiteral() || tk.isCharLiteral())
- setFormat(tk.begin(), tk.length(), m_formats[CppStringFormat]);
+ highlightLine(text, tk.begin(), tk.length(), m_formats[CppStringFormat]);
else if (tk.isComment()) {
+ const int startPosition = initialState ? previousTokenEnd : tk.begin();
if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT))
- setFormat(tk.begin(), tk.length(), m_formats[CppCommentFormat]);
+ highlightLine(text, startPosition, tk.end() - startPosition, m_formats[CppCommentFormat]);
else // a doxygen comment
- highlightDoxygenComment(text, tk.begin(), tk.length());
+ highlightDoxygenComment(text, startPosition, tk.end() - startPosition);
// we need to insert a close comment parenthesis, if
// - the line starts in a C Comment (initalState != 0)
@@ -208,12 +208,9 @@ void CppHighlighter::highlightBlock(const QString &text)
}
// mark the trailing white spaces
- {
- const Token tk = tokens.last();
- const int lastTokenEnd = tk.begin() + tk.length();
- if (text.length() > lastTokenEnd)
- highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, QTextCharFormat());
- }
+ const int lastTokenEnd = tokens.last().end();
+ if (text.length() > lastTokenEnd)
+ highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, m_formats[CppVisualWhitespace]);
if (! initialState && state && ! tokens.isEmpty()) {
parentheses.append(Parenthesis(Parenthesis::Opened, QLatin1Char('+'),
@@ -335,7 +332,8 @@ bool CppHighlighter::isPPKeyword(const QStringRef &text) const
void CppHighlighter::highlightLine(const QString &text, int position, int length,
const QTextCharFormat &format)
{
- const QTextCharFormat visualSpaceFormat = m_formats[CppVisualWhitespace];
+ QTextCharFormat visualSpaceFormat = m_formats[CppVisualWhitespace];
+ visualSpaceFormat.setBackground(format.background());
const int end = position + length;
int index = position;