Skip to content

Commit

Permalink
fix: [search functionality] Resolve issue with empty editor content
Browse files Browse the repository at this point in the history
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<QString>` 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
  • Loading branch information
Kakueeen committed Jan 7, 2025
1 parent 5f7068a commit d778f4f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
23 changes: 20 additions & 3 deletions src/plugins/find/gui/advancedsearchwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class AdvancedSearchWidgetPrivate : public QObject
WindowService *winSrv { nullptr };
MainController *controller { nullptr };
QTimer startTimer;
bool isReplaceAll { false };
};

AdvancedSearchWidgetPrivate::AdvancedSearchWidgetPrivate(AdvancedSearchWidget *qq)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SearchReplaceWorkerPrivate : public QObject
{
QString program;
QStringList arguments;
QString channelData;
std::optional<QString> channelData;
QString keyword;
SearchFlags flags;
};
Expand Down

0 comments on commit d778f4f

Please sign in to comment.