Editor: Fix visual whitespace highlighting

in comments and strings

Change-Id: I94cb478419afde77299b3be1ec252a21c17a1658
Reviewed-by: Leandro Melo <leandro.melo@nokia.com>
This commit is contained in:
David Schulz
2012-08-22 15:24:40 +02:00
committed by Leandro Melo
parent ef3c61fae5
commit a92694596e

View File

@@ -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;