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();
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;
}
}

View File

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

View File

@@ -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)

View File

@@ -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);
}

View File

@@ -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,

View File

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