From 1537163cce330668ec214ef9159548e7a55d09be Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 22 Aug 2024 15:35:37 +0200 Subject: [PATCH] CppEditor: Let doxygen continuation logic work also on asterisks So that the closing part of a block comment gets properly aligned. Fixes: QTCREATORBUG-31256 Change-Id: I0ce18240c478deb7efce844f8a2c27c69d983447 Reviewed-by: David Schulz --- src/plugins/cppeditor/cppdoxygen_test.cpp | 12 +++++ src/plugins/cppeditor/cppeditorwidget.cpp | 53 ++++++++++++----------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/plugins/cppeditor/cppdoxygen_test.cpp b/src/plugins/cppeditor/cppdoxygen_test.cpp index 8ef6a6fff0f..8c604693e79 100644 --- a/src/plugins/cppeditor/cppdoxygen_test.cpp +++ b/src/plugins/cppeditor/cppdoxygen_test.cpp @@ -387,6 +387,18 @@ void DoxygenTest::testBasic_data() " * \n" " */\n" "int a;\n") << int(CommandPrefix::Auto); + + QTest::newRow("continuation_on_asterisk") << _( + "bool preventFolding;\n" + "/* leading comment\n" + " * cont|*/\n" + "int a;\n" + ) << _( + "bool preventFolding;\n" + "/* leading comment\n" + " * cont\n" + " */\n" + "int a;\n") << int(CommandPrefix::Auto); } void DoxygenTest::testBasic() diff --git a/src/plugins/cppeditor/cppeditorwidget.cpp b/src/plugins/cppeditor/cppeditorwidget.cpp index 8ff27d07834..4a0f1fb429b 100644 --- a/src/plugins/cppeditor/cppeditorwidget.cpp +++ b/src/plugins/cppeditor/cppeditorwidget.cpp @@ -264,41 +264,44 @@ bool handleDoxygenContinuation(QTextCursor &cursor, if (!currentLine.at(followinPos).isSpace()) break; } - if (followinPos == currentLine.length() // a) - || currentLine.at(followinPos) != QLatin1Char('*')) { // b) - // So either a) the line ended after a '*' and we need to insert a continuation, or - // b) we found the start of some text and we want to align the continuation to that. - QString newLine(QLatin1Char('\n')); - QTextCursor c(cursor); - c.movePosition(QTextCursor::StartOfBlock); - c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, offset); - newLine.append(c.selectedText()); - if (currentLine.at(offset) == QLatin1Char('/')) { - if (leadingAsterisks) - newLine.append(QLatin1String(" * ")); + QString newLine(QLatin1Char('\n')); + QTextCursor c(cursor); + c.movePosition(QTextCursor::StartOfBlock); + c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, offset); + newLine.append(c.selectedText()); + const bool isAtAsterisk = followinPos < currentLine.length() + && currentLine.at(followinPos) == '*'; + if (currentLine.at(offset) == QLatin1Char('/')) { + if (leadingAsterisks) { + if (isAtAsterisk) + newLine.append(" "); else - newLine.append(QLatin1String(" ")); - offset += 3; + newLine.append(QLatin1String(" * ")); } else { - // If '*' is not within a comment, skip. - QTextCursor cursorOnFirstNonWhiteSpace(cursor); - const int positionOnFirstNonWhiteSpace = cursor.position() - blockPos + offset; - cursorOnFirstNonWhiteSpace.setPosition(positionOnFirstNonWhiteSpace); - if (!CPlusPlus::MatchingText::isInCommentHelper(cursorOnFirstNonWhiteSpace)) - return false; + newLine.append(QLatin1String(" ")); + } + offset += 3; + } else { + // If '*' is not within a comment, skip. + QTextCursor cursorOnFirstNonWhiteSpace(cursor); + const int positionOnFirstNonWhiteSpace = cursor.position() - blockPos + offset; + cursorOnFirstNonWhiteSpace.setPosition(positionOnFirstNonWhiteSpace); + if (!CPlusPlus::MatchingText::isInCommentHelper(cursorOnFirstNonWhiteSpace)) + return false; - // ...otherwise do the continuation + // ...otherwise do the continuation + if (!isAtAsterisk) { int start = offset; while (offset < blockPos && currentLine.at(offset) == QLatin1Char('*')) ++offset; const QChar ch = leadingAsterisks ? QLatin1Char('*') : QLatin1Char(' '); newLine.append(QString(offset - start, ch)); } - for (; offset < blockPos && currentLine.at(offset) == ' '; ++offset) - newLine.append(QLatin1Char(' ')); - cursor.insertText(newLine); - return true; } + for (; offset < blockPos && currentLine.at(offset) == ' '; ++offset) + newLine.append(QLatin1Char(' ')); + cursor.insertText(newLine); + return true; } return false;