CppEditor: Fix folding after comment

Fixes: QTCREATORBUG-5110
Change-Id: I65e0245d68ed41ab0cfc85cedbee21584b6fa90f
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2024-06-12 16:07:27 +02:00
parent e6299f510d
commit 322679db16
3 changed files with 59 additions and 5 deletions

View File

@@ -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<int>("line");
QTest::addColumn<int>("expectedFoldingIndent");
QTest::addColumn<int>("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

View File

@@ -54,6 +54,8 @@ private slots:
void test();
void testParentheses_data();
void testParentheses();
void testFoldingIndent_data();
void testFoldingIndent();
private:
QTextDocument m_doc;

View File

@@ -47,3 +47,24 @@ static void parenTest()
const char* s7 = R"(
))";
// comment
{
}
/*
*
*/
{
}
static void parenTest2()
{
parenTest();
{
}
}