Commit b9e618af authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Fix support for replaceAll

parent 81771f94
Pipeline #146580 passed with stages
in 5 minutes and 59 seconds
......@@ -31,26 +31,54 @@ QTextDocument::FindFlags FindUtils::convertTextEditFindFlags(TextEditFindBarBase
return flags;
}
int FindUtils::replaceAll(QTextEdit *view, const QString &str, const QString &replaceWidget, TextEditFindBarBase::FindFlags searchOptions)
int FindUtils::replaceAll(QTextEdit *view, const QString &str, const QString &replaceStr, TextEditFindBarBase::FindFlags searchOptions)
{
auto document = view->document();
QTextCursor c(document);
c.beginEditBlock();
int count = 0;
// Ignoring FindBackward when replacing all
const QTextDocument::FindFlags flags = FindUtils::convertTextEditFindFlags(searchOptions) & ~QTextDocument::FindBackward;
while (!c.isNull()) {
c = document->find(str, c, flags);
if (!c.isNull()) {
// find() selects found text, and insertText() replaces selection
c.insertText(replaceWidget);
count++;
} else {
break;
if (searchOptions & TextEditFindBarBase::FindFlag::FindRespectDiacritics) {
view->textCursor().beginEditBlock();
QTextCursor c(document);
while (!c.isNull()) {
c = document->find(str, c, flags);
if (!c.isNull()) {
// find() selects found text, and insertText() replaces selection
c.insertText(replaceStr);
count++;
} else {
break;
}
}
view->textCursor().endEditBlock();
} else {
const QString toPlainTextWithoutRespectDiacritics{FindUtils::normalize(view->toPlainText())};
const QString searchStrWithoutRespectDiacritics{FindUtils::normalize(str)};
QTextDocument documentWithoutRespectDiacritics(toPlainTextWithoutRespectDiacritics);
QTextCursor documentWithoutRespectDiacriticsTextCursor(&documentWithoutRespectDiacritics);
QTextCursor docCusor(view->textCursor());
documentWithoutRespectDiacriticsTextCursor.setPosition(0);
view->textCursor().beginEditBlock();
QTextCursor c(document);
while (!documentWithoutRespectDiacriticsTextCursor.isNull()) {
documentWithoutRespectDiacriticsTextCursor =
documentWithoutRespectDiacritics.find(searchStrWithoutRespectDiacritics, documentWithoutRespectDiacriticsTextCursor, flags);
if (!documentWithoutRespectDiacriticsTextCursor.isNull()) {
c.setPosition(documentWithoutRespectDiacriticsTextCursor.selectionStart());
c.setPosition(documentWithoutRespectDiacriticsTextCursor.selectionEnd(), QTextCursor::KeepAnchor);
// find() selects found text, and insertText() replaces selection
c.insertText(replaceStr);
documentWithoutRespectDiacriticsTextCursor.insertText(replaceStr);
count++;
} else {
break;
}
}
view->textCursor().endEditBlock();
}
c.endEditBlock();
view->setTextCursor(c);
return count;
}
......@@ -81,18 +109,20 @@ int FindUtils::replaceAll(QPlainTextEdit *view, const QString &str, const QStrin
QTextDocument documentWithoutRespectDiacritics(toPlainTextWithoutRespectDiacritics);
QTextCursor documentWithoutRespectDiacriticsTextCursor(&documentWithoutRespectDiacritics);
QTextCursor docCusor(view->textCursor());
documentWithoutRespectDiacriticsTextCursor.setPosition(docCusor.position());
documentWithoutRespectDiacriticsTextCursor.setPosition(0);
view->textCursor().beginEditBlock();
QTextCursor c(document);
while (!documentWithoutRespectDiacriticsTextCursor.isNull()) {
documentWithoutRespectDiacriticsTextCursor = document->find(searchStrWithoutRespectDiacritics, documentWithoutRespectDiacriticsTextCursor, flags);
documentWithoutRespectDiacriticsTextCursor =
documentWithoutRespectDiacritics.find(searchStrWithoutRespectDiacritics, documentWithoutRespectDiacriticsTextCursor, flags);
if (!documentWithoutRespectDiacriticsTextCursor.isNull()) {
c.setPosition(documentWithoutRespectDiacriticsTextCursor.selectionStart());
c.setPosition(documentWithoutRespectDiacriticsTextCursor.selectionEnd(), QTextCursor::KeepAnchor);
// find() selects found text, and insertText() replaces selection
c.insertText(replaceStr);
documentWithoutRespectDiacriticsTextCursor.insertText(replaceStr);
count++;
} else {
break;
......
......@@ -122,6 +122,18 @@ void PlainTextEditFindBarTest::shouldReplaceAllText_data()
QTest::newRow("wholewords-no-diacritics") << QStringLiteral("réunion reunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace")
<< flags << 3 << QStringLiteral("replace replace replace");
}
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindWholeWords;
QTest::newRow("wholewords-no-diacritics-2") << QStringLiteral("réunion réunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace")
<< flags << 3 << QStringLiteral("replace replace replace");
}
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindWholeWords;
QTest::newRow("wholewords-no-diacritics-2") << QStringLiteral("voiture voiture voiture") << QStringLiteral("réunion") << QStringLiteral("replace")
<< flags << 0 << QStringLiteral("voiture voiture voiture");
}
}
void PlainTextEditFindBarTest::shouldReplaceAllText()
......
......@@ -122,6 +122,18 @@ void RichTextEditFindBarTest::shouldReplaceAllText_data()
QTest::newRow("wholewords-no-diacritics") << QStringLiteral("réunion reunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace")
<< flags << 3 << QStringLiteral("replace replace replace");
}
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindWholeWords;
QTest::newRow("wholewords-no-diacritics-2") << QStringLiteral("réunion réunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace")
<< flags << 3 << QStringLiteral("replace replace replace");
}
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindWholeWords;
QTest::newRow("wholewords-no-diacritics-2") << QStringLiteral("voiture voiture voiture") << QStringLiteral("réunion") << QStringLiteral("replace")
<< flags << 0 << QStringLiteral("voiture voiture voiture");
}
}
void RichTextEditFindBarTest::shouldReplaceAllText()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment