CppEditor: Clear out mis-detected parentheses

... from the syntactic highlighter.
For instance, in this piece of code:
    void myfunc()
    {
        const auto rawString = R"(abc
        de)fg"hij)";
    }
The closing parenthesis inside the raw string literal is mis-interpreted
by SimpleLexer as closing the string, causing the next one to be
interpreted as an actual parenthesis, which would then be erroneously
matched against the opening brace of the function.
We fix this by removing parentheses that the semantic highlighter knows
are not actual punctuation tokens.

Change-Id: I057b9e747ed81282cdddd71a805c12db9616da65
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2021-09-20 15:22:05 +02:00
parent 0745dc1634
commit 63fbaedc03

View File

@@ -180,9 +180,29 @@ void SemanticHighlighter::onHighlighterResultAvailable(int from, int to)
if (result.kind != AngleBracketOpen && result.kind != AngleBracketClose
&& result.kind != DoubleAngleBracketClose
&& result.kind != TernaryIf && result.kind != TernaryElse) {
const QTextBlock block =
const QTextBlock firstBlockForResult =
m_baseTextDocument->document()->findBlockByNumber(result.line - 1);
TextDocumentLayout::setParentheses(block, getClearedParentheses(block));
const int startRange = firstBlockForResult.position() + result.column - 1;
const int endRange = startRange + result.length;
const QTextBlock lastBlockForResult = m_baseTextDocument->document()
->findBlock(endRange);
const QTextBlock endBlock = lastBlockForResult.next();
for (QTextBlock block = firstBlockForResult; block != endBlock; block = block.next()) {
Parentheses syntacticParens = getClearedParentheses(block);
// Remove mis-detected parentheses inserted by syntactic highlighter.
// This typically happens with raw string literals.
if (result.textStyles.mainStyle != C_PUNCTUATION) {
for (auto it = syntacticParens.begin(); it != syntacticParens.end();) {
const int absParenPos = block.position() + it->pos;
if (absParenPos >= startRange && absParenPos < endRange)
it = syntacticParens.erase(it);
else
++it;
}
}
TextDocumentLayout::setParentheses(block, syntacticParens);
}
continue;
}
if (parentheses.first.isValid() && result.line - 1 > parentheses.first.blockNumber()) {