From ddcf66eb3641171ca21cdf2d0facbe6df10c910e Mon Sep 17 00:00:00 2001 From: liuzhangjian Date: Mon, 3 Jun 2024 16:06:48 +0800 Subject: [PATCH] refactor: [search] Optimize advanced search logic Optimize advanced search logic --- src/common/find/abstractdocumentfind.h | 10 +- src/plugins/find/constants.h | 10 +- src/plugins/find/findtoolwindow.cpp | 117 +++++++----------- src/plugins/find/findtoolwindow.h | 2 +- src/plugins/find/searchresultwindow.cpp | 18 +-- src/plugins/find/searchresultwindow.h | 4 +- src/plugins/find/transceiver/findreceiver.cpp | 70 ----------- src/plugins/find/transceiver/findreceiver.h | 38 ------ src/plugins/find/util/searcreplacehworker.cpp | 9 +- 9 files changed, 67 insertions(+), 211 deletions(-) delete mode 100644 src/plugins/find/transceiver/findreceiver.cpp delete mode 100644 src/plugins/find/transceiver/findreceiver.h diff --git a/src/common/find/abstractdocumentfind.h b/src/common/find/abstractdocumentfind.h index 5bcafbd01..a3af919dd 100644 --- a/src/common/find/abstractdocumentfind.h +++ b/src/common/find/abstractdocumentfind.h @@ -18,11 +18,11 @@ class AbstractDocumentFind : public QObject virtual void findNext(const QString &txt) = 0; virtual void findPrevious(const QString &txt) = 0; - virtual void replace(const QString &before, const QString &after) {}; - virtual void replaceFind(const QString &before, const QString &after) {}; - virtual void replaceAll(const QString &before, const QString &after) {}; - virtual void findStringChanged() {}; - virtual bool supportsReplace() const { return true; }; + virtual void replace(const QString &before, const QString &after) {} + virtual void replaceFind(const QString &before, const QString &after) {} + virtual void replaceAll(const QString &before, const QString &after) {} + virtual void findStringChanged() {} + virtual bool supportsReplace() const { return true; } }; #endif // ABSTRACTDOCUMENTFIND_H diff --git a/src/plugins/find/constants.h b/src/plugins/find/constants.h index 1fb3d3d64..1c2bb86a6 100644 --- a/src/plugins/find/constants.h +++ b/src/plugins/find/constants.h @@ -8,6 +8,12 @@ #include #include +enum SearchScope { + AllProjects, + CurrentProject, + CurrentDocument +}; + struct SearchParams { QStringList filePathList; @@ -16,7 +22,6 @@ struct SearchParams bool wholeWordsFlag; QStringList patternsList; QStringList exPatternsList; - QMap projectInfoMap; }; struct ReplaceParams @@ -29,12 +34,11 @@ struct ReplaceParams struct FindItem { QString filePathName; - int lineNumber; + int lineNumber = -1; QString context; }; using FindItemList = QList; -using ProjectInfo = QMap; Q_DECLARE_METATYPE(SearchParams) Q_DECLARE_METATYPE(ReplaceParams) diff --git a/src/plugins/find/findtoolwindow.cpp b/src/plugins/find/findtoolwindow.cpp index 06ceb9ad5..8956d5514 100644 --- a/src/plugins/find/findtoolwindow.cpp +++ b/src/plugins/find/findtoolwindow.cpp @@ -4,9 +4,11 @@ #include "findtoolwindow.h" #include "searchresultwindow.h" -#include "transceiver/findreceiver.h" #include "util/searcreplacehworker.h" +#include "services/project/projectservice.h" +#include "services/editor/editorservice.h" + #include #include #include @@ -26,6 +28,7 @@ #include #include +using namespace dpfservice; DWIDGET_USE_NAMESPACE class FindToolWindowPrivate { @@ -34,10 +37,6 @@ class FindToolWindowPrivate DStackedWidget *stackedWidget { nullptr }; SearchResultWindow *searchResultWindow { nullptr }; - QSet allProjectsPathList { nullptr }; - QString currentProjectPath; - QString currentFilePath; - QMap projectInfoMap; QSharedPointer searchReplaceWorker { nullptr }; QThread thread; @@ -71,27 +70,6 @@ FindToolWindow::FindToolWindow(QWidget *parent) { setupUi(); initWorker(); - connect(FindEventTransmit::instance(), QOverload::of(&FindEventTransmit::sendProjectPath), - [=](const QString &projectPath, const QString &language) { - d->currentProjectPath = projectPath; - d->projectInfoMap.insert(projectPath, language); - d->allProjectsPathList.insert(projectPath); - }); - - connect(FindEventTransmit::instance(), QOverload::of(&FindEventTransmit::sendRemovedProject), - [=](const QString &projectPath) { - d->currentProjectPath = ""; - d->allProjectsPathList.remove(projectPath); - d->projectInfoMap.remove(projectPath); - }); - - connect(FindEventTransmit::instance(), QOverload::of(&FindEventTransmit::sendCurrentEditFile), - [=](const QString &filePath, bool actived) { - if (actived) { - d->currentFilePath = filePath; - } else - d->currentFilePath = ""; - }); } FindToolWindow::~FindToolWindow() @@ -118,8 +96,8 @@ void FindToolWindow::setupUi() scrollLayout->addWidget(scrollArea); mainPaneFrame->setLayout(scrollLayout); - QWidget *searchParamWidget = new QWidget(); - QWidget *searchResultWidget = new QWidget(); + QWidget *searchParamWidget = new QWidget(this); + QWidget *searchResultWidget = new QWidget(this); addSearchParamWidget(searchParamWidget); addSearchResultWidget(searchResultWidget); @@ -149,9 +127,9 @@ void FindToolWindow::addSearchParamWidget(QWidget *parentWidget) DLabel *scopeLabel = new DLabel(QLabel::tr("Scope:")); d->scopeComboBox = new DComboBox(parentWidget); - d->scopeComboBox->addItem(tr("All Projects")); - d->scopeComboBox->addItem(tr("Current Project")); - d->scopeComboBox->addItem(tr("Current File")); + d->scopeComboBox->addItem(tr("All Projects"), AllProjects); + d->scopeComboBox->addItem(tr("Current Project"), CurrentProject); + d->scopeComboBox->addItem(tr("Current File"), CurrentDocument); d->scopeComboBox->setFixedWidth(369); DLabel *searchLabel = new DLabel(QLabel::tr("Search for:")); @@ -284,29 +262,48 @@ void FindToolWindow::createMessageDialog(const QString &message) messageDialog->exec(); } -bool FindToolWindow::checkSelectedScopeValid() +bool FindToolWindow::checkSelectedScopeValid(QStringList *searchPathList) { - int index = d->scopeComboBox->currentIndex(); - switch (index) { - case 0: { - if (d->allProjectsPathList.isEmpty()) { + int scope = d->scopeComboBox->currentData().toInt(); + switch (scope) { + case AllProjects: { + auto projectSrv = dpfGetService(ProjectService); + const auto &infoList = projectSrv->getAllProjectInfo(); + if (infoList.isEmpty()) { createMessageDialog(tr("All projects path is empty, please import!")); return false; } + + if (!searchPathList) + break; + + for (const auto &info : infoList) { + searchPathList->append(info.sourceFiles().toList()); + } break; } - case 1: { - if (d->currentProjectPath.isEmpty()) { + case CurrentProject: { + auto projectSrv = dpfGetService(ProjectService); + const auto &info = projectSrv->getActiveProjectInfo(); + if (info.isEmpty()) { createMessageDialog(tr("Current project path is empty, please import!")); return false; } + + if (searchPathList) + *searchPathList = info.sourceFiles().toList(); break; } - case 2: { - if (d->currentFilePath.isEmpty()) { + case CurrentDocument: { + auto editSrv = dpfGetService(EditorService); + auto curFile = editSrv->currentFile(); + if (curFile.isEmpty()) { createMessageDialog(tr("Current project path is empty, please import!")); return false; } + + if (searchPathList) + searchPathList->append(curFile); break; } default: { @@ -320,7 +317,8 @@ bool FindToolWindow::checkSelectedScopeValid() bool FindToolWindow::getSearchParams(SearchParams *searchParams) { - if (!checkSelectedScopeValid()) + QStringList searchPathList; + if (!checkSelectedScopeValid(&searchPathList)) return false; QString text = d->searchLineEdit->text(); if (text.isEmpty()) { @@ -328,30 +326,12 @@ bool FindToolWindow::getSearchParams(SearchParams *searchParams) return false; } - QStringList searchPathList; - int index = d->scopeComboBox->currentIndex(); - switch (index) { - case 0: - searchPathList = d->allProjectsPathList.values(); - break; - case 1: - searchPathList = QStringList { d->currentProjectPath }; - break; - case 2: - searchPathList = QStringList { d->currentFilePath }; - break; - default: - break; - } - searchParams->filePathList = searchPathList; searchParams->searchText = text; searchParams->sensitiveFlag = d->senseCheckBtnFlag; searchParams->wholeWordsFlag = d->wholeWordsCheckBtnFlag; searchParams->patternsList = d->patternLineEdit->text().trimmed().split(",", QString::SkipEmptyParts); searchParams->exPatternsList = d->expatternLineEdit->text().trimmed().split(",", QString::SkipEmptyParts); - searchParams->exPatternsList << "*.so" << "*.o"; - searchParams->projectInfoMap = d->projectInfoMap; return true; } @@ -392,7 +372,7 @@ void FindToolWindow::handleSearchMatched() if (results.isEmpty()) return; - d->searchResultWindow->appendResults(results, d->projectInfoMap); + d->searchResultWindow->appendResults(results); } void FindToolWindow::handleSearchFinished() @@ -405,20 +385,9 @@ void FindToolWindow::handleReplace(const QString &text) ReplaceParams params; params.replaceText = text; params.searchText = d->searchLineEdit->text(); - int index = d->scopeComboBox->currentIndex(); - switch (index) { - case 0: - params.filePathList = d->allProjectsPathList.values(); - break; - case 1: - params.filePathList = QStringList { d->currentProjectPath }; - break; - case 2: - params.filePathList = QStringList { d->currentFilePath }; - break; - default: - break; - } + if (!checkSelectedScopeValid(¶ms.filePathList)) + return; + metaObject()->invokeMethod(d->searchReplaceWorker.data(), "addReplaceTask", Qt::QueuedConnection, diff --git a/src/plugins/find/findtoolwindow.h b/src/plugins/find/findtoolwindow.h index 90bc2db53..aade70dc6 100644 --- a/src/plugins/find/findtoolwindow.h +++ b/src/plugins/find/findtoolwindow.h @@ -28,7 +28,7 @@ class FindToolWindow : public QWidget void addSearchParamWidget(QWidget *parentWidget); void addSearchResultWidget(QWidget *parentWidget); void switchSearchParamWidget(); - bool checkSelectedScopeValid(); + bool checkSelectedScopeValid(QStringList *searchPathList = nullptr); bool getSearchParams(SearchParams *searchParams); void createMessageDialog(const QString &message); diff --git a/src/plugins/find/searchresultwindow.cpp b/src/plugins/find/searchresultwindow.cpp index 204e5ab9b..21d08f42f 100644 --- a/src/plugins/find/searchresultwindow.cpp +++ b/src/plugins/find/searchresultwindow.cpp @@ -89,7 +89,6 @@ class SearchResultTreeViewPrivate SearchResultTreeViewPrivate() {} ~SearchResultTreeViewPrivate(); - QMap projectInfoMap; QThread thread; QSharedPointer proxy; friend class SearchResultTreeView; @@ -108,7 +107,7 @@ SearchResultTreeView::SearchResultTreeView(QWidget *parent) QAbstractItemModel *itemModel = new QStandardItemModel(this); setModel(itemModel); - QObject::connect(this, &DTreeView::doubleClicked, [=](const QModelIndex &index) { + connect(this, &DTreeView::doubleClicked, this, [=](const QModelIndex &index) { if (!index.isValid()) return; if (!index.parent().isValid()) @@ -118,12 +117,7 @@ SearchResultTreeView::SearchResultTreeView(QWidget *parent) int lineNumber = index.data(Qt::UserRole + 1).toInt(); qInfo() << filePath << lineNumber; - foreach (QString key, d->projectInfoMap.keys()) { - if (filePath.contains(key, Qt::CaseInsensitive)) { - editor.gotoLine(filePath, lineNumber); - break; - } - } + editor.gotoLine(filePath, lineNumber); }); d->proxy.reset(new ItemProxy); @@ -138,9 +132,8 @@ SearchResultTreeView::~SearchResultTreeView() delete d; } -void SearchResultTreeView::appendData(const FindItemList &itemList, const ProjectInfo &projectInfo) +void SearchResultTreeView::appendData(const FindItemList &itemList) { - d->projectInfoMap = projectInfo; d->proxy->setRuningState(true); metaObject()->invokeMethod(d->proxy.data(), "addTask", @@ -197,7 +190,6 @@ SearchResultWindow::SearchResultWindow(QWidget *parent) setupUi(); qRegisterMetaType("FindItemList"); - qRegisterMetaType("ProjectInfo"); } SearchResultWindow::~SearchResultWindow() @@ -274,11 +266,11 @@ void SearchResultWindow::setRepalceWidgtVisible(bool visible) d->replaceWidget->setVisible(visible); } -void SearchResultWindow::appendResults(const FindItemList &itemList, const ProjectInfo &projectInfo) +void SearchResultWindow::appendResults(const FindItemList &itemList) { d->treeView->setVisible(true); d->iconLabel->setVisible(false); - d->treeView->appendData(itemList, projectInfo); + d->treeView->appendData(itemList); d->resultCount += itemList.count(); QString msg = tr("%1 matches found.").arg(d->resultCount); showMsg(true, msg); diff --git a/src/plugins/find/searchresultwindow.h b/src/plugins/find/searchresultwindow.h index e79e70c8d..688e8b336 100644 --- a/src/plugins/find/searchresultwindow.h +++ b/src/plugins/find/searchresultwindow.h @@ -42,7 +42,7 @@ class SearchResultTreeView : public DTreeView explicit SearchResultTreeView(QWidget *parent = nullptr); ~SearchResultTreeView(); - void appendData(const FindItemList &itemList, const ProjectInfo &projectInfo); + void appendData(const FindItemList &itemList); void clearData(); virtual QIcon icon(const QString &data); @@ -62,7 +62,7 @@ class SearchResultWindow : public DWidget ~SearchResultWindow(); void clear(); - void appendResults(const FindItemList &itemList, const ProjectInfo &projectInfo); + void appendResults(const FindItemList &itemList); void searchFinished(); void replaceFinished(bool success); void setRepalceWidgtVisible(bool hide); diff --git a/src/plugins/find/transceiver/findreceiver.cpp b/src/plugins/find/transceiver/findreceiver.cpp deleted file mode 100644 index 720dcf692..000000000 --- a/src/plugins/find/transceiver/findreceiver.cpp +++ /dev/null @@ -1,70 +0,0 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#include "findreceiver.h" -#include "common/common.h" -#include "services/project/projectservice.h" - -FindReceiver::FindReceiver(QObject *parent) - : dpf::EventHandler(parent) -{ - -} - -dpf::EventHandler::Type FindReceiver::type() -{ - return dpf::EventHandler::Type::Sync; -} - -QStringList FindReceiver::topics() -{ - return { project.topic, editor.topic }; -} - -void FindReceiver::eventProcess(const dpf::Event &event) -{ - if (event.data() == project.activedProject.name) { - dpfservice::ProjectInfo projectInfo = qvariant_cast( - event.property(project.activedProject.pKeys[0])); - QString workspace = projectInfo.workspaceFolder(); - QString language = projectInfo.language(); - emit FindEventTransmit::instance()->sendProjectPath(workspace, language); - } else if(event.data() == project.openProject.name) { - dpfservice::ProjectInfo projectInfo = qvariant_cast( - event.property(project.openProject.pKeys[0])); - QString workspace = projectInfo.workspaceFolder(); - QString language = projectInfo.language(); - emit FindEventTransmit::instance()->sendProjectPath(workspace, language); - } else if (event.data() == project.deletedProject.name){ - dpfservice::ProjectInfo projectInfo = qvariant_cast( - event.property(project.deletedProject.pKeys[0])); - QString workspace = projectInfo.workspaceFolder(); - emit FindEventTransmit::instance()->sendRemovedProject(workspace); - } else if (event.data() == editor.fileOpened.name) { - QString filePath = event.property(editor.fileOpened.pKeys[0]).toString(); - emit FindEventTransmit::instance()->sendCurrentEditFile(filePath, true); - } else if (event.data() == editor.fileClosed.name) { - QString filePath = event.property(editor.switchedFile.pKeys[0]).toString(); - emit FindEventTransmit::instance()->sendCurrentEditFile(filePath, false); - } else if (event.data() == editor.switchedFile.name) { - QString filePath = event.property(editor.switchedFile.pKeys[0]).toString(); - emit FindEventTransmit::instance()->sendCurrentEditFile(filePath, true); - } -} - -FindEventTransmit::FindEventTransmit(QObject *parent) - : QObject(parent) -{ -} - -FindEventTransmit::~FindEventTransmit() -{ - -} - -FindEventTransmit* FindEventTransmit::instance() -{ - static FindEventTransmit instance; - return &instance; -} diff --git a/src/plugins/find/transceiver/findreceiver.h b/src/plugins/find/transceiver/findreceiver.h deleted file mode 100644 index bda8f0f10..000000000 --- a/src/plugins/find/transceiver/findreceiver.h +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#ifndef FINDRECEIVER_H -#define FINDRECEIVER_H - -#include - -class FindReceiver : public dpf::EventHandler, dpf::AutoEventHandlerRegister -{ - Q_OBJECT -public: - explicit FindReceiver(QObject *parent = nullptr); - static Type type(); - static QStringList topics(); - virtual void eventProcess(const dpf::Event& event) override; -}; - -class FindEventTransmit : public QObject -{ - Q_OBJECT - Q_DISABLE_COPY(FindEventTransmit) -public: - static FindEventTransmit* instance(); - -signals: - void sendProjectPath(const QString &projectPath, const QString &language); - void sendRemovedProject(const QString &projectPath); - void sendCurrentEditFile(const QString &filePath, bool actived); - -private: - explicit FindEventTransmit(QObject *parent = nullptr); - virtual ~FindEventTransmit(); -}; - - -#endif // FINDRECEIVER_H diff --git a/src/plugins/find/util/searcreplacehworker.cpp b/src/plugins/find/util/searcreplacehworker.cpp index baad9351c..1681986ff 100644 --- a/src/plugins/find/util/searcreplacehworker.cpp +++ b/src/plugins/find/util/searcreplacehworker.cpp @@ -26,7 +26,6 @@ class SearchReplaceWorkerPrivate QAtomicInteger isRuning { false }; QSharedPointer searchProcess { nullptr }; QSharedPointer replaceProcess { nullptr }; - QString filePath; }; SearchReplaceWorkerPrivate::SearchReplaceWorkerPrivate(SearchReplaceWorker *qq) @@ -45,22 +44,22 @@ SearchReplaceWorkerPrivate::~SearchReplaceWorkerPrivate() QString SearchReplaceWorkerPrivate::buildCommand(const SearchParams ¶ms) { - filePath = params.filePathList.join(" "); + QString searchPath = params.filePathList.join(" "); QString sensitiveFlag = params.sensitiveFlag ? "" : " -i "; QString wholeWordsFlag = params.wholeWordsFlag ? " -w " : ""; QString patternList; if (!params.patternsList.isEmpty()) patternList = " --include=" + params.patternsList.join(" --include="); - QString exPatternList(" --exclude-dir=.*"); + QString exPatternList; if (!params.exPatternsList.isEmpty()) { QString format(" --exclude={%1}"); exPatternList += format.arg(params.exPatternsList.join(',')); } - QString cmd = QString("grep -rn " + sensitiveFlag + wholeWordsFlag + QString cmd = QString("grep -Hn " + sensitiveFlag + wholeWordsFlag + "\"" + params.searchText + "\" " - + patternList + exPatternList + " " + filePath); + + patternList + exPatternList + " " + searchPath); return cmd; }