- enhancement: Add "Open files in editor" in the search panel

- enhancement: Auto disable the "in project" option in the "search in files" dialog, if no project is opened.
  - enhancement: Auto disable the "search again" button in the search panel if the current search history item is search in the project, and no project is opened.
This commit is contained in:
Roy Qu 2023-01-12 16:53:00 +08:00
parent 93a37a2bc6
commit ea26548835
11 changed files with 761 additions and 638 deletions

View File

@ -28,6 +28,9 @@ Red Panda C++ Version 2.8
- enhancement: Rename symbols won't remove all breakpoints/bookmarks
- enhancement: Batch replace won't remove all breakpoints/bookmarks
- enhancement: Execute parameters can be used in debug.
- enhancement: Add "Open files in editor" in the search panel
- enhancement: Auto disable the "in project" option in the "search in files" dialog, if no project is opened.
- enhancement: Auto disable the "search again" button in the search panel if the current search history item is search in the project, and no project is opened.
Red Panda C++ Version 2.7

View File

@ -324,9 +324,17 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
} else {
Editor editor(nullptr);
QByteArray encoding;
editor.document()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding);
QStringList newContents;
editor.setSyntaxer(syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::CPP));
try {
editor.document()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding);
} catch(FileError e) {
QMessageBox::critical(pMainWindow,
tr("Rename Symbol Error"),
e.reason());
return;
}
QStringList newContents;
int posY = 0;
while (posY < editor.document()->count()) {
QString line = editor.document()->getLine(posY);
@ -365,8 +373,16 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
}
QByteArray realEncoding;
QFile file(filename);
try {
editor.document()->saveToFile(file,ENCODING_AUTO_DETECT,
pSettings->editor().defaultEncoding(),
realEncoding);
} catch(FileError e) {
QMessageBox::critical(pMainWindow,
tr("Rename Symbol Error"),
e.reason());
return;
}
}
}

View File

