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>
This commit is contained in:
Eike Ziller
2017-05-07 17:04:37 +02:00
parent db11c01df8
commit 0a1376dd2c
4 changed files with 43 additions and 34 deletions

View File

@@ -494,6 +494,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.