From 8e118e9d5ec92cdfdb2fb70d2d7ef3690cf89232 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 15 Sep 2022 14:17:19 +0200 Subject: [PATCH] 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: Reviewed-by: David Schulz --- src/plugins/texteditor/texteditor.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index a0da4d2f1c0..4079b73c6b2 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -88,6 +88,7 @@ #include #include #include +#include #include #include #include @@ -7195,26 +7196,35 @@ void TextEditorWidget::rewrapParagraph() QTextCursor nextBlock = cursor; QString commonPrefix; + const QString doxygenPrefix("^\\s*(?:///|/\\*\\*|/\\*\\!|\\*)?[ *]+"); if (nextBlock.movePosition(QTextCursor::NextBlock)) { QString nText = nextBlock.block().text(); 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) { const QChar ch = text.at(i); - if (ch != nText[i] || ch.isLetterOrNumber()) + if (ch != nText[i] || ch.isLetterOrNumber() + || ((ch == '@' || ch == '\\' ) && hasDoxygenPrefix())) { break; + } commonPrefix.append(ch); } } - // Find end of paragraph. + const QRegularExpression immovableDoxygenCommand(doxygenPrefix + "[@\\\\].*"); + QTC_CHECK(immovableDoxygenCommand.isValid()); while (cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor)) { QString text = cursor.block().text(); - if (!text.contains(anyLettersOrNumbers)) + if (!text.contains(anyLettersOrNumbers) || immovableDoxygenCommand.match(text).hasMatch()) break; }