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 <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-08-22 15:35:37 +02:00
parent 818cb7f08a
commit 1537163cce
2 changed files with 40 additions and 25 deletions

View File

@@ -387,6 +387,18 @@ void DoxygenTest::testBasic_data()
" * \n" " * \n"
" */\n" " */\n"
"int a;\n") << int(CommandPrefix::Auto); "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() void DoxygenTest::testBasic()

View File

@@ -264,41 +264,44 @@ bool handleDoxygenContinuation(QTextCursor &cursor,
if (!currentLine.at(followinPos).isSpace()) if (!currentLine.at(followinPos).isSpace())
break; break;
} }
if (followinPos == currentLine.length() // a) QString newLine(QLatin1Char('\n'));
|| currentLine.at(followinPos) != QLatin1Char('*')) { // b) QTextCursor c(cursor);
// So either a) the line ended after a '*' and we need to insert a continuation, or c.movePosition(QTextCursor::StartOfBlock);
// b) we found the start of some text and we want to align the continuation to that. c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, offset);
QString newLine(QLatin1Char('\n')); newLine.append(c.selectedText());
QTextCursor c(cursor); const bool isAtAsterisk = followinPos < currentLine.length()
c.movePosition(QTextCursor::StartOfBlock); && currentLine.at(followinPos) == '*';
c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, offset); if (currentLine.at(offset) == QLatin1Char('/')) {
newLine.append(c.selectedText()); if (leadingAsterisks) {
if (currentLine.at(offset) == QLatin1Char('/')) { if (isAtAsterisk)
if (leadingAsterisks) newLine.append(" ");
newLine.append(QLatin1String(" * "));
else else
newLine.append(QLatin1String(" ")); newLine.append(QLatin1String(" * "));
offset += 3;
} else { } else {
// If '*' is not within a comment, skip. newLine.append(QLatin1String(" "));
QTextCursor cursorOnFirstNonWhiteSpace(cursor); }
const int positionOnFirstNonWhiteSpace = cursor.position() - blockPos + offset; offset += 3;
cursorOnFirstNonWhiteSpace.setPosition(positionOnFirstNonWhiteSpace); } else {
if (!CPlusPlus::MatchingText::isInCommentHelper(cursorOnFirstNonWhiteSpace)) // If '*' is not within a comment, skip.
return false; 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; int start = offset;
while (offset < blockPos && currentLine.at(offset) == QLatin1Char('*')) while (offset < blockPos && currentLine.at(offset) == QLatin1Char('*'))
++offset; ++offset;
const QChar ch = leadingAsterisks ? QLatin1Char('*') : QLatin1Char(' '); const QChar ch = leadingAsterisks ? QLatin1Char('*') : QLatin1Char(' ');
newLine.append(QString(offset - start, ch)); 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; return false;