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,20 +264,22 @@ bool handleDoxygenContinuation(QTextCursor &cursor,
if (!currentLine.at(followinPos).isSpace()) if (!currentLine.at(followinPos).isSpace())
break; 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')); QString newLine(QLatin1Char('\n'));
QTextCursor c(cursor); QTextCursor c(cursor);
c.movePosition(QTextCursor::StartOfBlock); c.movePosition(QTextCursor::StartOfBlock);
c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, offset); c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, offset);
newLine.append(c.selectedText()); newLine.append(c.selectedText());
const bool isAtAsterisk = followinPos < currentLine.length()
&& currentLine.at(followinPos) == '*';
if (currentLine.at(offset) == QLatin1Char('/')) { if (currentLine.at(offset) == QLatin1Char('/')) {
if (leadingAsterisks) if (leadingAsterisks) {
newLine.append(QLatin1String(" * ")); if (isAtAsterisk)
newLine.append(" ");
else else
newLine.append(QLatin1String(" * "));
} else {
newLine.append(QLatin1String(" ")); newLine.append(QLatin1String(" "));
}
offset += 3; offset += 3;
} else { } else {
// If '*' is not within a comment, skip. // If '*' is not within a comment, skip.
@@ -288,18 +290,19 @@ bool handleDoxygenContinuation(QTextCursor &cursor,
return false; 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) for (; offset < blockPos && currentLine.at(offset) == ' '; ++offset)
newLine.append(QLatin1Char(' ')); newLine.append(QLatin1Char(' '));
cursor.insertText(newLine); cursor.insertText(newLine);
return true; return true;
} }
}
return false; return false;
} }