diff options
author | Eike Ziller <eike.ziller@qt.io> | 2017-05-07 17:04:37 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2017-05-23 08:50:32 +0000 |
commit | 0a1376dd2c8460d9e297ce5466fe3c6669fd5bc8 (patch) | |
tree | 19f21919a16263f1dd6b988a08e135313d238fe0 /src/plugins/texteditor/syntaxhighlighter.cpp | |
parent | db11c01df807988b5135b4d33d600869b52f0fa8 (diff) |
Generalize function for visualizing whitespace from CppHighligher
Move function which formats all non-whitespace with a given format, and
all whitespace in the same range with correct whitespace highlighting
(merged with the non-whitespace format), from CppHighlighter to
SyntaxHighligher.
Change-Id: I8cac306f6362e804698068a0df0292f88726264f
Reviewed-by: David Schulz <david.schulz@qt.io>
Diffstat (limited to 'src/plugins/texteditor/syntaxhighlighter.cpp')
-rw-r--r-- | src/plugins/texteditor/syntaxhighlighter.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/plugins/texteditor/syntaxhighlighter.cpp b/src/plugins/texteditor/syntaxhighlighter.cpp index 7479c4a0a8..a9abec096b 100644 --- a/src/plugins/texteditor/syntaxhighlighter.cpp +++ b/src/plugins/texteditor/syntaxhighlighter.cpp @@ -495,6 +495,39 @@ void SyntaxHighlighter::formatSpaces(const QString &text) } /*! + The specified \a format is applied to all non-whitespace characters in the current text block + with \a text, from the \a start position for a length of \a count characters. + Whitespace characters are formatted with the visual whitespace format, merged with the + non-whitespace format. + + \sa setFormat() +*/ +void SyntaxHighlighter::setFormatWithSpaces(const QString &text, int start, int count, + const QTextCharFormat &format) +{ + Q_D(const SyntaxHighlighter); + QTextCharFormat visualSpaceFormat = d->whitespaceFormat; + visualSpaceFormat.setBackground(format.background()); + + const int end = start + count; + int index = start; + + 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; + if (isSpace) + setFormat(start, tokenLength, visualSpaceFormat); + else if (format.isValid()) + setFormat(start, tokenLength, format); + } +} + +/*! Returns the format at \a position inside the syntax highlighter's current text block. */ |