From 3b1070130893934a03e6af91331ef741050f821c Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Thu, 8 Oct 2009 14:57:18 +0200 Subject: [PATCH] Whitespace visualisation in the QML/JS editor akin to the C++ editor. --- .../qscripthighlighter/qscripthighlighter.cpp | 26 ++++++++++++++++--- .../qscripthighlighter/qscripthighlighter.h | 2 ++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/shared/qscripthighlighter/qscripthighlighter.cpp b/src/shared/qscripthighlighter/qscripthighlighter.cpp index 714e2189803..a028ac4d7ba 100644 --- a/src/shared/qscripthighlighter/qscripthighlighter.cpp +++ b/src/shared/qscripthighlighter/qscripthighlighter.cpp @@ -75,15 +75,15 @@ void QScriptHighlighter::highlightBlock(const QString &text) break; case QScriptIncrementalScanner::Token::String: - setFormat(token.offset, token.length, m_formats[StringFormat]); + highlightWhitespace(token, text, StringFormat); break; case QScriptIncrementalScanner::Token::Comment: - setFormat(token.offset, token.length, m_formats[CommentFormat]); + highlightWhitespace(token, text, CommentFormat); break; case QScriptIncrementalScanner::Token::Number: - setFormat(token.offset, token.length, m_formats[NumberFormat]); + highlightWhitespace(token, text, NumberFormat); break; case QScriptIncrementalScanner::Token::LeftParenthesis: @@ -238,3 +238,23 @@ int QScriptHighlighter::onBlockStart() void QScriptHighlighter::onOpeningParenthesis(QChar, int) {} void QScriptHighlighter::onClosingParenthesis(QChar, int) {} void QScriptHighlighter::onBlockEnd(int state, int) { return setCurrentBlockState(state); } + +void QScriptHighlighter::highlightWhitespace(const QScriptIncrementalScanner::Token &token, const QString &text, int nonWhitespaceFormat) +{ + const QTextCharFormat normalFormat = m_formats[nonWhitespaceFormat]; + const QTextCharFormat visualSpaceFormat = m_formats[VisualWhitespace]; + + const int end = token.end(); + int index = token.offset; + + while (index != end) { + const bool isSpace = text.at(index).isSpace(); + const int start = index; + + do { ++index; } + while (index != end && text.at(index).isSpace() == isSpace); + + const int tokenLength = index - start; + setFormat(start, tokenLength, isSpace ? visualSpaceFormat : normalFormat); + } +} diff --git a/src/shared/qscripthighlighter/qscripthighlighter.h b/src/shared/qscripthighlighter/qscripthighlighter.h index 46106fde538..9d654e9c553 100644 --- a/src/shared/qscripthighlighter/qscripthighlighter.h +++ b/src/shared/qscripthighlighter/qscripthighlighter.h @@ -69,6 +69,8 @@ protected: // sets the enriched user state, or simply calls setCurrentBlockState(state); virtual void onBlockEnd(int state, int firstNonSpace); + virtual void highlightWhitespace(const QScriptIncrementalScanner::Token &token, const QString &text, int nonWhitespaceFormat); + protected: QScriptIncrementalScanner m_scanner;