From d778f4f561e4146d3d9405076f153aa91d9e7ab7 Mon Sep 17 00:00:00 2001 From: Liu Zhangjian Date: Tue, 7 Jan 2025 14:05:45 +0800 Subject: [PATCH] fix: [search functionality] Resolve issue with empty editor content This commit addresses a bug where the search functionality would not work when the editor content was empty. The following changes were made: - Changed the `channelData` type to `std::optional` to handle cases where there may not be any data to send. These changes ensure that the search operation can gracefully handle empty input, improving the overall robustness of the search feature. Log: Fix search issue when editor content is empty Bug: https://pms.uniontech.com/bug-view-298499.html --- src/plugins/find/gui/advancedsearchwidget.cpp | 23 ++++++++++++++++--- .../worker/searchreplaceworker.cpp | 4 ++-- .../worker/searchreplaceworker_p.h | 2 +- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/plugins/find/gui/advancedsearchwidget.cpp b/src/plugins/find/gui/advancedsearchwidget.cpp index 41cdfe5a2..e53afe5b9 100644 --- a/src/plugins/find/gui/advancedsearchwidget.cpp +++ b/src/plugins/find/gui/advancedsearchwidget.cpp @@ -88,6 +88,7 @@ class AdvancedSearchWidgetPrivate : public QObject WindowService *winSrv { nullptr }; MainController *controller { nullptr }; QTimer startTimer; + bool isReplaceAll { false }; }; AdvancedSearchWidgetPrivate::AdvancedSearchWidgetPrivate(AdvancedSearchWidget *qq) @@ -179,7 +180,7 @@ DToolButton *AdvancedSearchWidgetPrivate::registerOperator(const QIcon &icon, co btn->setIconSize({ 16, 16 }); btn->setToolTip(description); connect(btn, &DToolButton::clicked, this, handler); - + winSrv->registerWidgetToDockHeader(MWNA_ADVANCEDSEARCH, btn); return btn; } @@ -461,19 +462,35 @@ void AdvancedSearchWidgetPrivate::handleReplaceAll() } // 0: cancel 1:contionue int code = showMessage(msg); - if (1 == code) + if (1 == code) { + isReplaceAll = true; replace(resultMap); + } } void AdvancedSearchWidgetPrivate::handleReplaceFinished() { searchSpinner->setVisible(false); searchSpinner->stop(); + if (isReplaceAll) { + isReplaceAll = false; + const auto &results = resultWidget->allResult(); + const auto &items = results.values(); + const int resultCount = std::accumulate(items.cbegin(), items.cend(), 0, + [](int sum, const FindItemList &list) { + return sum + list.size(); + }); + QString msg = AdvancedSearchWidget::tr("Replaced %1 occurrences across %2 files with \"%3\""); + resultWidget->clear(); + resultWidget->showMessage(msg.arg(QString::number(results.count()), + QString::number(resultCount), + replaceEdit->text())); + } } AdvancedSearchWidget::AdvancedSearchWidget(QWidget *parent) : QWidget(parent), - d(new AdvancedSearchWidgetPrivate(this)) + d(new AdvancedSearchWidgetPrivate(this)) { d->initUI(); d->initConnection(); diff --git a/src/plugins/find/maincontroller/worker/searchreplaceworker.cpp b/src/plugins/find/maincontroller/worker/searchreplaceworker.cpp index 4ced584b3..68a029d8e 100644 --- a/src/plugins/find/maincontroller/worker/searchreplaceworker.cpp +++ b/src/plugins/find/maincontroller/worker/searchreplaceworker.cpp @@ -43,8 +43,8 @@ void SearchReplaceWorkerPrivate::startNextJob() process->setProgram(job.program); process->setArguments(job.arguments); process->start(); - if (!job.channelData.isEmpty()) { - process->write(job.channelData.toUtf8()); + if (job.channelData.has_value()) { + process->write(job.channelData->toUtf8()); process->closeWriteChannel(); } } diff --git a/src/plugins/find/maincontroller/worker/searchreplaceworker_p.h b/src/plugins/find/maincontroller/worker/searchreplaceworker_p.h index 7b89b56a1..3828c0438 100644 --- a/src/plugins/find/maincontroller/worker/searchreplaceworker_p.h +++ b/src/plugins/find/maincontroller/worker/searchreplaceworker_p.h @@ -17,7 +17,7 @@ class SearchReplaceWorkerPrivate : public QObject { QString program; QStringList arguments; - QString channelData; + std::optional channelData; QString keyword; SearchFlags flags; };