@ -934,6 +934,7 @@ void MainWindow::applySettings()
void MainWindow::applyUISettings()
{
const Settings::UI& settings = pSettings->ui();
ui->chkOpenFileInEditors->setChecked(settings.openEditorsWhenReplace());
restoreGeometry(settings.mainWindowGeometry());
restoreState(settings.mainWindowState());
ui->actionTool_Window_Bars->setChecked(settings.showToolWindowBars());
@ -5030,6 +5031,8 @@ void MainWindow::closeProject(bool refreshEditor)
updateProjectView();
mClosingProject=false;
}
if (refreshEditor)
on_cbSearchHistory_currentIndexChanged(ui->cbSearchHistory->currentIndex());
}
void MainWindow::updateProjectView()
@ -5203,6 +5206,7 @@ void MainWindow::closeEvent(QCloseEvent *event) {
if (mCPUDialog)
mCPUDialog->close();
Settings::UI& settings = pSettings->ui();
settings.setOpenEditorsWhenReplace(ui->chkOpenFileInEditors->isChecked());
settings.setMainWindowState(saveState());
settings.setMainWindowGeometry(saveGeometry());
settings.setBottomPanelIndex(ui->tabMessages->currentIndex());
@ -6197,6 +6201,11 @@ void MainWindow::on_cbSearchHistory_currentIndexChanged(int index)
mSearchResultModel.setCurrentIndex(index);
PSearchResults results = mSearchResultModel.results(index);
if (results) {
if (results->searchType==SearchType::Search
&& results->scope==SearchFileScope::wholeProject
&& pMainWindow->project()==nullptr)
ui->btnSearchAgain->setEnabled(false);
else
ui->btnSearchAgain->setEnabled(true);
} else {
ui->btnSearchAgain->setEnabled(false);
@ -6212,6 +6221,9 @@ void MainWindow::on_btnSearchAgain_clicked()
if (!results)
return;
if (results->searchType == SearchType::Search){
if (results->scope==SearchFileScope::wholeProject
&& pMainWindow->project()==nullptr)
return;
mSearchInFilesDialog->findInFiles(results->keyword,
results->scope,
results->options);
@ -7705,26 +7717,56 @@ void MainWindow::on_btnReplace_clicked()
}
QString newWord = ui->cbReplaceInHistory->currentText();
foreach (const PSearchResultTreeItem& file, results->results) {
QStringList contents;
Editor* editor = openFile(file->filename);
QVector<PSearchResultTreeItem> selections;
foreach(const PSearchResultTreeItem& item,file->results) {
if (item->selected) {
selections.push_back(item);
}
}
if (selections.isEmpty())
continue;
Editor* editor = nullptr;
if (ui->chkOpenFileInEditors->isChecked()) {
editor = openFile(file->filename);
if (!editor) {
QMessageBox::critical(this,
tr("Replace Error"),
tr("Can't open file '%1' for replace!").arg(file->filename));
return;
}
} else {
editor = mEditorList->getOpenedEditorByFilename(file->filename);
}
bool needSave=false;
std::shared_ptr<Editor> pEditor;
if (editor) {
editor->clearSelection();
editor->addGroupBreak();
editor->beginUndoBlock();
for (int i=file->results.count()-1;i>=0;i--) {
const PSearchResultTreeItem& item = file->results[i];
if (!item->selected)
continue;
} else {
needSave=true;
pEditor = std::make_shared<Editor>(nullptr);
editor = pEditor.get();
QByteArray encoding;
editor->setSyntaxer(syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::CPP));
try {
editor->document()->loadFromFile(file->filename,ENCODING_AUTO_DETECT,encoding);
} catch(FileError e) {
QMessageBox::critical(this,
tr("Replace Error"),
e.reason());
return;
}
}
while (!selections.isEmpty()) {
const PSearchResultTreeItem& item = selections.back();
selections.pop_back();
QString line = editor->document()->getLine(item->line-1);
if (line.mid(item->start-1,results->keyword.length())!=results->keyword) {
QMessageBox::critical(editor,
tr("Replace Error"),
tr("Contents has changed since last search!"));
if (!needSave)
editor->endUndoBlock();
return;
}
@ -7732,7 +7774,22 @@ void MainWindow::on_btnReplace_clicked()
line.insert(item->start-1, newWord);
editor->replaceLine(item->line,line);
}
if (!needSave) {
editor->endUndoBlock();
} else {
QByteArray realEncoding;
QFile toFile(file->filename);
try {
editor->document()->saveToFile(toFile,ENCODING_AUTO_DETECT,
pSettings->editor().defaultEncoding(),
realEncoding);
} catch(FileError e) {
QMessageBox::critical(this,
tr("Replace Error"),
e.reason());
return;
}
}
}
showSearchReplacePanel(false);
stretchMessagesPanel(false);

View File

@ -929,7 +929,7 @@
<enum>QTabWidget::South</enum>
</property>
<property name="currentIndex">
<number>6</number>
<number>3</number>
</property>
<property name="iconSize">
<size>
@ -1445,6 +1445,13 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="chkOpenFileInEditors">
<property name="text">
<string>Open file in editors</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnReplace">
<property name="text">

View File

@ -5272,6 +5272,16 @@ void Settings::UI::setProblemOrder(int newProblemOrder)
mProblemOrder = newProblemOrder;
}
bool Settings::UI::openEditorsWhenReplace() const
{
return mOpenEditorsWhenReplace;
}
void Settings::UI::setOpenEditorsWhenReplace(bool newOpenEditorsWhenReplace)
{
mOpenEditorsWhenReplace = newOpenEditorsWhenReplace;
}
int Settings::UI::bookmarkOrder() const
{
return mBookmarkOrder;
@ -5644,6 +5654,8 @@ void Settings::UI::setMainWindowState(const QByteArray &newMainWindowState)
void Settings::UI::doSave()
{
saveValue("open_editor_when_batch_replace",mOpenEditorsWhenReplace);
saveValue("main_window_state",mMainWindowState);
saveValue("main_window_geometry",mMainWindowGeometry);
saveValue("bottom_panel_index",mBottomPanelIndex);
@ -5707,6 +5719,8 @@ void Settings::UI::doSave()
void Settings::UI::doLoad()
{
mOpenEditorsWhenReplace=boolValue("open_editor_when_batch_replace",true);
mMainWindowState = value("main_window_state",QByteArray()).toByteArray();
mMainWindowGeometry = value("main_window_geometry",QByteArray()).toByteArray();
mBottomPanelIndex = intValue("bottom_panel_index",0);

View File

@ -1122,7 +1122,11 @@ public:
int problemOrder() const;
void setProblemOrder(int newProblemOrder);
bool openEditorsWhenReplace() const;
void setOpenEditorsWhenReplace(bool newOpenEditorsWhenReplace);
private:
bool mOpenEditorsWhenReplace;
QByteArray mMainWindowState;
QByteArray mMainWindowGeometry;
int mBottomPanelIndex;

View File

@ -4940,6 +4940,10 @@
<source>Newline</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Open file in editors</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NewClassDialog</name>

File diff suppressed because it is too large Load Diff

View File

@ -4753,6 +4753,10 @@
<source>Newline</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Open file in editors</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NewClassDialog</name>

View File

@ -163,6 +163,8 @@ void SearchInFileDialog::doSearch(bool replace)
}
pMainWindow->searchResultModel()->notifySearchResultsUpdated();
} else if (ui->rbProject->isChecked()) {
if (!pMainWindow->project())
return;
PSearchResults results = pMainWindow->searchResultModel()->addSearchResults(
keyword,
mSearchOptions,
@ -277,6 +279,9 @@ std::shared_ptr<SearchResultTreeItem> SearchInFileDialog::batchFindInEditor(QSyn
void SearchInFileDialog::showEvent(QShowEvent *event)
{
QDialog::showEvent(event);
ui->rbProject->setEnabled(pMainWindow->project()!=nullptr);
if (ui->rbProject->isChecked() && !ui->rbProject->isEnabled())
ui->rbCurrentFile->setChecked(true);
if (pSettings->environment().language()=="zh_CN") {
ui->txtRegExpHelp->setText(
QString("<html><head/><body><p><a href=\"%1\"><span style=\" text-decoration: underline; color:#0000ff;\">(?)</span></a></p></body></html>")

View File

@ -4373,7 +4373,6 @@ void QSynEdit::doUndoItem()
break;
}
case ChangeReason::ReplaceLine:
qDebug()<<item->changeStartPos().line<<item->changeText();
mRedoList->addRedo(
item->changeReason(),
item->changeStartPos(),