TextEditor: Prevent unwanted reflowing of doxygen comments

... in rewrapParagraph().
One could argue that this functionality is only for pure text and should
not be used on code, but since it was apparently contributed with
doxygen in mind, we might as well go out of our way a bit to support it
a little better.
The simple approach here is to treat a doxygen command at the start of a
line as the end of the reflow region.

Fixes: QTCREATORBUG-9739
Change-Id: I5affee9c441bd9e862bdaf38930fcf8e770b6d97
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-09-15 14:17:19 +02:00
parent e363f27288
commit 8e118e9d5e

View File

@@ -88,6 +88,7 @@
#include <QPrinter> #include <QPrinter>
#include <QPropertyAnimation> #include <QPropertyAnimation>
#include <QDrag> #include <QDrag>
#include <QRegularExpression>
#include <QSequentialAnimationGroup> #include <QSequentialAnimationGroup>
#include <QScreen> #include <QScreen>
#include <QScrollBar> #include <QScrollBar>
@@ -7195,26 +7196,35 @@ void TextEditorWidget::rewrapParagraph()
QTextCursor nextBlock = cursor; QTextCursor nextBlock = cursor;
QString commonPrefix; QString commonPrefix;
const QString doxygenPrefix("^\\s*(?:///|/\\*\\*|/\\*\\!|\\*)?[ *]+");
if (nextBlock.movePosition(QTextCursor::NextBlock)) if (nextBlock.movePosition(QTextCursor::NextBlock))
{ {
QString nText = nextBlock.block().text(); QString nText = nextBlock.block().text();
int maxLength = qMin(text.length(), nText.length()); int maxLength = qMin(text.length(), nText.length());
const auto hasDoxygenPrefix = [&] {
static const QRegularExpression pattern(doxygenPrefix);
return pattern.match(commonPrefix).hasMatch();
};
for (int i = 0; i < maxLength; ++i) { for (int i = 0; i < maxLength; ++i) {
const QChar ch = text.at(i); const QChar ch = text.at(i);
if (ch != nText[i] || ch.isLetterOrNumber()) if (ch != nText[i] || ch.isLetterOrNumber()
|| ((ch == '@' || ch == '\\' ) && hasDoxygenPrefix())) {
break; break;
}
commonPrefix.append(ch); commonPrefix.append(ch);
} }
} }
// Find end of paragraph. // Find end of paragraph.
const QRegularExpression immovableDoxygenCommand(doxygenPrefix + "[@\\\\].*");
QTC_CHECK(immovableDoxygenCommand.isValid());
while (cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor)) { while (cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor)) {
QString text = cursor.block().text(); QString text = cursor.block().text();
if (!text.contains(anyLettersOrNumbers)) if (!text.contains(anyLettersOrNumbers) || immovableDoxygenCommand.match(text).hasMatch())
break; break;
} }