From 58b0a5056c3afe922a2fe8570735da03fb9c3db5 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Fri, 22 Apr 2022 13:56:22 +0200 Subject: [PATCH] Editor: Allow selecting a group of whitespaces with double click Fixes: QTCREATORBUG-24607 Change-Id: I993e2c3a8f1054fc6787cca8e22704fb5b2ab9d1 Reviewed-by: Marcus Tillmanns Reviewed-by: Christian Stenger --- src/plugins/texteditor/texteditor.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 318464fb7f9..28dca256e62 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -5493,7 +5493,30 @@ void TextEditorWidget::mouseDoubleClickEvent(QMouseEvent *e) } } + QTextCursor oldCursor = multiTextCursor().mainCursor(); + const int oldPosition = oldCursor.position(); + QPlainTextEdit::mouseDoubleClickEvent(e); + + // QPlainTextEdit::mouseDoubleClickEvent just selects the word under the text cursor. If the + // event is triggered on a position that is inbetween two whitespaces this event selects the + // previous word or nothing if the whitespaces are at the block start. Replace this behavior + // with selecting the whitespaces starting from the previous word end to the next word. + const QChar character = characterAt(oldPosition); + const QChar prevCharacter = characterAt(oldPosition - 1); + + if (character.isSpace() && prevCharacter.isSpace()) { + if (prevCharacter != QChar::ParagraphSeparator) { + oldCursor.movePosition(QTextCursor::PreviousWord); + oldCursor.movePosition(QTextCursor::EndOfWord); + } else if (character == QChar::ParagraphSeparator) { + return; // no special handling for empty lines + } + oldCursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor); + MultiTextCursor cursor = multiTextCursor(); + cursor.replaceMainCursor(oldCursor); + setMultiTextCursor(cursor); + } } void TextEditorWidgetPrivate::setClipboardSelection()