diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp index 656173feee2..f562d9afece 100644 --- a/src/plugins/cppeditor/cpphighlighter.cpp +++ b/src/plugins/cppeditor/cpphighlighter.cpp @@ -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("line"); + QTest::addColumn("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 diff --git a/src/plugins/cppeditor/cpphighlighter.h b/src/plugins/cppeditor/cpphighlighter.h index 1728ffeb777..e2fac20bba6 100644 --- a/src/plugins/cppeditor/cpphighlighter.h +++ b/src/plugins/cppeditor/cpphighlighter.h @@ -52,6 +52,8 @@ public: private slots: void test_data(); void test(); + void testParentheses_data(); + void testParentheses(); private: QTextDocument m_doc; diff --git a/src/plugins/cppeditor/testcases/highlightingtestcase.cpp b/src/plugins/cppeditor/testcases/highlightingtestcase.cpp index 53ffcc1b55e..d1ec061aa29 100644 --- a/src/plugins/cppeditor/testcases/highlightingtestcase.cpp +++ b/src/plugins/cppeditor/testcases/highlightingtestcase.cpp @@ -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); +}