TextEditor: Add option to prefer single line comments

Fixes: QTCREATORBUG-24017
Change-Id: Icf34710707b51ea9fbd348dd207950cffeedfdd3
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Jacopo Martellini
2021-02-04 10:59:05 +01:00
parent 8a243aa6ca
commit cc89f72fe5
7 changed files with 69 additions and 43 deletions

View File

@@ -74,7 +74,8 @@ static bool isComment(const QString &text, int index,
}
void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &definition)
void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &definition,
bool preferSingleLine)
{
if (!definition.isValid())
return;
@@ -103,7 +104,7 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
bool hasSelection = cursor.hasSelection();
if (hasSelection && definition.hasMultiLineStyle()) {
if (hasSelection && definition.hasMultiLineStyle() && !preferSingleLine) {
QString startText = startBlock.text();
int startPos = start - startBlock.position();
@@ -145,6 +146,7 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
} else if (!hasSelection && !definition.hasSingleLineStyle()) {
QString text = startBlock.text().trimmed();
doMultiLineStyleUncomment = text.startsWith(definition.multiLineStart)
&& text.endsWith(definition.multiLineEnd);
doMultiLineStyleComment = !doMultiLineStyleUncomment && !text.isEmpty();

View File

@@ -58,6 +58,7 @@ public:
QTCREATOR_UTILS_EXPORT
void unCommentSelection(QPlainTextEdit *edit,
const CommentDefinition &definiton = CommentDefinition());
const CommentDefinition &definiton = CommentDefinition(),
bool preferSingleLine = false);
} // namespace Utils

View File

@@ -175,6 +175,8 @@ void BehaviorSettingsWidget::setAssignedTypingSettings(const TypingSettings &typ
d->m_ui.autoIndent->setChecked(typingSettings.m_autoIndent);
d->m_ui.smartBackspaceBehavior->setCurrentIndex(typingSettings.m_smartBackspaceBehavior);
d->m_ui.tabKeyBehavior->setCurrentIndex(typingSettings.m_tabKeyBehavior);
d->m_ui.preferSingleLineComments->setChecked(typingSettings.m_preferSingleLineComments);
}
void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettings) const
@@ -184,6 +186,8 @@ void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettin
(TypingSettings::SmartBackspaceBehavior)(d->m_ui.smartBackspaceBehavior->currentIndex());
typingSettings->m_tabKeyBehavior =
(TypingSettings::TabKeyBehavior)(d->m_ui.tabKeyBehavior->currentIndex());
typingSettings->m_preferSingleLineComments = d->m_ui.preferSingleLineComments->isChecked();
}
void BehaviorSettingsWidget::setAssignedStorageSettings(const StorageSettings &storageSettings)

View File

@@ -37,21 +37,32 @@
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="autoIndent">
<property name="text">
<string>Enable automatic &amp;indentation</string>
<item row="5" column="1">
<widget class="QComboBox" name="tabKeyBehavior">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<property name="text">
<string>Never</string>
</property>
</item>
<item>
<property name="text">
<string>Always</string>
</property>
</item>
<item>
<property name="text">
<string>In Leading White Space</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QLabel" name="smartBackspaceLabel">
<property name="text">
<string>Backspace indentation:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QComboBox" name="smartBackspaceBehavior">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
@@ -92,14 +103,28 @@ Specifies how backspace interacts with indentation.
</item>
</widget>
</item>
<item row="3" column="0" colspan="2">
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="autoIndent">
<property name="text">
<string>Enable automatic &amp;indentation</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QLabel" name="tabKeyBehaviorLabel">
<property name="text">
<string>Tab key performs auto-indent:</string>
</property>
</widget>
</item>
<item row="4" column="0">
<item row="2" column="0" colspan="2">
<widget class="QLabel" name="smartBackspaceLabel">
<property name="text">
<string>Backspace indentation:</string>
</property>
</widget>
</item>
<item row="5" column="0">
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -115,29 +140,11 @@ Specifies how backspace interacts with indentation.
</property>
</spacer>
</item>
<item row="4" column="1">
<widget class="QComboBox" name="tabKeyBehavior">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<item row="6" column="0" colspan="2">
<widget class="QCheckBox" name="preferSingleLineComments">
<property name="text">
<string>Prefer single line comments</string>
</property>
<item>
<property name="text">
<string>Never</string>
</property>
</item>
<item>
<property name="text">
<string>Always</string>
</property>
</item>
<item>
<property name="text">
<string>In Leading White Space</string>
</property>
</item>
</widget>
</item>
</layout>

