diff --git a/src/libs/utils/uncommentselection.cpp b/src/libs/utils/uncommentselection.cpp index 2a9bf682f55..cdc271e5ef3 100644 --- a/src/libs/utils/uncommentselection.cpp +++ b/src/libs/utils/uncommentselection.cpp @@ -74,13 +74,14 @@ static bool isComment(const QString &text, int index, } -void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &definition, - bool preferSingleLine) +QTextCursor Utils::unCommentSelection(const QTextCursor &cursorIn, + const CommentDefinition &definition, + bool preferSingleLine) { if (!definition.isValid()) - return; + return cursorIn; - QTextCursor cursor = edit->textCursor(); + QTextCursor cursor = cursorIn; QTextDocument *doc = cursor.document(); cursor.beginEditBlock(); @@ -227,9 +228,9 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de cursor.endEditBlock(); + cursor = cursorIn; // adjust selection when commenting out if (hasSelection && !doMultiLineStyleUncomment && !doSingleLineStyleUncomment) { - cursor = edit->textCursor(); if (!doMultiLineStyleComment) start = startBlock.position(); // move the comment into the selection int lastSelPos = anchorIsStart ? cursor.position() : cursor.anchor(); @@ -240,6 +241,6 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de cursor.setPosition(lastSelPos); cursor.setPosition(start, QTextCursor::KeepAnchor); } - edit->setTextCursor(cursor); } + return cursor; } diff --git a/src/libs/utils/uncommentselection.h b/src/libs/utils/uncommentselection.h index 9a27c46c1bc..c937086aef8 100644 --- a/src/libs/utils/uncommentselection.h +++ b/src/libs/utils/uncommentselection.h @@ -28,6 +28,7 @@ #include "utils_global.h" #include +#include QT_BEGIN_NAMESPACE class QPlainTextEdit; @@ -57,8 +58,8 @@ public: }; QTCREATOR_UTILS_EXPORT -void unCommentSelection(QPlainTextEdit *edit, - const CommentDefinition &definiton = CommentDefinition(), - bool preferSingleLine = false); +QTextCursor unCommentSelection(const QTextCursor &cursor, + const CommentDefinition &definiton = CommentDefinition(), + bool preferSingleLine = false); } // namespace Utils diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index b68086ac97f..46077a23854 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -7336,8 +7336,11 @@ void TextEditorWidget::rewrapParagraph() void TextEditorWidget::unCommentSelection() { - Utils::unCommentSelection(this, d->m_commentDefinition, - d->m_document->typingSettings().m_preferSingleLineComments); + const bool singleLine = d->m_document->typingSettings().m_preferSingleLineComments; + const QTextCursor cursor = Utils::unCommentSelection(textCursor(), + d->m_commentDefinition, + singleLine); + setTextCursor(cursor); } void TextEditorWidget::autoFormat()