Add possibility to comment selection after the white spaces

Fixes: QTCREATORBUG-29410
Change-Id: I736378e0b5fb542a6c1032a47adfb251b50ce2ef
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Xavier BESSON
2023-09-12 14:40:59 +02:00
parent 6cf4b1a3cd
commit 5cfe69ea1e
6 changed files with 47 additions and 10 deletions

View File

@@ -176,12 +176,34 @@ QTextCursor unCommentSelection(const QTextCursor &cursorIn,
} }
const int singleLineLength = definition.singleLine.length(); 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()) { for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
if (doSingleLineStyleUncomment) { if (doSingleLineStyleUncomment) {
QString text = block.text(); QString text = block.text();
int i = 0; int i = 0;
while (i <= text.size() - singleLineLength) { 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.setPosition(block.position() + i);
cursor.movePosition(QTextCursor::NextCharacter, cursor.movePosition(QTextCursor::NextCharacter,
QTextCursor::KeepAnchor, QTextCursor::KeepAnchor,
@@ -197,11 +219,13 @@ QTextCursor unCommentSelection(const QTextCursor &cursorIn,
const QString text = block.text(); const QString text = block.text();
for (QChar c : text) { for (QChar c : text) {
if (!c.isSpace()) { if (!c.isSpace()) {
if (definition.isAfterWhiteSpaces) if (definition.isAfterWhitespace) {
cursor.setPosition(block.position() + text.indexOf(c)); cursor.setPosition(block.position() + minTab);
else cursor.insertText(definition.singleLine + ' ');
} else {
cursor.setPosition(block.position()); cursor.setPosition(block.position());
cursor.insertText(definition.singleLine); cursor.insertText(definition.singleLine);
}
break; break;
} }
} }

View File

@@ -31,7 +31,7 @@ public:
bool hasMultiLineStyle() const; bool hasMultiLineStyle() const;
public: public:
bool isAfterWhiteSpaces = false; bool isAfterWhitespace = false;
QString singleLine; QString singleLine;
QString multiLineStart; QString multiLineStart;
QString multiLineEnd; QString multiLineEnd;

View File

@@ -38,6 +38,7 @@ struct BehaviorSettingsWidgetPrivate
QComboBox *smartBackspaceBehavior; QComboBox *smartBackspaceBehavior;
QCheckBox *autoIndent; QCheckBox *autoIndent;
QCheckBox *preferSingleLineComments; QCheckBox *preferSingleLineComments;
QCheckBox *preferAfterWhitespaceComments;
QGroupBox *groupBoxStorageSettings; QGroupBox *groupBoxStorageSettings;
QGroupBox *groupBoxTyping; QGroupBox *groupBoxTyping;
QCheckBox *skipTrailingWhitespace; QCheckBox *skipTrailingWhitespace;
@@ -95,6 +96,7 @@ BehaviorSettingsWidget::BehaviorSettingsWidget(QWidget *parent)
d->autoIndent = new QCheckBox(Tr::tr("Enable automatic &indentation")); d->autoIndent = new QCheckBox(Tr::tr("Enable automatic &indentation"));
d->preferSingleLineComments = new QCheckBox(Tr::tr("Prefer single line comments")); 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 = 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.")); 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), indent(d->smartBackspaceBehavior),
Tr::tr("Tab key performs auto-indent:"), Tr::tr("Tab key performs auto-indent:"),
indent(d->tabKeyBehavior), indent(d->tabKeyBehavior),
d->preferSingleLineComments d->preferSingleLineComments,
d->preferAfterWhitespaceComments
}.attachTo(d->groupBoxTyping); }.attachTo(d->groupBoxTyping);
Column { Column {
@@ -293,6 +296,7 @@ void BehaviorSettingsWidget::setAssignedTypingSettings(const TypingSettings &typ
d->tabKeyBehavior->setCurrentIndex(typingSettings.m_tabKeyBehavior); d->tabKeyBehavior->setCurrentIndex(typingSettings.m_tabKeyBehavior);
d->preferSingleLineComments->setChecked(typingSettings.m_preferSingleLineComments); d->preferSingleLineComments->setChecked(typingSettings.m_preferSingleLineComments);
d->preferAfterWhitespaceComments->setChecked(typingSettings.m_preferAfterWhitespaceComments);
} }
void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettings) const void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettings) const
@@ -304,6 +308,7 @@ void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettin
(TypingSettings::TabKeyBehavior)(d->tabKeyBehavior->currentIndex()); (TypingSettings::TabKeyBehavior)(d->tabKeyBehavior->currentIndex());
typingSettings->m_preferSingleLineComments = d->preferSingleLineComments->isChecked(); typingSettings->m_preferSingleLineComments = d->preferSingleLineComments->isChecked();
typingSettings->m_preferAfterWhitespaceComments = d->preferAfterWhitespaceComments->isChecked();
} }
void BehaviorSettingsWidget::setAssignedStorageSettings(const StorageSettings &storageSettings) void BehaviorSettingsWidget::setAssignedStorageSettings(const StorageSettings &storageSettings)

View File

@@ -7698,8 +7698,10 @@ void TextEditorWidget::rewrapParagraph()
void TextEditorWidget::unCommentSelection() void TextEditorWidget::unCommentSelection()
{ {
const bool singleLine = d->m_document->typingSettings().m_preferSingleLineComments; 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(), const MultiTextCursor cursor = Utils::unCommentSelection(multiTextCursor(),
d->m_commentDefinition, commentDefinition,
singleLine); singleLine);
setMultiTextCursor(cursor); setMultiTextCursor(cursor);
} }

View File

@@ -12,6 +12,7 @@ static const char autoIndentKey[] = "AutoIndent";
static const char tabKeyBehaviorKey[] = "TabKeyBehavior"; static const char tabKeyBehaviorKey[] = "TabKeyBehavior";
static const char smartBackspaceBehaviorKey[] = "SmartBackspaceBehavior"; static const char smartBackspaceBehaviorKey[] = "SmartBackspaceBehavior";
static const char preferSingleLineCommentsKey[] = "PreferSingleLineComments"; static const char preferSingleLineCommentsKey[] = "PreferSingleLineComments";
static const char preferAfterWhitespaceCommentsKey[] = "PreferAfterWhitespaceComments";
using namespace Utils; using namespace Utils;
@@ -31,7 +32,8 @@ Store TypingSettings::toMap() const
{autoIndentKey, m_autoIndent}, {autoIndentKey, m_autoIndent},
{tabKeyBehaviorKey, m_tabKeyBehavior}, {tabKeyBehaviorKey, m_tabKeyBehavior},
{smartBackspaceBehaviorKey, m_smartBackspaceBehavior}, {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(); smartBackspaceBehaviorKey, m_smartBackspaceBehavior).toInt();
m_preferSingleLineComments = m_preferSingleLineComments =
map.value(preferSingleLineCommentsKey, m_preferSingleLineComments).toBool(); map.value(preferSingleLineCommentsKey, m_preferSingleLineComments).toBool();
m_preferAfterWhitespaceComments =
map.value(preferAfterWhitespaceCommentsKey, m_preferAfterWhitespaceComments).toBool();
} }
bool TypingSettings::equals(const TypingSettings &ts) const bool TypingSettings::equals(const TypingSettings &ts) const
@@ -50,7 +54,8 @@ bool TypingSettings::equals(const TypingSettings &ts) const
return m_autoIndent == ts.m_autoIndent return m_autoIndent == ts.m_autoIndent
&& m_tabKeyBehavior == ts.m_tabKeyBehavior && m_tabKeyBehavior == ts.m_tabKeyBehavior
&& m_smartBackspaceBehavior == ts.m_smartBackspaceBehavior && 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, bool TypingSettings::tabShouldIndent(const QTextDocument *document,

View File

@@ -48,6 +48,7 @@ public:
SmartBackspaceBehavior m_smartBackspaceBehavior; SmartBackspaceBehavior m_smartBackspaceBehavior;
bool m_preferSingleLineComments; bool m_preferSingleLineComments;
bool m_preferAfterWhitespaceComments = false;
}; };
} // namespace TextEditor } // namespace TextEditor