View File

@@ -7310,7 +7310,8 @@ void TextEditorWidget::rewrapParagraph()
void TextEditorWidget::unCommentSelection()
{
Utils::unCommentSelection(this, d->m_commentDefinition);
Utils::unCommentSelection(this, d->m_commentDefinition,
d->m_document->typingSettings().m_preferSingleLineComments);
}
void TextEditorWidget::autoFormat()

View File

@@ -32,6 +32,7 @@
static const char autoIndentKey[] = "AutoIndent";
static const char tabKeyBehaviorKey[] = "TabKeyBehavior";
static const char smartBackspaceBehaviorKey[] = "SmartBackspaceBehavior";
static const char preferSingleLineCommentsKey[] = "PreferSingleLineComments";
static const char groupPostfix[] = "TypingSettings";
@@ -40,7 +41,8 @@ namespace TextEditor {
TypingSettings::TypingSettings():
m_autoIndent(true),
m_tabKeyBehavior(TabNeverIndents),
m_smartBackspaceBehavior(BackspaceNeverIndents)
m_smartBackspaceBehavior(BackspaceNeverIndents),
m_preferSingleLineComments(false)
{
}
@@ -60,24 +62,31 @@ void TypingSettings::toMap(const QString &prefix, QVariantMap *map) const
map->insert(prefix + QLatin1String(autoIndentKey), m_autoIndent);
map->insert(prefix + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior);
map->insert(prefix + QLatin1String(smartBackspaceBehaviorKey), m_smartBackspaceBehavior);
map->insert(prefix + QLatin1String(preferSingleLineCommentsKey), m_preferSingleLineComments);
}
void TypingSettings::fromMap(const QString &prefix, const QVariantMap &map)
{
m_autoIndent = map.value(prefix + QLatin1String(autoIndentKey), m_autoIndent).toBool();
m_autoIndent =
map.value(prefix + QLatin1String(autoIndentKey), m_autoIndent).toBool();
m_tabKeyBehavior = (TabKeyBehavior)
map.value(prefix + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior).toInt();
m_smartBackspaceBehavior = (SmartBackspaceBehavior)
map.value(prefix + QLatin1String(smartBackspaceBehaviorKey),
m_smartBackspaceBehavior).toInt();
m_preferSingleLineComments =
map.value(prefix + QLatin1String(preferSingleLineCommentsKey), m_preferSingleLineComments).toBool();
}
bool TypingSettings::equals(const TypingSettings &ts) const
{
return m_autoIndent == ts.m_autoIndent
&& m_tabKeyBehavior == ts.m_tabKeyBehavior
&& m_smartBackspaceBehavior == ts.m_smartBackspaceBehavior;
&& m_smartBackspaceBehavior == ts.m_smartBackspaceBehavior
&& m_preferSingleLineComments == ts.m_preferSingleLineComments;
}
bool TypingSettings::tabShouldIndent(const QTextDocument *document, const QTextCursor &cursor, int *suggestedPosition) const

View File

@@ -69,6 +69,8 @@ public:
bool m_autoIndent;
TabKeyBehavior m_tabKeyBehavior;
SmartBackspaceBehavior m_smartBackspaceBehavior;
bool m_preferSingleLineComments;
};
inline bool operator==(const TypingSettings &t1, const TypingSettings &t2) { return t1.equals(t2); }