diff --git a/src/plugins/texteditor/behaviorsettingswidget.cpp b/src/plugins/texteditor/behaviorsettingswidget.cpp
index 2a457596cad..4e7c70fb22c 100644
--- a/src/plugins/texteditor/behaviorsettingswidget.cpp
+++ b/src/plugins/texteditor/behaviorsettingswidget.cpp
@@ -38,7 +38,7 @@ struct BehaviorSettingsWidgetPrivate
QComboBox *smartBackspaceBehavior;
QCheckBox *autoIndent;
QCheckBox *preferSingleLineComments;
- QCheckBox *preferAfterWhitespaceComments;
+ QComboBox *commentPosition;
QGroupBox *groupBoxStorageSettings;
QGroupBox *groupBoxTyping;
QCheckBox *skipTrailingWhitespace;
@@ -96,7 +96,48 @@ 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->commentPosition = new QComboBox;
+ const QString automaticText = Tr::tr("Automatic");
+ d->commentPosition->addItem(automaticText);
+ const QString lineStartText = Tr::tr("At Line Start");
+ d->commentPosition->addItem(lineStartText);
+ const QString afterWhitespaceText = Tr::tr("After Whitespace");
+ d->commentPosition->addItem(afterWhitespaceText);
+
+ const QString generalCommentPosition = Tr::tr(
+ "Specifies where single line comments should be positioned.");
+ const QString automaticCommentPosition
+ = Tr::tr(
+ "%1: The highlight definition for the file determines the position. If no highlight "
+ "definition is available, the comment is placed after leading whitespaces.")
+ .arg(automaticText);
+ const QString lineStartCommentPosition
+ = Tr::tr("%1: The comment is placed at the start of the line.").arg(lineStartText);
+ const QString afterWhitespaceCommentPosition
+ = Tr::tr("%1: The comment is placed after leading whitespaces.").arg(afterWhitespaceText);
+
+ const QString commentPositionToolTip = QString("
\n"
+ "%1\n"
+ "\n"
+ "\n"
+ "- %2\n"
+ "
\n"
+ "\n"
+ "- %3\n"
+ "
\n"
+ "\n"
+ "- %4\n"
+ "
\n"
+ "
\n"
+ "")
+ .arg(generalCommentPosition)
+ .arg(automaticCommentPosition)
+ .arg(lineStartCommentPosition)
+ .arg(afterWhitespaceCommentPosition);
+ d->commentPosition->setToolTip(commentPositionToolTip);
+
+ auto commentPositionLabel = new QLabel(Tr::tr("Preferred comment position"));
+ commentPositionLabel->setToolTip(commentPositionToolTip);
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."));
@@ -176,7 +217,8 @@ BehaviorSettingsWidget::BehaviorSettingsWidget(QWidget *parent)
Tr::tr("Tab key performs auto-indent:"),
indent(d->tabKeyBehavior),
d->preferSingleLineComments,
- d->preferAfterWhitespaceComments
+ commentPositionLabel,
+ indent(d->commentPosition)
}.attachTo(d->groupBoxTyping);
Column {
@@ -296,7 +338,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);
+ d->commentPosition->setCurrentIndex(typingSettings.m_commentPosition);
}
void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettings) const
@@ -308,7 +350,8 @@ void BehaviorSettingsWidget::assignedTypingSettings(TypingSettings *typingSettin
(TypingSettings::TabKeyBehavior)(d->tabKeyBehavior->currentIndex());
typingSettings->m_preferSingleLineComments = d->preferSingleLineComments->isChecked();
- typingSettings->m_preferAfterWhitespaceComments = d->preferAfterWhitespaceComments->isChecked();
+ typingSettings->m_commentPosition = TypingSettings::CommentPosition(
+ d->commentPosition->currentIndex());
}
void BehaviorSettingsWidget::setAssignedStorageSettings(const StorageSettings &storageSettings)
diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp
index 6bd50ef2bdb..7add698645d 100644
--- a/src/plugins/texteditor/texteditor.cpp
+++ b/src/plugins/texteditor/texteditor.cpp
@@ -3585,11 +3585,19 @@ void TextEditorWidgetPrivate::configureGenericHighlighter(
void TextEditorWidgetPrivate::setupFromDefinition(const KSyntaxHighlighting::Definition &definition)
{
+ const TypingSettings::CommentPosition commentPosition
+ = m_document->typingSettings().m_commentPosition;
+ m_commentDefinition.isAfterWhitespace = commentPosition != TypingSettings::StartOfLine;
if (!definition.isValid())
return;
m_commentDefinition.singleLine = definition.singleLineCommentMarker();
m_commentDefinition.multiLineStart = definition.multiLineCommentMarker().first;
m_commentDefinition.multiLineEnd = definition.multiLineCommentMarker().second;
+ if (commentPosition == TypingSettings::Automatic) {
+ m_commentDefinition.isAfterWhitespace
+ = definition.singleLineCommentPosition()
+ == KSyntaxHighlighting::CommentPosition::AfterWhitespace;
+ }
q->setCodeFoldingSupported(true);
}
@@ -7698,10 +7706,8 @@ 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(),
- commentDefinition,
+ d->m_commentDefinition,
singleLine);
setMultiTextCursor(cursor);
}
@@ -7862,6 +7868,7 @@ void TextEditorWidget::setBehaviorSettings(const BehaviorSettings &bs)
void TextEditorWidget::setTypingSettings(const TypingSettings &typingSettings)
{
d->m_document->setTypingSettings(typingSettings);
+ d->setupFromDefinition(d->currentDefinition());
}
void TextEditorWidget::setStorageSettings(const StorageSettings &storageSettings)
@@ -9263,6 +9270,8 @@ BaseTextEditor *TextEditorFactoryPrivate::createEditorHelper(const TextDocumentP
textEditorWidget->d->m_hoverHandlers = m_hoverHandlers;
textEditorWidget->d->m_commentDefinition = m_commentDefinition;
+ textEditorWidget->d->m_commentDefinition.isAfterWhitespace
+ = document->typingSettings().m_commentPosition != TypingSettings::StartOfLine;
QObject::connect(textEditorWidget,
&TextEditorWidget::activateEditor,
diff --git a/src/plugins/texteditor/typingsettings.cpp b/src/plugins/texteditor/typingsettings.cpp
index 11c444a6986..3f9a42f9f7f 100644
--- a/src/plugins/texteditor/typingsettings.cpp
+++ b/src/plugins/texteditor/typingsettings.cpp
@@ -33,7 +33,7 @@ Store TypingSettings::toMap() const
{tabKeyBehaviorKey, m_tabKeyBehavior},
{smartBackspaceBehaviorKey, m_smartBackspaceBehavior},
{preferSingleLineCommentsKey, m_preferSingleLineComments},
- {preferAfterWhitespaceCommentsKey, m_preferAfterWhitespaceComments}
+ {preferAfterWhitespaceCommentsKey, m_commentPosition}
};
}
@@ -45,17 +45,19 @@ 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();
+ m_commentPosition = CommentPosition(
+ std::clamp(map.value(preferAfterWhitespaceCommentsKey, m_commentPosition).toInt(),
+ int(Automatic),
+ int(AfterWhitespace)));
}
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_preferAfterWhitespaceComments == ts.m_preferAfterWhitespaceComments;
+ && m_tabKeyBehavior == ts.m_tabKeyBehavior
+ && m_smartBackspaceBehavior == ts.m_smartBackspaceBehavior
+ && m_preferSingleLineComments == ts.m_preferSingleLineComments
+ && m_commentPosition == ts.m_commentPosition;
}
bool TypingSettings::tabShouldIndent(const QTextDocument *document,
diff --git a/src/plugins/texteditor/typingsettings.h b/src/plugins/texteditor/typingsettings.h
index f3282308675..283a665f52c 100644
--- a/src/plugins/texteditor/typingsettings.h
+++ b/src/plugins/texteditor/typingsettings.h
@@ -31,6 +31,12 @@ public:
BackspaceUnindents = 2
};
+ enum CommentPosition {
+ Automatic = 0,
+ StartOfLine = 1,
+ AfterWhitespace = 2,
+ };
+
TypingSettings();
bool tabShouldIndent(const QTextDocument *document, const QTextCursor &cursor, int *suggestedPosition) const;
@@ -48,7 +54,7 @@ public:
SmartBackspaceBehavior m_smartBackspaceBehavior;
bool m_preferSingleLineComments;
- bool m_preferAfterWhitespaceComments = false;
+ CommentPosition m_commentPosition = Automatic;
};
} // namespace TextEditor