CppEditor: Fix parentheses mismatch on comment followed by backslash

Apparently, it was mistakenly assumed that a non-zero lexer state
implies that the comment continues on the next line when it can also
mean that the line ends in a backslash. Adapt the check accordingly.

Fixes: QTCREATORBUG-29339
Change-Id: Ib239aca86dae9983d920f7715ae628ab4bf80220
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Christian Kandeler
2023-07-05 17:09:52 +02:00
parent 29ca86ce65
commit 6bf6c093cd
3 changed files with 37 additions and 2 deletions

View File

@@ -56,7 +56,8 @@ void CppHighlighter::highlightBlock(const QString &text)
const Tokens tokens = tokenize(text, initialLexerState);
lexerState = tokenize.state(); // refresh lexer state
initialLexerState &= ~0x80; // discard newline expected bit
static const auto lexerStateWithoutNewLineExpectedBit = [](int state) { return state & ~0x80; };
initialLexerState = lexerStateWithoutNewLineExpectedBit(initialLexerState);
int foldingIndent = initialBraceDepth;
if (TextBlockUserData *userData = TextDocumentLayout::textUserData(currentBlock())) {
userData->setFoldingIndent(0);
@@ -220,7 +221,8 @@ void CppHighlighter::highlightBlock(const QString &text)
if (text.length() > lastTokenEnd)
formatSpaces(text, lastTokenEnd, text.length() - lastTokenEnd);
if (!initialLexerState && lexerState && !tokens.isEmpty()) {
if (!initialLexerState && lexerStateWithoutNewLineExpectedBit(lexerState)
&& !tokens.isEmpty()) {
const Token &lastToken = tokens.last();
if (lastToken.is(T_COMMENT) || lastToken.is(T_DOXY_COMMENT)) {
insertParen({Parenthesis::Opened, QLatin1Char('+'), lastToken.utf16charsBegin()});
@@ -636,6 +638,30 @@ void CppHighlighterTest::test()
QCOMPARE(actualFormat, expectedFormat);
}
}
void CppHighlighterTest::testParentheses_data()
{
QTest::addColumn<int>("line");
QTest::addColumn<int>("expectedParenCount");
QTest::newRow("function head") << 41 << 2;
QTest::newRow("function opening brace") << 42 << 1;
QTest::newRow("loop head") << 43 << 1;
QTest::newRow("comment") << 44 << 0;
QTest::newRow("loop end") << 45 << 3;
QTest::newRow("function closing brace") << 46 << 1;
}
void CppHighlighterTest::testParentheses()
{
QFETCH(int, line);
QFETCH(int, expectedParenCount);
QTextBlock block = m_doc.findBlockByNumber(line - 1);
QVERIFY(block.isValid());
QCOMPARE(TextDocumentLayout::parentheses(block).count(), expectedParenCount);
}
} // namespace Internal
#endif // WITH_TESTS

View File

@@ -52,6 +52,8 @@ public:
private slots:
void test_data();
void test();
void testParentheses_data();
void testParentheses();
private:
QTextDocument m_doc;

View File

@@ -37,3 +37,10 @@ const auto s5 = uR"("o
ne")"_w;
const auto s6 = u"o\
ne"_w;
static void parenTest()
{
do {
/* comment */ \
} while (false);
}