RedPanda-CPP/RedPandaIDE/widgets/searchdialog.cpp

434 lines
14 KiB
C++
Raw Normal View History

2021-08-02 21:58:39 +08:00
#include "searchdialog.h"
#include "ui_searchdialog.h"
2021-08-02 23:42:10 +08:00
#include <QTabBar>
2021-08-03 23:55:57 +08:00
#include "../editor.h"
#include "../mainwindow.h"
#include "../editorlist.h"
#include "../qsynedit/Search.h"
#include "../qsynedit/SearchRegex.h"
#include "../project.h"
2021-08-03 23:55:57 +08:00
#include <QMessageBox>
2021-08-05 12:31:53 +08:00
#include <QDebug>
2021-08-03 23:55:57 +08:00
2021-08-02 21:58:39 +08:00
SearchDialog::SearchDialog(QWidget *parent) :
QDialog(parent),
2021-08-03 23:55:57 +08:00
ui(new Ui::SearchDialog),
mSearchEngine()
2021-08-02 21:58:39 +08:00
{
ui->setupUi(this);
2021-08-02 23:42:10 +08:00
mTabBar = new QTabBar();
mTabBar->addTab(tr("Find"));
mTabBar->addTab(tr("Find in files"));
mTabBar->addTab(tr("Replace"));
mTabBar->addTab(tr("Replace in files"));
2021-08-03 23:55:57 +08:00
mTabBar->setExpanding(false);
2021-08-02 23:42:10 +08:00
ui->dialogLayout->insertWidget(0,mTabBar);
connect(mTabBar,&QTabBar::currentChanged,this, &SearchDialog::onTabChanged);
2021-08-03 23:55:57 +08:00
mSearchOptions&=0;
mBasicSearchEngine= PSynSearchBase(new SynSearch());
mRegexSearchEngine= PSynSearchBase(new SynSearchRegex());
2021-08-02 21:58:39 +08:00
}
SearchDialog::~SearchDialog()
{
delete ui;
}
2021-08-02 23:42:10 +08:00
void SearchDialog::find(const QString &text)
{
2021-08-04 09:13:41 +08:00
if (mTabBar->currentIndex()==0) {
this->onTabChanged();
} else {
mTabBar->setCurrentIndex(0);
}
2021-08-02 23:42:10 +08:00
ui->cbFind->setCurrentText(text);
show();
}
void SearchDialog::findNext()
{
if (mTabBar->currentIndex()==0) { // it's a find action
// Disable entire scope searching
ui->rbEntireScope->setChecked(false);
// Always search forwards
ui->rbForward->setChecked(true);
ui->btnExecute->click();
}
}
2021-08-02 23:42:10 +08:00
void SearchDialog::findInFiles(const QString &text)
{
mTabBar->setCurrentIndex(1);
ui->cbFind->setCurrentText(text);
show();
}
2021-08-05 19:58:32 +08:00
void SearchDialog::findInFiles(const QString &keyword, SearchFileScope scope, SynSearchOptions options)
{
mTabBar->setCurrentIndex(1);
2021-08-05 19:58:32 +08:00
ui->cbFind->setCurrentText(keyword);
switch(scope) {
case SearchFileScope::currentFile:
ui->rbCurrentFile->setChecked(true);
break;
case SearchFileScope::openedFiles:
ui->rbOpenFiles->setChecked(true);
break;
case SearchFileScope::wholeProject:
ui->rbProject->setChecked(true);
break;
}
// Apply options
2021-10-20 18:05:43 +08:00
ui->chkRegExp->setChecked(options.testFlag(ssoRegExp));
ui->chkCaseSensetive->setChecked(options.testFlag(ssoMatchCase));
ui->chkWholeWord->setChecked(options.testFlag(ssoWholeWord));
2021-08-05 19:58:32 +08:00
show();
}
2021-08-02 23:42:10 +08:00
void SearchDialog::replace(const QString &sFind, const QString &sReplace)
{
mTabBar->setCurrentIndex(2);
ui->cbFind->setCurrentText(sFind);
ui->cbReplace->setCurrentText(sReplace);
show();
}
void SearchDialog::onTabChanged()
{
bool isfind = (mTabBar->currentIndex() == 0);
bool isfindfiles = (mTabBar->currentIndex() == 1 || mTabBar->currentIndex() == 3 );
2021-08-02 23:42:10 +08:00
bool isreplace = (mTabBar->currentIndex() == 2);
2021-08-04 09:13:41 +08:00
ui->lblReplace->setVisible(isreplace);
ui->cbReplace->setVisible(isreplace);
2021-08-02 23:42:10 +08:00
ui->grpOrigin->setVisible(isfind || isreplace);
ui->grpOrigin->setEnabled(isfind || isreplace);
2021-08-02 23:42:10 +08:00
ui->grpScope->setVisible(isfind || isreplace);
ui->grpScope->setEnabled(isreplace);
2021-08-04 09:13:41 +08:00
ui->grpWhere->setVisible(isfindfiles);
ui->grpWhere->setEnabled(isfindfiles);
2021-08-02 23:42:10 +08:00
ui->grpDirection->setVisible(isfind || isreplace);
ui->grpDirection->setEnabled(isfind || isreplace);
2021-08-02 23:42:10 +08:00
// grpOption is always visible
// Disable project search option when none is open
// rbProjectFiles.Enabled := Assigned(MainForm.Project);
ui->rbProject->setEnabled(pMainWindow->project()!=nullptr);
ui->rbOpenFiles->setEnabled(pMainWindow->editorList()->pageCount()>0);
2021-08-02 23:42:10 +08:00
// if not Assigned(MainForm.Project) then
// rbOpenFiles.Checked := true;
// Disable prompt when doing finds
2021-08-04 09:13:41 +08:00
ui->chkPrompt->setEnabled(isreplace);
2021-08-02 23:42:10 +08:00
2021-08-04 09:13:41 +08:00
if (isfind || isfindfiles) {
2021-08-02 23:42:10 +08:00
ui->btnExecute->setText(tr("Find"));
} else {
ui->btnExecute->setText(tr("Replace"));
}
setWindowTitle(mTabBar->tabText(mTabBar->currentIndex()));
}
void SearchDialog::on_cbFind_currentTextChanged(const QString &)
{
ui->btnExecute->setEnabled(!ui->cbFind->currentText().isEmpty());
}
void SearchDialog::on_btnCancel_clicked()
{
this->close();
}
2021-08-03 23:55:57 +08:00
void SearchDialog::on_btnExecute_clicked()
2021-08-02 23:42:10 +08:00
{
2021-08-05 12:31:53 +08:00
int findCount = 0;
2021-08-03 23:55:57 +08:00
SearchAction actionType;
switch (mTabBar->currentIndex()) {
case 0:
actionType = SearchAction::Find;
break;
case 1:
actionType = SearchAction::FindFiles;
break;
case 2:
actionType = SearchAction::Replace;
break;
case 3:
actionType = SearchAction::ReplaceFiles;
break;
default:
return;
}
mSearchOptions&=0;
// Apply options
if (ui->chkRegExp->isChecked()) {
mSearchOptions.setFlag(ssoRegExp);
}
if (ui->chkCaseSensetive->isChecked()) {
mSearchOptions.setFlag(ssoMatchCase);
}
if (ui->chkWholeWord->isChecked()) {
mSearchOptions.setFlag(ssoWholeWord);
}
// Apply scope, when enabled
if (ui->grpScope->isEnabled()) {
2021-08-03 23:55:57 +08:00
if (ui->rbSelection->isChecked()) {
mSearchOptions.setFlag(ssoSelectedOnly);
}
}
// Apply direction, when enabled
if (ui->grpDirection->isEnabled()) {
2021-08-03 23:55:57 +08:00
if (ui->rbBackward->isChecked()) {
mSearchOptions.setFlag(ssoBackwards);
}
}
// Apply origin, when enabled
if (ui->grpOrigin->isEnabled()) {
2021-08-03 23:55:57 +08:00
if (ui->rbEntireScope->isChecked()) {
mSearchOptions.setFlag(ssoEntireScope);
}
}
// Use entire scope for file finding/replacing
if (actionType == SearchAction::FindFiles || actionType == SearchAction::ReplaceFiles) {
mSearchOptions.setFlag(ssoEntireScope);
}
this->close();
// Find the first one, then quit
if (actionType == SearchAction::Find) {
Editor *e = pMainWindow->editorList()->getEditor();
if (e!=nullptr) {
2021-08-05 12:31:53 +08:00
findCount+=execute(e,ui->cbFind->currentText(),"");
2021-08-03 23:55:57 +08:00
}
2021-08-05 12:31:53 +08:00
} else if (actionType == SearchAction::Replace) {
2021-08-04 09:13:41 +08:00
Editor *e = pMainWindow->editorList()->getEditor();
if (e!=nullptr) {
bool doPrompt = ui->chkPrompt->isChecked();
2021-08-05 12:31:53 +08:00
findCount+=execute(e,ui->cbFind->currentText(),ui->cbReplace->currentText(),
2021-08-04 09:13:41 +08:00
[&doPrompt](const QString& sSearch,
2021-10-20 18:05:43 +08:00
const QString& /*sReplace*/, int /*Line*/, int /*ch*/, int /*wordLen*/){
2021-08-04 09:13:41 +08:00
if (doPrompt) {
switch(QMessageBox::question(pMainWindow,
tr("Replace"),
tr("Replace this occurrence of ''%1''?").arg(sSearch),
QMessageBox::Yes|QMessageBox::YesAll|QMessageBox::No|QMessageBox::Cancel,
QMessageBox::Yes)) {
case QMessageBox::Yes:
return SynSearchAction::Replace;
case QMessageBox::YesAll:
return SynSearchAction::ReplaceAll;
case QMessageBox::No:
return SynSearchAction::Skip;
case QMessageBox::Cancel:
return SynSearchAction::Exit;
default:
return SynSearchAction::Exit;
}
} else {
return SynSearchAction::ReplaceAll;
}
});
}
} else if (actionType == SearchAction::FindFiles || actionType == SearchAction::ReplaceFiles) {
2021-08-05 12:31:53 +08:00
int fileSearched = 0;
int fileHitted = 0;
QString keyword = ui->cbFind->currentText();
if (ui->rbOpenFiles->isChecked()) {
PSearchResults results = pMainWindow->searchResultModel()->addSearchResults(
keyword,
mSearchOptions,
SearchFileScope::openedFiles
);
// loop through editors, add results to message control
for (int i=0;i<pMainWindow->editorList()->pageCount();i++) {
Editor * e=pMainWindow->editorList()->operator[](i);
if (e!=nullptr) {
fileSearched++;
PSearchResultTreeItem parentItem = batchFindInEditor(e,
keyword);
int t = parentItem->results.size();
findCount+=t;
if (t>0) {
fileHitted++;
results->results.append(parentItem);
}
}
}
pMainWindow->searchResultModel()->notifySearchResultsUpdated();
} else if (ui->rbCurrentFile->isChecked()) {
PSearchResults results = pMainWindow->searchResultModel()->addSearchResults(
keyword,
mSearchOptions,
SearchFileScope::openedFiles
);
Editor * e= pMainWindow->editorList()->getEditor();
if (e!=nullptr) {
fileSearched++;
PSearchResultTreeItem parentItem = batchFindInEditor(e,
keyword);
int t = parentItem->results.size();
findCount+=t;
if (t>0) {
fileHitted++;
results->results.append(parentItem);
}
}
pMainWindow->searchResultModel()->notifySearchResultsUpdated();
} else if (ui->rbProject->isChecked()) {
PSearchResults results = pMainWindow->searchResultModel()->addSearchResults(
keyword,
mSearchOptions,
SearchFileScope::wholeProject
);
for (int i=0;i<pMainWindow->project()->units().count();i++) {
Editor * e = pMainWindow->project()->units()[i]->editor();
QString curFilename = pMainWindow->project()->units()[i]->fileName();
if (e) {
fileSearched++;
PSearchResultTreeItem parentItem = batchFindInEditor(e,
keyword);
int t = parentItem->results.size();
findCount+=t;
if (t>0) {
fileHitted++;
results->results.append(parentItem);
}
} else if (fileExists(curFilename)) {
Editor editor(nullptr,curFilename,ENCODING_AUTO_DETECT,false,false,nullptr);
fileSearched++;
PSearchResultTreeItem parentItem = batchFindInEditor(&editor,
keyword);
int t = parentItem->results.size();
findCount+=t;
if (t>0) {
fileHitted++;
results->results.append(parentItem);
}
}
}
2021-08-05 12:31:53 +08:00
// end else if rbProjectFiles.Checked then begin
// for I := 0 to MainForm.Project.Units.Count - 1 do begin
// e := MainForm.Project.Units[i].Editor;
// fCurFile := MainForm.Project.Units[i].FileName;
// // file is already open, use memory
// if Assigned(e) then begin begin
// inc(fileSearched);
2021-08-19 17:08:01 +08:00
// t:=Execute(e->text, actiontype);
2021-08-05 12:31:53 +08:00
// Inc(findcount, t);
// if t>0 then
// inc(filehitted);
// end;
}
2021-08-05 19:58:32 +08:00
if (findCount>0)
pMainWindow->showSearchPanel(actionType == SearchAction::ReplaceFiles);
2021-08-03 23:55:57 +08:00
}
2021-08-02 23:42:10 +08:00
}
2021-08-04 09:13:41 +08:00
int SearchDialog::execute(Editor *editor, const QString &sSearch, const QString &sReplace, SynSearchMathedProc matchCallback)
2021-08-02 23:42:10 +08:00
{
2021-08-03 23:55:57 +08:00
if (editor==nullptr)
return 0;
// Modify the caret when using 'from cursor' and when the selection is ignored
if (!mSearchOptions.testFlag(ssoEntireScope) && !mSearchOptions.testFlag(ssoSelectedOnly)
&& editor->selAvail()) {
// start at end of selection
if (mSearchOptions.testFlag(ssoBackwards)) {
editor->setCaretXY(editor->blockBegin());
} else {
editor->setCaretXY(editor->blockEnd());
}
}
if (mSearchOptions.testFlag(ssoRegExp)) {
mSearchEngine = mRegexSearchEngine;
} else {
mSearchEngine = mBasicSearchEngine;
}
2021-08-04 09:13:41 +08:00
return editor->searchReplace(sSearch, sReplace, mSearchOptions,
mSearchEngine, matchCallback);
2021-08-05 12:31:53 +08:00
}
2021-08-03 23:55:57 +08:00
2021-08-05 12:31:53 +08:00
std::shared_ptr<SearchResultTreeItem> SearchDialog::batchFindInEditor(Editor *e, const QString &keyword)
{
//backup
BufferCoord caretBackup = e->caretXY();
BufferCoord blockBeginBackup = e->blockBegin();
BufferCoord blockEndBackup = e->blockEnd();
int toplineBackup = e->topLine();
int leftCharBackup = e->leftChar();
PSearchResultTreeItem parentItem = std::make_shared<SearchResultTreeItem>();
parentItem->filename = e->filename();
parentItem->parent = nullptr;
execute(e,keyword,"",
[e,&parentItem](const QString&,
const QString&, int Line, int ch, int wordLen){
PSearchResultTreeItem item = std::make_shared<SearchResultTreeItem>();
item->filename = e->filename();
item->line = Line;
item->start = ch;
item->len = wordLen;
item->parent = parentItem.get();
item->text = e->lines()->getString(Line-1);
2021-08-05 19:58:32 +08:00
item->text.replace('\t',' ');
2021-08-05 12:31:53 +08:00
parentItem->results.append(item);
return SynSearchAction::Skip;
});
// restore
e->setCaretXY(caretBackup);
e->setTopLine(toplineBackup);
e->setLeftChar(leftCharBackup);
e->setCaretAndSelection(
caretBackup,
blockBeginBackup,
blockEndBackup
);
return parentItem;
2021-08-03 23:55:57 +08:00
}
QTabBar *SearchDialog::tabBar() const
{
return mTabBar;
}
2021-08-03 23:55:57 +08:00
PSynSearchBase SearchDialog::searchEngine() const
{
return mSearchEngine;
2021-08-02 23:42:10 +08:00
}
void SearchDialog::findPrevious()
{
if (mTabBar->currentIndex()==0) { // it's a find action
// Disable entire scope searching
ui->rbEntireScope->setChecked(false);
// Always search backward
ui->rbBackward->setChecked(true);
ui->btnExecute->click();
}
}