forked from qt-creator/qt-creator
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:
@@ -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())
|
if (!definition.isValid())
|
||||||
return;
|
return;
|
||||||
@@ -103,7 +104,7 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
|
|||||||
|
|
||||||
bool hasSelection = cursor.hasSelection();
|
bool hasSelection = cursor.hasSelection();
|
||||||
|
|
||||||
if (hasSelection && definition.hasMultiLineStyle()) {
|
if (hasSelection && definition.hasMultiLineStyle() && !preferSingleLine) {
|
||||||
|
|
||||||
QString startText = startBlock.text();
|
QString startText = startBlock.text();
|
||||||
int startPos = start - startBlock.position();
|
int startPos = start - startBlock.position();
|
||||||
@@ -145,6 +146,7 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
|
|||||||
} else if (!hasSelection && !definition.hasSingleLineStyle()) {
|
} else if (!hasSelection && !definition.hasSingleLineStyle()) {
|
||||||
|
|
||||||
QString text = startBlock.text().trimmed();
|
QString text = startBlock.text().trimmed();
|
||||||
|
|
||||||
doMultiLineStyleUncomment = text.startsWith(definition.multiLineStart)
|
doMultiLineStyleUncomment = text.startsWith(definition.multiLineStart)
|
||||||
&& text.endsWith(definition.multiLineEnd);
|
&& text.endsWith(definition.multiLineEnd);
|
||||||
doMultiLineStyleComment = !doMultiLineStyleUncomment && !text.isEmpty();
|
doMultiLineStyleComment = !doMultiLineStyleUncomment && !text.isEmpty();
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ public:
|
|||||||
|
|
||||||
QTCREATOR_UTILS_EXPORT
|
QTCREATOR_UTILS_EXPORT
|
||||||
void unCommentSelection(QPlainTextEdit *edit,
|
void unCommentSelection(QPlainTextEdit *edit,
|
||||||
const CommentDefinition &definiton = CommentDefinition());
|
const CommentDefinition &definiton = CommentDefinition(),
|
||||||
|
bool preferSingleLine = false);
|
||||||
|
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
|
|||||||
@@ -175,6 +175,8 @@ void BehaviorSettingsWidget::setAssignedTypingSettings(const TypingSettings &typ
|
|||||||
d->m_ui.autoIndent->setChecked(typingSettings.m_autoIndent);
|
d->m_ui.autoIndent->setChecked(typingSettings.m_autoIndent);
|
||||||
d->m_ui.smartBackspaceBehavior->setCurrentIndex(typingSettings.m_smartBackspaceBehavior);
|
d->m_ui.smartBackspaceBehavior->setCurrentIndex(typingSettings.m_smartBackspaceBehavior);
|
||||||
d->m_ui.tabKeyBehavior->setCurrentIndex(typingSettings.m_tabKeyBehavior);
|
d->m_ui.tabKeyBehavior->setCurrentIndex(typingSettings.m_tabKeyBehavior);
|
||||||
|
|
||||||
|
d->m_ui.preferSingleLineComments->setChecked(typingSettings.m_preferSingleLineComments);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettings) const
|
void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettings) const
|
||||||
@@ -184,6 +186,8 @@ void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettin
|
|||||||
(TypingSettings::SmartBackspaceBehavior)(d->m_ui.smartBackspaceBehavior->currentIndex());
|
(TypingSettings::SmartBackspaceBehavior)(d->m_ui.smartBackspaceBehavior->currentIndex());
|
||||||
typingSettings->m_tabKeyBehavior =
|
typingSettings->m_tabKeyBehavior =
|
||||||
(TypingSettings::TabKeyBehavior)(d->m_ui.tabKeyBehavior->currentIndex());
|
(TypingSettings::TabKeyBehavior)(d->m_ui.tabKeyBehavior->currentIndex());
|
||||||
|
|
||||||
|
typingSettings->m_preferSingleLineComments = d->m_ui.preferSingleLineComments->isChecked();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BehaviorSettingsWidget::setAssignedStorageSettings(const StorageSettings &storageSettings)
|
void BehaviorSettingsWidget::setAssignedStorageSettings(const StorageSettings &storageSettings)
|
||||||
|
|||||||
@@ -37,21 +37,32 @@
|
|||||||
<bool>false</bool>
|
<bool>false</bool>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="gridLayout_3">
|
<layout class="QGridLayout" name="gridLayout_3">
|
||||||
<item row="0" column="0" colspan="2">
|
<item row="5" column="1">
|
||||||
<widget class="QCheckBox" name="autoIndent">
|
<widget class="QComboBox" name="tabKeyBehavior">
|
||||||
<property name="text">
|
<property name="sizePolicy">
|
||||||
<string>Enable automatic &indentation</string>
|
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
|
||||||
|
<horstretch>0</horstretch>
|
||||||
|
<verstretch>0</verstretch>
|
||||||
|
</sizepolicy>
|
||||||
</property>
|
</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>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="0" colspan="2">
|
<item row="3" column="1">
|
||||||
<widget class="QLabel" name="smartBackspaceLabel">
|
|
||||||
<property name="text">
|
|
||||||
<string>Backspace indentation:</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="2" column="1">
|
|
||||||
<widget class="QComboBox" name="smartBackspaceBehavior">
|
<widget class="QComboBox" name="smartBackspaceBehavior">
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
|
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
|
||||||
@@ -92,14 +103,28 @@ Specifies how backspace interacts with indentation.
|
|||||||
</item>
|
</item>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</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 &indentation</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="4" column="0" colspan="2">
|
||||||
<widget class="QLabel" name="tabKeyBehaviorLabel">
|
<widget class="QLabel" name="tabKeyBehaviorLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Tab key performs auto-indent:</string>
|
<string>Tab key performs auto-indent:</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</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">
|
<spacer name="horizontalSpacer_5">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
<enum>Qt::Horizontal</enum>
|
<enum>Qt::Horizontal</enum>
|
||||||
@@ -115,29 +140,11 @@ Specifies how backspace interacts with indentation.
|
|||||||
</property>
|
</property>
|
||||||
</spacer>
|
</spacer>
|
||||||
</item>
|
</item>
|
||||||
<item row="4" column="1">
|
<item row="6" column="0" colspan="2">
|
||||||
<widget class="QComboBox" name="tabKeyBehavior">
|
<widget class="QCheckBox" name="preferSingleLineComments">
|
||||||
<property name="sizePolicy">
|
<property name="text">
|
||||||
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
|
<string>Prefer single line comments</string>
|
||||||
<horstretch>0</horstretch>
|
|
||||||
<verstretch>0</verstretch>
|
|
||||||
</sizepolicy>
|
|
||||||
</property>
|
</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>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
|
|||||||
@@ -7310,7 +7310,8 @@ void TextEditorWidget::rewrapParagraph()
|
|||||||
|
|
||||||
void TextEditorWidget::unCommentSelection()
|
void TextEditorWidget::unCommentSelection()
|
||||||
{
|
{
|
||||||
Utils::unCommentSelection(this, d->m_commentDefinition);
|
Utils::unCommentSelection(this, d->m_commentDefinition,
|
||||||
|
d->m_document->typingSettings().m_preferSingleLineComments);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditorWidget::autoFormat()
|
void TextEditorWidget::autoFormat()
|
||||||
|
|||||||
@@ -32,6 +32,7 @@
|
|||||||
static const char autoIndentKey[] = "AutoIndent";
|
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 groupPostfix[] = "TypingSettings";
|
static const char groupPostfix[] = "TypingSettings";
|
||||||
|
|
||||||
|
|
||||||
@@ -40,7 +41,8 @@ namespace TextEditor {
|
|||||||
TypingSettings::TypingSettings():
|
TypingSettings::TypingSettings():
|
||||||
m_autoIndent(true),
|
m_autoIndent(true),
|
||||||
m_tabKeyBehavior(TabNeverIndents),
|
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(autoIndentKey), m_autoIndent);
|
||||||
map->insert(prefix + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior);
|
map->insert(prefix + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior);
|
||||||
map->insert(prefix + QLatin1String(smartBackspaceBehaviorKey), m_smartBackspaceBehavior);
|
map->insert(prefix + QLatin1String(smartBackspaceBehaviorKey), m_smartBackspaceBehavior);
|
||||||
|
|
||||||
|
map->insert(prefix + QLatin1String(preferSingleLineCommentsKey), m_preferSingleLineComments);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypingSettings::fromMap(const QString &prefix, const QVariantMap &map)
|
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)
|
m_tabKeyBehavior = (TabKeyBehavior)
|
||||||
map.value(prefix + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior).toInt();
|
map.value(prefix + QLatin1String(tabKeyBehaviorKey), m_tabKeyBehavior).toInt();
|
||||||
m_smartBackspaceBehavior = (SmartBackspaceBehavior)
|
m_smartBackspaceBehavior = (SmartBackspaceBehavior)
|
||||||
map.value(prefix + QLatin1String(smartBackspaceBehaviorKey),
|
map.value(prefix + QLatin1String(smartBackspaceBehaviorKey),
|
||||||
m_smartBackspaceBehavior).toInt();
|
m_smartBackspaceBehavior).toInt();
|
||||||
|
|
||||||
|
m_preferSingleLineComments =
|
||||||
|
map.value(prefix + QLatin1String(preferSingleLineCommentsKey), m_preferSingleLineComments).toBool();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TypingSettings::equals(const TypingSettings &ts) const
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TypingSettings::tabShouldIndent(const QTextDocument *document, const QTextCursor &cursor, int *suggestedPosition) const
|
bool TypingSettings::tabShouldIndent(const QTextDocument *document, const QTextCursor &cursor, int *suggestedPosition) const
|
||||||
|
|||||||
@@ -69,6 +69,8 @@ public:
|
|||||||
bool m_autoIndent;
|
bool m_autoIndent;
|
||||||
TabKeyBehavior m_tabKeyBehavior;
|
TabKeyBehavior m_tabKeyBehavior;
|
||||||
SmartBackspaceBehavior m_smartBackspaceBehavior;
|
SmartBackspaceBehavior m_smartBackspaceBehavior;
|
||||||
|
|
||||||
|
bool m_preferSingleLineComments;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool operator==(const TypingSettings &t1, const TypingSettings &t2) { return t1.equals(t2); }
|
inline bool operator==(const TypingSettings &t1, const TypingSettings &t2) { return t1.equals(t2); }
|
||||||
|
|||||||
Reference in New Issue
Block a user