From cf81ff2a29a75c1c2028c57324eaf5e813cd57d8 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Mon, 11 Apr 2022 14:43:47 +0200 Subject: [PATCH] texteditor: Add Select Next Match function Adds an Action to select the next occurrence(s) of the currently selected text. Change-Id: I37e23a3c2d1651ec4898fac53d75044d92ed7079 Reviewed-by: David Schulz --- src/plugins/texteditor/texteditor.cpp | 40 +++++++++++++++++++ src/plugins/texteditor/texteditor.h | 1 + .../texteditor/texteditoractionhandler.cpp | 4 ++ src/plugins/texteditor/texteditorconstants.h | 1 + 4 files changed, 46 insertions(+) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index d07dc79d9e3..8951630dd53 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -529,6 +529,7 @@ public: void handleBackspaceKey(); void moveLineUpDown(bool up); void copyLineUpDown(bool up); + void addSelectionNextFindMatch(); void saveCurrentCursorPositionForNavigation(); void updateHighlights(); void updateCurrentLineInScrollbar(); @@ -6718,6 +6719,40 @@ void TextEditorWidget::copyLine() copy(); } +void TextEditorWidgetPrivate::addSelectionNextFindMatch() +{ + MultiTextCursor cursor = q->multiTextCursor(); + const QList cursors = cursor.cursors(); + + if (cursor.cursorCount() == 0 || !cursors.first().hasSelection()) + return; + + const QTextCursor &firstCursor = cursors.first(); + QTextDocumentFragment selection = firstCursor.selection(); + QTextDocument *document = firstCursor.document(); + + if (Utils::anyOf(cursors, [&firstCursor](const QTextCursor &c) { + return c.selection().toPlainText().toCaseFolded() + != firstCursor.selection().toPlainText().toCaseFolded(); + })) { + return; + } + + int searchFrom = cursors.last().selectionEnd(); + while (true) { + QTextCursor next = document->find(selection.toPlainText(), searchFrom); + if (next.isNull()) { + searchFrom = 0; + continue; + } + if (next.selectionStart() == firstCursor.selectionStart()) + break; + cursor.addCursor(next); + q->setMultiTextCursor(cursor); + break; + } +} + void TextEditorWidgetPrivate::duplicateSelection(bool comment) { if (comment && !m_commentDefinition.hasMultiLineStyle()) @@ -6762,6 +6797,11 @@ void TextEditorWidget::duplicateSelection() d->duplicateSelection(false); } +void TextEditorWidget::addSelectionNextFindMatch() +{ + d->addSelectionNextFindMatch(); +} + void TextEditorWidget::duplicateSelectionAndComment() { d->duplicateSelection(true); diff --git a/src/plugins/texteditor/texteditor.h b/src/plugins/texteditor/texteditor.h index 71759cb17b2..8489ccd7683 100644 --- a/src/plugins/texteditor/texteditor.h +++ b/src/plugins/texteditor/texteditor.h @@ -384,6 +384,7 @@ public: void updateTextCodecLabel(); void selectLineEnding(int index); void updateTextLineEndingLabel(); + void addSelectionNextFindMatch(); void gotoBlockStart(); void gotoBlockEnd(); diff --git a/src/plugins/texteditor/texteditoractionhandler.cpp b/src/plugins/texteditor/texteditoractionhandler.cpp index 98b66f10008..ff19f96078e 100644 --- a/src/plugins/texteditor/texteditoractionhandler.cpp +++ b/src/plugins/texteditor/texteditoractionhandler.cpp @@ -329,6 +329,10 @@ void TextEditorActionHandlerPrivate::createActions() [] (TextEditorWidget *w) { w->copyLine(); }, false, tr("Copy &Line"), QKeySequence(tr("Ctrl+Ins")), G_EDIT_TEXT, advancedEditMenu); + registerAction(ADD_SELECT_NEXT_FIND_MATCH, + [] (TextEditorWidget *w) { w->addSelectionNextFindMatch(); }, false, tr("Add Selection to Next Find Match"), + QKeySequence(tr("Ctrl+D")), + G_EDIT_TEXT, advancedEditMenu); m_modifyingActions << registerAction(DUPLICATE_SELECTION, [] (TextEditorWidget *w) { w->duplicateSelection(); }, false, tr("&Duplicate Selection"), QKeySequence(), diff --git a/src/plugins/texteditor/texteditorconstants.h b/src/plugins/texteditor/texteditorconstants.h index 060c521a400..bb95b6f93d3 100644 --- a/src/plugins/texteditor/texteditorconstants.h +++ b/src/plugins/texteditor/texteditorconstants.h @@ -168,6 +168,7 @@ const char LOWERCASE_SELECTION[] = "TextEditor.LowercaseSelection"; const char SORT_SELECTED_LINES[] = "TextEditor.SortSelectedLines"; const char CUT_LINE[] = "TextEditor.CutLine"; const char COPY_LINE[] = "TextEditor.CopyLine"; +const char ADD_SELECT_NEXT_FIND_MATCH[] = "TextEditor.AddSelectionNextFindMatch"; const char DUPLICATE_SELECTION[] = "TextEditor.DuplicateSelection"; const char DUPLICATE_SELECTION_AND_COMMENT[] = "TextEditor.DuplicateSelectionAndComment"; const char DELETE_LINE[] = "TextEditor.DeleteLine";