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 <david.schulz@qt.io>
This commit is contained in:
Marcus Tillmanns
2022-04-11 14:43:47 +02:00
parent 70dde948ed
commit cf81ff2a29
4 changed files with 46 additions and 0 deletions

View File

@@ -529,6 +529,7 @@ public:
void handleBackspaceKey(); void handleBackspaceKey();
void moveLineUpDown(bool up); void moveLineUpDown(bool up);
void copyLineUpDown(bool up); void copyLineUpDown(bool up);
void addSelectionNextFindMatch();
void saveCurrentCursorPositionForNavigation(); void saveCurrentCursorPositionForNavigation();
void updateHighlights(); void updateHighlights();
void updateCurrentLineInScrollbar(); void updateCurrentLineInScrollbar();
@@ -6718,6 +6719,40 @@ void TextEditorWidget::copyLine()
copy(); copy();
} }
void TextEditorWidgetPrivate::addSelectionNextFindMatch()
{
MultiTextCursor cursor = q->multiTextCursor();
const QList<QTextCursor> 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) void TextEditorWidgetPrivate::duplicateSelection(bool comment)
{ {
if (comment && !m_commentDefinition.hasMultiLineStyle()) if (comment && !m_commentDefinition.hasMultiLineStyle())
@@ -6762,6 +6797,11 @@ void TextEditorWidget::duplicateSelection()
d->duplicateSelection(false); d->duplicateSelection(false);
} }
void TextEditorWidget::addSelectionNextFindMatch()
{
d->addSelectionNextFindMatch();
}
void TextEditorWidget::duplicateSelectionAndComment() void TextEditorWidget::duplicateSelectionAndComment()
{ {
d->duplicateSelection(true); d->duplicateSelection(true);

View File

@@ -384,6 +384,7 @@ public:
void updateTextCodecLabel(); void updateTextCodecLabel();
void selectLineEnding(int index); void selectLineEnding(int index);
void updateTextLineEndingLabel(); void updateTextLineEndingLabel();
void addSelectionNextFindMatch();
void gotoBlockStart(); void gotoBlockStart();
void gotoBlockEnd(); void gotoBlockEnd();

View File

@@ -329,6 +329,10 @@ void TextEditorActionHandlerPrivate::createActions()
[] (TextEditorWidget *w) { w->copyLine(); }, false, tr("Copy &Line"), [] (TextEditorWidget *w) { w->copyLine(); }, false, tr("Copy &Line"),
QKeySequence(tr("Ctrl+Ins")), QKeySequence(tr("Ctrl+Ins")),
G_EDIT_TEXT, advancedEditMenu); 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, m_modifyingActions << registerAction(DUPLICATE_SELECTION,
[] (TextEditorWidget *w) { w->duplicateSelection(); }, false, tr("&Duplicate Selection"), [] (TextEditorWidget *w) { w->duplicateSelection(); }, false, tr("&Duplicate Selection"),
QKeySequence(), QKeySequence(),

View File

@@ -168,6 +168,7 @@ const char LOWERCASE_SELECTION[] = "TextEditor.LowercaseSelection";
const char SORT_SELECTED_LINES[] = "TextEditor.SortSelectedLines"; const char SORT_SELECTED_LINES[] = "TextEditor.SortSelectedLines";
const char CUT_LINE[] = "TextEditor.CutLine"; const char CUT_LINE[] = "TextEditor.CutLine";
const char COPY_LINE[] = "TextEditor.CopyLine"; 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[] = "TextEditor.DuplicateSelection";
const char DUPLICATE_SELECTION_AND_COMMENT[] = "TextEditor.DuplicateSelectionAndComment"; const char DUPLICATE_SELECTION_AND_COMMENT[] = "TextEditor.DuplicateSelectionAndComment";
const char DELETE_LINE[] = "TextEditor.DeleteLine"; const char DELETE_LINE[] = "TextEditor.DeleteLine";