Skip to content

Commit

Permalink
refactor: [search] Optimize advanced search logic
Browse files Browse the repository at this point in the history
Optimize advanced search logic
  • Loading branch information
Kakueeen authored and deepin-mozart committed Jun 4, 2024
1 parent 424243a commit ddcf66e
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 211 deletions.
10 changes: 5 additions & 5 deletions src/common/find/abstractdocumentfind.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 7 additions & 3 deletions src/plugins/find/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include <QMap>
#include <QObject>

enum SearchScope {
AllProjects,
CurrentProject,
CurrentDocument
};

struct SearchParams
{
QStringList filePathList;
Expand All @@ -16,7 +22,6 @@ struct SearchParams
bool wholeWordsFlag;
QStringList patternsList;
QStringList exPatternsList;
QMap<QString, QString> projectInfoMap;
};

struct ReplaceParams
Expand All @@ -29,12 +34,11 @@ struct ReplaceParams
struct FindItem
{
QString filePathName;
int lineNumber;
int lineNumber = -1;
QString context;
};

using FindItemList = QList<FindItem>;
using ProjectInfo = QMap<QString, QString>;

Q_DECLARE_METATYPE(SearchParams)
Q_DECLARE_METATYPE(ReplaceParams)
Expand Down
117 changes: 43 additions & 74 deletions src/plugins/find/findtoolwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <DLineEdit>
#include <DLabel>
#include <DComboBox>
Expand All @@ -26,6 +28,7 @@
#include <QFormLayout>
#include <QThread>

using namespace dpfservice;
DWIDGET_USE_NAMESPACE
class FindToolWindowPrivate
{
Expand All @@ -34,10 +37,6 @@ class FindToolWindowPrivate

DStackedWidget *stackedWidget { nullptr };
SearchResultWindow *searchResultWindow { nullptr };
QSet<QString> allProjectsPathList { nullptr };
QString currentProjectPath;
QString currentFilePath;
QMap<QString, QString> projectInfoMap;
QSharedPointer<SearchReplaceWorker> searchReplaceWorker { nullptr };
QThread thread;

Expand Down Expand Up @@ -71,27 +70,6 @@ FindToolWindow::FindToolWindow(QWidget *parent)
{
setupUi();
initWorker();
connect(FindEventTransmit::instance(), QOverload<const QString &, const QString &>::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<const QString &>::of(&FindEventTransmit::sendRemovedProject),
[=](const QString &projectPath) {
d->currentProjectPath = "";
d->allProjectsPathList.remove(projectPath);
d->projectInfoMap.remove(projectPath);
});

connect(FindEventTransmit::instance(), QOverload<const QString &, bool>::of(&FindEventTransmit::sendCurrentEditFile),
[=](const QString &filePath, bool actived) {
if (actived) {
d->currentFilePath = filePath;
} else
d->currentFilePath = "";
});
}

FindToolWindow::~FindToolWindow()
Expand All @@ -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);
Expand Down Expand Up @@ -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:"));
Expand Down Expand Up @@ -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: {
Expand All @@ -320,38 +317,21 @@ 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()) {
d->searchLineEdit->showAlertMessage(tr("Search for text is empty, please input!"));
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;
}
Expand Down Expand Up @@ -392,7 +372,7 @@ void FindToolWindow::handleSearchMatched()
if (results.isEmpty())
return;

d->searchResultWindow->appendResults(results, d->projectInfoMap);
d->searchResultWindow->appendResults(results);
}

void FindToolWindow::handleSearchFinished()
Expand All @@ -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(&params.filePathList))
return;

metaObject()->invokeMethod(d->searchReplaceWorker.data(),
"addReplaceTask",
Qt::QueuedConnection,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/find/findtoolwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 5 additions & 13 deletions src/plugins/find/searchresultwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class SearchResultTreeViewPrivate
SearchResultTreeViewPrivate() {}
~SearchResultTreeViewPrivate();

QMap<QString, QString> projectInfoMap;
QThread thread;
QSharedPointer<ItemProxy> proxy;
friend class SearchResultTreeView;
Expand All @@ -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())
Expand All @@ -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);
Expand All @@ -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",
Expand Down Expand Up @@ -197,7 +190,6 @@ SearchResultWindow::SearchResultWindow(QWidget *parent)
setupUi();

qRegisterMetaType<FindItemList>("FindItemList");
qRegisterMetaType<ProjectInfo>("ProjectInfo");
}

SearchResultWindow::~SearchResultWindow()
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/find/searchresultwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
Loading

0 comments on commit ddcf66e

Please sign in to comment.