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 "cpphighlighter.h"
#include <cpptools/cppdoxygen.h>
#include <Token.h> #include <Token.h>
#include <cplusplus/SimpleLexer.h> #include <cplusplus/SimpleLexer.h>
#include <cplusplus/Lexer.h>
#include <cpptools/cppdoxygen.h>
#include <cpptools/cpptoolsreuse.h> #include <cpptools/cpptoolsreuse.h>
#include <texteditor/basetextdocumentlayout.h> #include <texteditor/basetextdocumentlayout.h>
@@ -79,10 +80,12 @@ void CppHighlighter::highlightBlock(const QString &text)
setCurrentBlockState(previousState); setCurrentBlockState(previousState);
BaseTextDocumentLayout::clearParentheses(currentBlock()); BaseTextDocumentLayout::clearParentheses(currentBlock());
if (text.length()) {// the empty line can still contain whitespace if (text.length()) {// the empty line can still contain whitespace
if (!initialState) if (initialState == Lexer::State_MultiLineComment)
setFormat(0, text.length(), m_formats[CppVisualWhitespace]); highlightLine(text, 0, text.length(), m_formats[CppCommentFormat]);
else if (initialState == Lexer::State_MultiLineDoxyComment)
highlightLine(text, 0, text.length(), m_formats[CppDoxygenCommentFormat]);
else else
setFormat(0, text.length(), m_formats[CppCommentFormat]); setFormat(0, text.length(), m_formats[CppVisualWhitespace]);
} }
BaseTextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent); BaseTextDocumentLayout::setFoldingIndent(currentBlock(), foldingIndent);
return; return;
@@ -106,12 +109,8 @@ void CppHighlighter::highlightBlock(const QString &text)
tokens.at(i - 1).length(); tokens.at(i - 1).length();
} }
if (previousTokenEnd != tk.begin()) { if (previousTokenEnd != tk.begin())
if (initialState && tk.isComment()) setFormat(previousTokenEnd, tk.begin() - previousTokenEnd, m_formats[CppVisualWhitespace]);
setFormat(previousTokenEnd, tk.begin() - previousTokenEnd, m_formats[CppCommentFormat]);
else
setFormat(previousTokenEnd, tk.begin() - previousTokenEnd, m_formats[CppVisualWhitespace]);
}
if (tk.is(T_LPAREN) || tk.is(T_LBRACE) || tk.is(T_LBRACKET)) { if (tk.is(T_LPAREN) || tk.is(T_LBRACE) || tk.is(T_LBRACKET)) {
const QChar c = text.at(tk.begin()); 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]); setFormat(tk.begin(), tk.length(), m_formats[CppNumberFormat]);
else if (tk.isStringLiteral() || tk.isCharLiteral()) 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()) { else if (tk.isComment()) {
const int startPosition = initialState ? previousTokenEnd : tk.begin();
if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT)) 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 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 // we need to insert a close comment parenthesis, if
// - the line starts in a C Comment (initalState != 0) // - the line starts in a C Comment (initalState != 0)
@@ -208,12 +208,9 @@ void CppHighlighter::highlightBlock(const QString &text)
} }
// mark the trailing white spaces // mark the trailing white spaces
{ const int lastTokenEnd = tokens.last().end();
const Token tk = tokens.last(); if (text.length() > lastTokenEnd)
const int lastTokenEnd = tk.begin() + tk.length(); highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, m_formats[CppVisualWhitespace]);
if (text.length() > lastTokenEnd)
highlightLine(text, lastTokenEnd, text.length() - lastTokenEnd, QTextCharFormat());
}
if (! initialState && state && ! tokens.isEmpty()) { if (! initialState && state && ! tokens.isEmpty()) {
parentheses.append(Parenthesis(Parenthesis::Opened, QLatin1Char('+'), 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, void CppHighlighter::highlightLine(const QString &text, int position, int length,
const QTextCharFormat &format) const QTextCharFormat &format)
{ {
const QTextCharFormat visualSpaceFormat = m_formats[CppVisualWhitespace]; QTextCharFormat visualSpaceFormat = m_formats[CppVisualWhitespace];
visualSpaceFormat.setBackground(format.background());
const int end = position + length; const int end = position + length;
int index = position; int index = position;