From 5cfe69ea1ea1b066ccbcba3d5c76e69ae91ff661 Mon Sep 17 00:00:00 2001 From: Xavier BESSON Date: Tue, 12 Sep 2023 14:40:59 +0200 Subject: [PATCH] Add possibility to comment selection after the white spaces Fixes: QTCREATORBUG-29410 Change-Id: I736378e0b5fb542a6c1032a47adfb251b50ce2ef Reviewed-by: David Schulz Reviewed-by: --- src/libs/utils/uncommentselection.cpp | 34 ++++++++++++++++--- src/libs/utils/uncommentselection.h | 2 +- .../texteditor/behaviorsettingswidget.cpp | 7 +++- src/plugins/texteditor/texteditor.cpp | 4 ++- src/plugins/texteditor/typingsettings.cpp | 9 +++-- src/plugins/texteditor/typingsettings.h | 1 + 6 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/libs/utils/uncommentselection.cpp b/src/libs/utils/uncommentselection.cpp index 5bba136cdf1..8cce4c86a02 100644 --- a/src/libs/utils/uncommentselection.cpp +++ b/src/libs/utils/uncommentselection.cpp @@ -176,12 +176,34 @@ QTextCursor unCommentSelection(const QTextCursor &cursorIn, } const int singleLineLength = definition.singleLine.length(); + unsigned int minTab = -1; + if (definition.isAfterWhitespace && !doSingleLineStyleUncomment) { + for (QTextBlock block = startBlock; block != endBlock && minTab != 0; block = block.next()) { + QTextCursor c(block); + if (doc->characterAt(block.position()).isSpace()) { + c.movePosition(QTextCursor::NextWord); + if (c.block() != block) // ignore empty lines + continue; + } + const int pos = c.positionInBlock(); + if (pos < minTab) + minTab = pos; + } + } for (QTextBlock block = startBlock; block != endBlock; block = block.next()) { if (doSingleLineStyleUncomment) { QString text = block.text(); int i = 0; while (i <= text.size() - singleLineLength) { - if (isComment(text, i, definition.singleLine)) { + if (definition.isAfterWhitespace + && isComment(text, i, definition.singleLine + ' ')) { + cursor.setPosition(block.position() + i); + cursor.movePosition(QTextCursor::NextCharacter, + QTextCursor::KeepAnchor, + singleLineLength + 1); + cursor.removeSelectedText(); + break; + } else if (isComment(text, i, definition.singleLine)) { cursor.setPosition(block.position() + i); cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, @@ -197,11 +219,13 @@ QTextCursor unCommentSelection(const QTextCursor &cursorIn, const QString text = block.text(); for (QChar c : text) { if (!c.isSpace()) { - if (definition.isAfterWhiteSpaces) - cursor.setPosition(block.position() + text.indexOf(c)); - else + if (definition.isAfterWhitespace) { + cursor.setPosition(block.position() + minTab); + cursor.insertText(definition.singleLine + ' '); + } else { cursor.setPosition(block.position()); - cursor.insertText(definition.singleLine); + cursor.insertText(definition.singleLine); + } break; } } diff --git a/src/libs/utils/uncommentselection.h b/src/libs/utils/uncommentselection.h index a309b028aa4..b6836ec5101 100644 --- a/src/libs/utils/uncommentselection.h +++ b/src/libs/utils/uncommentselection.h @@ -31,7 +31,7 @@ public: bool hasMultiLineStyle() const; public: - bool isAfterWhiteSpaces = false; + bool isAfterWhitespace = false; QString singleLine; QString multiLineStart; QString multiLineEnd; diff --git a/src/plugins/texteditor/behaviorsettingswidget.cpp b/src/plugins/texteditor/behaviorsettingswidget.cpp index 144afa6eb23..2a457596cad 100644 --- a/src/plugins/texteditor/behaviorsettingswidget.cpp +++ b/src/plugins/texteditor/behaviorsettingswidget.cpp @@ -38,6 +38,7 @@ struct BehaviorSettingsWidgetPrivate QComboBox *smartBackspaceBehavior; QCheckBox *autoIndent; QCheckBox *preferSingleLineComments; + QCheckBox *preferAfterWhitespaceComments; QGroupBox *groupBoxStorageSettings; QGroupBox *groupBoxTyping; QCheckBox *skipTrailingWhitespace; @@ -95,6 +96,7 @@ BehaviorSettingsWidget::BehaviorSettingsWidget(QWidget *parent) d->autoIndent = new QCheckBox(Tr::tr("Enable automatic &indentation")); d->preferSingleLineComments = new QCheckBox(Tr::tr("Prefer single line comments")); + d->preferAfterWhitespaceComments = new QCheckBox(Tr::tr("Prefer comments after whitespace")); d->skipTrailingWhitespace = new QCheckBox(Tr::tr("Skip clean whitespace for file types:")); d->skipTrailingWhitespace->setToolTip(Tr::tr("For the file patterns listed, do not trim trailing whitespace.")); @@ -173,7 +175,8 @@ BehaviorSettingsWidget::BehaviorSettingsWidget(QWidget *parent) indent(d->smartBackspaceBehavior), Tr::tr("Tab key performs auto-indent:"), indent(d->tabKeyBehavior), - d->preferSingleLineComments + d->preferSingleLineComments, + d->preferAfterWhitespaceComments }.attachTo(d->groupBoxTyping); Column { @@ -293,6 +296,7 @@ void BehaviorSettingsWidget::setAssignedTypingSettings(const TypingSettings &typ d->tabKeyBehavior->setCurrentIndex(typingSettings.m_tabKeyBehavior); d->preferSingleLineComments->setChecked(typingSettings.m_preferSingleLineComments); + d->preferAfterWhitespaceComments->setChecked(typingSettings.m_preferAfterWhitespaceComments); } void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettings) const @@ -304,6 +308,7 @@ void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettin (TypingSettings::TabKeyBehavior)(d->tabKeyBehavior->currentIndex()); typingSettings->m_preferSingleLineComments = d->preferSingleLineComments->isChecked(); + typingSettings->m_preferAfterWhitespaceComments = d->preferAfterWhitespaceComments->isChecked(); } void BehaviorSettingsWidget::setAssignedStorageSettings(const StorageSettings &storageSettings) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 6cf2221da7b..6bd50ef2bdb 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -7698,8 +7698,10 @@ void TextEditorWidget::rewrapParagraph() void TextEditorWidget::unCommentSelection() { const bool singleLine = d->m_document->typingSettings().m_preferSingleLineComments; + CommentDefinition commentDefinition = d->m_commentDefinition; + commentDefinition.isAfterWhitespace = d->m_document->typingSettings().m_preferAfterWhitespaceComments; const MultiTextCursor cursor = Utils::unCommentSelection(multiTextCursor(), - d->m_commentDefinition, + commentDefinition, singleLine); setMultiTextCursor(cursor); } diff --git a/src/plugins/texteditor/typingsettings.cpp b/src/plugins/texteditor/typingsettings.cpp index 731f922a9dd..11c444a6986 100644 --- a/src/plugins/texteditor/typingsettings.cpp +++ b/src/plugins/texteditor/typingsettings.cpp @@ -12,6 +12,7 @@ static const char autoIndentKey[] = "AutoIndent"; static const char tabKeyBehaviorKey[] = "TabKeyBehavior"; static const char smartBackspaceBehaviorKey[] = "SmartBackspaceBehavior"; static const char preferSingleLineCommentsKey[] = "PreferSingleLineComments"; +static const char preferAfterWhitespaceCommentsKey[] = "PreferAfterWhitespaceComments"; using namespace Utils; @@ -31,7 +32,8 @@ Store TypingSettings::toMap() const {autoIndentKey, m_autoIndent}, {tabKeyBehaviorKey, m_tabKeyBehavior}, {smartBackspaceBehaviorKey, m_smartBackspaceBehavior}, - {preferSingleLineCommentsKey, m_preferSingleLineComments} + {preferSingleLineCommentsKey, m_preferSingleLineComments}, + {preferAfterWhitespaceCommentsKey, m_preferAfterWhitespaceComments} }; } @@ -43,6 +45,8 @@ void TypingSettings::fromMap(const Store &map) smartBackspaceBehaviorKey, m_smartBackspaceBehavior).toInt(); m_preferSingleLineComments = map.value(preferSingleLineCommentsKey, m_preferSingleLineComments).toBool(); + m_preferAfterWhitespaceComments = + map.value(preferAfterWhitespaceCommentsKey, m_preferAfterWhitespaceComments).toBool(); } bool TypingSettings::equals(const TypingSettings &ts) const @@ -50,7 +54,8 @@ bool TypingSettings::equals(const TypingSettings &ts) const return m_autoIndent == ts.m_autoIndent && m_tabKeyBehavior == ts.m_tabKeyBehavior && m_smartBackspaceBehavior == ts.m_smartBackspaceBehavior - && m_preferSingleLineComments == ts.m_preferSingleLineComments; + && m_preferSingleLineComments == ts.m_preferSingleLineComments + && m_preferAfterWhitespaceComments == ts.m_preferAfterWhitespaceComments; } bool TypingSettings::tabShouldIndent(const QTextDocument *document, diff --git a/src/plugins/texteditor/typingsettings.h b/src/plugins/texteditor/typingsettings.h index 03d48610f39..f3282308675 100644 --- a/src/plugins/texteditor/typingsettings.h +++ b/src/plugins/texteditor/typingsettings.h @@ -48,6 +48,7 @@ public: SmartBackspaceBehavior m_smartBackspaceBehavior; bool m_preferSingleLineComments; + bool m_preferAfterWhitespaceComments = false; }; } // namespace TextEditor