diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp index 952d74707a2..ff8ebc53d3b 100644 --- a/src/plugins/cppeditor/cpphighlighter.cpp +++ b/src/plugins/cppeditor/cpphighlighter.cpp @@ -81,8 +81,6 @@ void CppHighlighter::highlightBlock(const QString &text) return; } - const int firstNonSpace = tokens.first().utf16charsBegin(); - // Keep "semantic parentheses". Parentheses parentheses; if (TextBlockUserData *userData = TextDocumentLayout::textUserData(currentBlock())) { @@ -121,9 +119,16 @@ void CppHighlighter::highlightBlock(const QString &text) if (tk.is(T_LBRACE)) { ++braceDepth; - // if a folding block opens at the beginning of a line, treat the entire line - // as if it were inside the folding block - if (tk.utf16charsBegin() == firstNonSpace) { + // if a folding block opens at the beginning of a line, treat the line before + // as if it were inside the folding block except if it is a comment or the line does + // end with ; + const int firstNonSpace = tokens.first().utf16charsBegin(); + const QString prevBlockText = currentBlock().previous().isValid() + ? currentBlock().previous().text().trimmed() + : QString(); + if (!prevBlockText.isEmpty() && !prevBlockText.startsWith("//") + && !prevBlockText.endsWith("*/") && !prevBlockText.endsWith(";") + && tk.utf16charsBegin() == firstNonSpace) { ++foldingIndent; TextDocumentLayout::userData(currentBlock())->setFoldingStartIncluded(true); } @@ -653,6 +658,32 @@ void CppHighlighterTest::testParentheses() QCOMPARE(TextDocumentLayout::parentheses(block).count(), expectedParenCount); } +void CppHighlighterTest::testFoldingIndent_data() +{ + QTest::addColumn("line"); + QTest::addColumn("expectedFoldingIndent"); + QTest::addColumn("expectedFoldingIndentNextLine"); + + QTest::newRow("braces after one line comment") << 52 << 0 << 1; + QTest::newRow("braces after multiline comment") << 59 << 0 << 1; + QTest::newRow("braces after completed line") << 67 << 1 << 2; +} + +void CppHighlighterTest::testFoldingIndent() +{ + QFETCH(int, line); + QFETCH(int, expectedFoldingIndent); + QFETCH(int, expectedFoldingIndentNextLine); + + QTextBlock block = m_doc.findBlockByNumber(line - 1); + QVERIFY(block.isValid()); + QCOMPARE(TextDocumentLayout::foldingIndent(block), expectedFoldingIndent); + + QTextBlock nextBlock = m_doc.findBlockByNumber(line); + QVERIFY(nextBlock.isValid()); + QCOMPARE(TextDocumentLayout::foldingIndent(nextBlock), expectedFoldingIndentNextLine); +} + } // namespace Internal #endif // WITH_TESTS diff --git a/src/plugins/cppeditor/cpphighlighter.h b/src/plugins/cppeditor/cpphighlighter.h index 5a6af63adaf..70fe8525c2c 100644 --- a/src/plugins/cppeditor/cpphighlighter.h +++ b/src/plugins/cppeditor/cpphighlighter.h @@ -54,6 +54,8 @@ private slots: void test(); void testParentheses_data(); void testParentheses(); + void testFoldingIndent_data(); + void testFoldingIndent(); private: QTextDocument m_doc; diff --git a/src/plugins/cppeditor/testcases/highlightingtestcase.cpp b/src/plugins/cppeditor/testcases/highlightingtestcase.cpp index a9102ce4578..92b4e21a260 100644 --- a/src/plugins/cppeditor/testcases/highlightingtestcase.cpp +++ b/src/plugins/cppeditor/testcases/highlightingtestcase.cpp @@ -47,3 +47,24 @@ static void parenTest() const char* s7 = R"( ))"; + +// comment +{ + +} + +/* + * + */ +{ + +} + +static void parenTest2() +{ + + parenTest(); + { + + } +}