From 63fbaedc0389754b23f15c12cf1541b9785350c3 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Mon, 20 Sep 2021 15:22:05 +0200 Subject: [PATCH] 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 --- src/plugins/cppeditor/semantichighlighter.cpp | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/plugins/cppeditor/semantichighlighter.cpp b/src/plugins/cppeditor/semantichighlighter.cpp index 830efb08675..b4c1a6ff1ff 100644 --- a/src/plugins/cppeditor/semantichighlighter.cpp +++ b/src/plugins/cppeditor/semantichighlighter.cpp @@ -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()) {