From f15cae2bacba3616936a12b0e8f7bea0e660fce0 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Mon, 23 Jan 2023 08:21:24 +0100 Subject: TextEditor: fix endless loop on adding cursor for selection Since the find of QTextDocument is line based, it is not possible to find selections spanning multiple lines in the editor. Triggering a find on a search term containing a paragraph separator returnes an invalid QTextCursor which always result in searching from the beginning of the document in the add cursor for selection logic. Prevent that by checking the selected text beforehand and add a safety net in the loop to verify that we do not start over again on an invalid TextCursor. Fixes: QTCREATORBUG-28709 Change-Id: I8c1b9d16e707fefbba6dc0a0a9ef21b8c82ebe19 Reviewed-by: Marcus Tillmanns --- src/plugins/texteditor/texteditor.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index d6bce9d5a0..3b8f5505d6 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -7055,12 +7055,13 @@ void TextEditorWidgetPrivate::addSelectionNextFindMatch() return; const QTextCursor &firstCursor = cursors.first(); - QTextDocumentFragment selection = firstCursor.selection(); + const QString selection = firstCursor.selectedText(); + if (selection.contains(QChar::ParagraphSeparator)) + return; QTextDocument *document = firstCursor.document(); - if (Utils::anyOf(cursors, [&firstCursor](const QTextCursor &c) { - return c.selection().toPlainText().toCaseFolded() - != firstCursor.selection().toPlainText().toCaseFolded(); + if (Utils::anyOf(cursors, [selection = selection.toCaseFolded()](const QTextCursor &c) { + return c.selectedText().toCaseFolded() != selection; })) { return; } @@ -7069,8 +7070,9 @@ void TextEditorWidgetPrivate::addSelectionNextFindMatch() int searchFrom = cursors.last().selectionEnd(); while (true) { - QTextCursor next = document->find(selection.toPlainText(), searchFrom, findFlags); + QTextCursor next = document->find(selection, searchFrom, findFlags); if (next.isNull()) { + QTC_ASSERT(searchFrom != 0, return); searchFrom = 0; continue; } -- cgit v1.2.1