From d28805ea5563120dfc811129d25d2a7e1c6e553d Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Fri, 12 Nov 2021 12:40:47 +0800 Subject: [PATCH 1/5] - fix: editor auto save settings not saved and applied - fix: only auto save files that has new modifications - fix: correctly auto save files with it's own name --- NEWS.md | 3 + RedPandaIDE/editor.cpp | 14 ++++ RedPandaIDE/editor.h | 4 ++ RedPandaIDE/mainwindow.cpp | 66 +++++++++++-------- .../settingsdialog/editorautosavewidget.cpp | 6 +- .../settingsdialog/editorautosavewidget.ui | 2 +- RedPandaIDE/widgets/darkfusionstyle.cpp | 2 +- 7 files changed, 67 insertions(+), 30 deletions(-) diff --git a/NEWS.md b/NEWS.md index 2b9ff2fe..a5a351aa 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,9 @@ Version 0.8.6 For Dev-C++ 7 Beta - enhancement: greatly reduces memory usage for symbol parsing ( memory needed for bits/stdc++.h reduced from 150m+ to 80m+) - fix: currect compiler set not correctly updated when switch between normal file and project file + - fix: editor auto save settings not saved and applied + - fix: only auto save files that has new modifications + - fix: correctly auto save files with it's own name Version 0.8.5 For Dev-C++ 7 Beta - enhancement: use lighter color to draw menu seperators diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 33866062..8a9e3097 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -151,10 +151,12 @@ Editor::Editor(QWidget *parent, const QString& filename, connect(this, &QWidget::customContextMenuRequested, pMainWindow, &MainWindow::onEditorContextMenu); + mCanAutoSave = false; if (isNew && parentPageControl!=nullptr) { QString fileTemplate = pMainWindow->codeSnippetManager()->newFileTemplate(); if (!fileTemplate.isEmpty()) { insertCodeSnippet(fileTemplate); + mCanAutoSave = true; } } if (!isNew && parentPageControl!=nullptr) { @@ -1390,6 +1392,8 @@ void Editor::onStatusChanged(SynStatusChanges changes) } if (changes.testFlag(scModified)) { mCurrentLineModified = true; + if (mParentPageControl!=nullptr) + mCanAutoSave = true; } if (changes.testFlag(SynStatusChange::scCaretX) @@ -3120,6 +3124,16 @@ void Editor::onExportedFormatToken(PSynHighlighter syntaxHighlighter, int Line, } } +bool Editor::canAutoSave() const +{ + return mCanAutoSave; +} + +void Editor::setCanAutoSave(bool newCanAutoSave) +{ + mCanAutoSave = newCanAutoSave; +} + const QDateTime &Editor::hideTime() const { return mHideTime; diff --git a/RedPandaIDE/editor.h b/RedPandaIDE/editor.h index 52b07f33..70e44cfb 100644 --- a/RedPandaIDE/editor.h +++ b/RedPandaIDE/editor.h @@ -269,6 +269,7 @@ private: int mTabStopBegin; int mTabStopEnd; int mTabStopY; + bool mCanAutoSave; QString mLineBeforeTabStop; QString mLineAfterTabStop; QList mUserCodeInTabStops; @@ -312,6 +313,9 @@ public: const QDateTime &hideTime() const; void setHideTime(const QDateTime &newHideTime); + bool canAutoSave() const; + void setCanAutoSave(bool newCanAutoSave); + protected: void mouseReleaseEvent(QMouseEvent *event) override; diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index f9e93de8..06939749 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -799,8 +799,10 @@ void MainWindow::updateClassBrowserForEditor(Editor *editor) void MainWindow::resetAutoSaveTimer() { if (pSettings->editor().enableAutoSave()) { + mAutoSaveTimer.stop(); //minute to milliseconds mAutoSaveTimer.start(pSettings->editor().autoSaveInterval()*60*1000); + onAutoSaveTimeout(); } else { mAutoSaveTimer.stop(); } @@ -1547,35 +1549,47 @@ void MainWindow::prepareDebugger() void MainWindow::doAutoSave(Editor *e) { - if (!e) - return; - if (!e->modified()) + + if (!e || !e->canAutoSave()) return; QString filename = e->filename(); - QFileInfo fileInfo(filename); - QDir parent = fileInfo.absoluteDir(); - QString baseName = fileInfo.completeBaseName(); - QString suffix = fileInfo.suffix(); - switch(pSettings->editor().autoSaveStrategy()) { - case assOverwrite: - break; - case assAppendUnixTimestamp: - filename = parent.filePath( - QString("%1.%2.%3") - .arg(baseName) - .arg(QDateTime::currentSecsSinceEpoch()) - .arg(suffix)); - break; - case assAppendFormatedTimeStamp: { - QDateTime time = QDateTime::currentDateTime(); - filename = parent.filePath( - QString("%1.%2.%3") - .arg(baseName) - .arg(time.toString("yyyy.MM.dd.hh.mm.ss")) - .arg(suffix)); + try { + QFileInfo fileInfo(filename); + QDir parent = fileInfo.absoluteDir(); + QString baseName = fileInfo.completeBaseName(); + QString suffix = fileInfo.suffix(); + switch(pSettings->editor().autoSaveStrategy()) { + case assOverwrite: + e->save(); + return; + case assAppendUnixTimestamp: + filename = parent.filePath( + QString("%1.%2.%3") + .arg(baseName) + .arg(QDateTime::currentSecsSinceEpoch()) + .arg(suffix)); + break; + case assAppendFormatedTimeStamp: { + QDateTime time = QDateTime::currentDateTime(); + filename = parent.filePath( + QString("%1.%2.%3") + .arg(baseName) + .arg(time.toString("yyyy.MM.dd.hh.mm.ss")) + .arg(suffix)); + } + } + if (e->isNew()) { + e->saveAs(); + } else { + e->saveFile(filename); + e->setCanAutoSave(false); + } + } catch (FileError& error) { + QMessageBox::critical(e, + tr("Auto Save Error"), + tr("Auto save \"%1\" to \"%2\" failed:%3") + .arg(e->filename(), filename, error.reason())); } - } - e->saveFile(filename); } //static void limitActionShortCutScope(QAction* action,QWidget* scopeWidget) { diff --git a/RedPandaIDE/settingsdialog/editorautosavewidget.cpp b/RedPandaIDE/settingsdialog/editorautosavewidget.cpp index 85668668..15d961dd 100644 --- a/RedPandaIDE/settingsdialog/editorautosavewidget.cpp +++ b/RedPandaIDE/settingsdialog/editorautosavewidget.cpp @@ -34,7 +34,7 @@ void EditorAutoSaveWidget::doLoad() { //pSettings->editor().load(); //font - ui->chkEnableAutoSave->setChecked(pSettings->editor().enableAutoSave()); + ui->grpEnableAutoSave->setChecked(pSettings->editor().enableAutoSave()); ui->spinInterval->setValue(pSettings->editor().autoSaveInterval()); switch(pSettings->editor().autoSaveTarget()) { case astCurrentFile: @@ -60,7 +60,7 @@ void EditorAutoSaveWidget::doLoad() void EditorAutoSaveWidget::doSave() { - pSettings->editor().setEnableAutoSave(ui->chkEnableAutoSave->isChecked()); + pSettings->editor().setEnableAutoSave(ui->grpEnableAutoSave->isChecked()); pSettings->editor().setAutoSaveInterval(ui->spinInterval->value()); if (ui->rbCurrentFile->isChecked()) pSettings->editor().setAutoSaveTarget(astCurrentFile); @@ -74,6 +74,8 @@ void EditorAutoSaveWidget::doSave() pSettings->editor().setAutoSaveStrategy(assAppendUnixTimestamp); else pSettings->editor().setAutoSaveStrategy(assAppendFormatedTimeStamp); + pSettings->editor().save(); + pMainWindow->resetAutoSaveTimer(); } void EditorAutoSaveWidget::on_rbOverwrite_toggled(bool) diff --git a/RedPandaIDE/settingsdialog/editorautosavewidget.ui b/RedPandaIDE/settingsdialog/editorautosavewidget.ui index 5f36b260..7e6634dc 100644 --- a/RedPandaIDE/settingsdialog/editorautosavewidget.ui +++ b/RedPandaIDE/settingsdialog/editorautosavewidget.ui @@ -15,7 +15,7 @@ - + Enable auto save diff --git a/RedPandaIDE/widgets/darkfusionstyle.cpp b/RedPandaIDE/widgets/darkfusionstyle.cpp index 8a581ac9..ac5560fc 100644 --- a/RedPandaIDE/widgets/darkfusionstyle.cpp +++ b/RedPandaIDE/widgets/darkfusionstyle.cpp @@ -805,12 +805,12 @@ void DarkFusionStyle::drawControl(ControlElement element, const QStyleOption *op switch (element) { case CE_MenuItem: - painter->save(); // Draws one item in a popup menu. if (const QStyleOptionMenuItem *menuItem = qstyleoption_cast(option)) { QColor highlightOutline = highlightedOutline; QColor highlight = option->palette.highlight().color(); if (menuItem->menuItemType == QStyleOptionMenuItem::Separator) { + painter->save(); int w = 0; const int margin = int(QStyleHelper::dpiScaled(5, option)); if (!menuItem->text.isEmpty()) { From 7e6830905716183a8f79cc647ea5acd31dea4ac0 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Fri, 12 Nov 2021 15:29:42 +0800 Subject: [PATCH 2/5] change version number --- RedPandaIDE/systemconsts.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RedPandaIDE/systemconsts.h b/RedPandaIDE/systemconsts.h index 1502fc1c..de3b4b01 100644 --- a/RedPandaIDE/systemconsts.h +++ b/RedPandaIDE/systemconsts.h @@ -3,7 +3,7 @@ #include -#define DEVCPP_VERSION "beta.0.8.5" +#define DEVCPP_VERSION "beta.0.8.6" #define APP_SETTSINGS_FILENAME "redpandacpp.ini" #ifdef Q_OS_WIN From 664c8e752a9a77121fe2f1df9820272795a536b9 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Sat, 13 Nov 2021 09:05:02 +0800 Subject: [PATCH 3/5] work save --- RedPandaIDE/editor.cpp | 5 +++ RedPandaIDE/qsynedit/SynEdit.cpp | 60 +++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/RedPandaIDE/editor.cpp b/RedPandaIDE/editor.cpp index 8a9e3097..904fa7ee 100644 --- a/RedPandaIDE/editor.cpp +++ b/RedPandaIDE/editor.cpp @@ -3528,7 +3528,12 @@ void Editor::reformat() content); selectAll(); + SynEditorOptions oldOptions = getOptions(); + SynEditorOptions newOptions = oldOptions; + newOptions.setFlag(SynEditorOption::eoAutoIndent,false); + setOptions(newOptions); setSelText(QString::fromUtf8(newContent)); + setOptions(oldOptions); reparse(); checkSyntaxInBack(); reparseTodo(); diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 3497183a..45789dd6 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -1,4 +1,5 @@ #include "SynEdit.h" +#include "highlighter/cpp.h" #include #include #include @@ -1473,7 +1474,14 @@ int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent) matchingIndents = rangeAfterFirstToken.matchingIndents; dontAddIndent = true; l = startLine; - } else if (mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state) + } else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter + && trimmedLineText.startsWith('#') + && attr == ((SynEditCppHighlighter *)mHighlighter.get())->preprocessorAttribute()) { + dontAddIndent = true; + indentSpaces=0; + l=0; + } else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter + && mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state) && (trimmedLineText.startsWith("*")) ) { // last line is a not finished comment, and this line start with "*" @@ -2624,8 +2632,40 @@ void SynEdit::doAddChar(QChar AChar) } mUndoList->BeginBlock(); - if (mOptions.testFlag(eoAutoIndent) && mHighlighter - && (oldCaretY<=mLines->count())) { + if (mOptions.testFlag(eoAutoIndent) + && mHighlighter + && mHighlighter->getClass()==SynHighlighterClass::CppHighlighter + && (oldCaretY<=mLines->count()) ) { + + if (AChar == '#') { + QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); + QString right = mLines->getString(oldCaretY-1).mid(oldCaretX); + // and the first nonblank char is this new { + if (temp.trimmed().isEmpty()) { + int indentSpaces = calcIndentSpaces(oldCaretY,"#", true); + QString line = mLines->getString(oldCaretY-1); + if (indentSpaces==0 && leftSpaces(temp)!=0) { + QString temp = right; + int i = temp.length(); + mLines->putString(oldCaretY-1,""); + internalSetCaretXY(BufferCoord{i+1,oldCaretY}); + mUndoList->AddChange( + SynChangeReason::crDelete, + BufferCoord{1, oldCaretY}, + BufferCoord{line.length()+1, oldCaretY}, + line, + SynSelectionMode::smNormal + ); + mUndoList->AddChange( + SynChangeReason::crInsert, + BufferCoord{1, oldCaretY}, + BufferCoord{temp.length()+1, oldCaretY}, + "", + SynSelectionMode::smNormal + ); + } + } + } else //unindent if ':' at end of the line if (AChar == ':') { QString line = mLines->getString(oldCaretY-1); @@ -2652,15 +2692,16 @@ void SynEdit::doAddChar(QChar AChar) ); } } - } + } else //unindent if '{' is after an statement like 'if' 'for' if (AChar == '{') { QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); + QString right = mLines->getString(oldCaretY-1).mid(oldCaretX); // and the first nonblank char is this new { if (temp.trimmed().isEmpty()) { int indentSpaces = calcIndentSpaces(oldCaretY,"{", true); - QString line = mLines->getString(oldCaretY-1); - if (indentSpaces != leftSpaces(line)) { + if (indentSpaces != leftSpaces(temp)) { + QString line = mLines->getString(oldCaretY-1) + right; QString temp = GetLeftSpacing(indentSpaces,true); int i = temp.length(); mLines->putString(oldCaretY-1,temp); @@ -2681,16 +2722,17 @@ void SynEdit::doAddChar(QChar AChar) ); } } - } + } else // Remove TabWidth of indent of the current line when typing a } if (AChar == '}') { QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); + QString right = mLines->getString(oldCaretY-1).mid(oldCaretX); // and the first nonblank char is this new } if (temp.trimmed().isEmpty()) { int indentSpaces = calcIndentSpaces(oldCaretY,"}", true); QString line = mLines->getString(oldCaretY-1); - if (indentSpaces != leftSpaces(line)) { - QString temp = GetLeftSpacing(indentSpaces,true); + if (indentSpaces != leftSpaces(temp)) { + QString temp = GetLeftSpacing(indentSpaces,true) + right; int i = temp.length(); mLines->putString(oldCaretY-1,temp); internalSetCaretXY(BufferCoord{i+1,oldCaretY}); From 4c359dd7cdfa034971bcce9adb25f1fa1160cb85 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Sat, 13 Nov 2021 09:11:28 +0800 Subject: [PATCH 4/5] work save --- RedPandaIDE/qsynedit/SynEdit.cpp | 68 ++++++++++++++++---------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 45789dd6..92c25e8a 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -2660,7 +2660,7 @@ void SynEdit::doAddChar(QChar AChar) SynChangeReason::crInsert, BufferCoord{1, oldCaretY}, BufferCoord{temp.length()+1, oldCaretY}, - "", + temp, SynSelectionMode::smNormal ); } @@ -2687,7 +2687,7 @@ void SynEdit::doAddChar(QChar AChar) SynChangeReason::crInsert, BufferCoord{1, oldCaretY}, BufferCoord{temp.length()+1, oldCaretY}, - "", + temp, SynSelectionMode::smNormal ); } @@ -2701,37 +2701,7 @@ void SynEdit::doAddChar(QChar AChar) if (temp.trimmed().isEmpty()) { int indentSpaces = calcIndentSpaces(oldCaretY,"{", true); if (indentSpaces != leftSpaces(temp)) { - QString line = mLines->getString(oldCaretY-1) + right; - QString temp = GetLeftSpacing(indentSpaces,true); - int i = temp.length(); - mLines->putString(oldCaretY-1,temp); - internalSetCaretXY(BufferCoord{i+1,oldCaretY}); - mUndoList->AddChange( - SynChangeReason::crDelete, - BufferCoord{1, oldCaretY}, - BufferCoord{line.length()+1, oldCaretY}, - line, - SynSelectionMode::smNormal - ); - mUndoList->AddChange( - SynChangeReason::crInsert, - BufferCoord{1, oldCaretY}, - BufferCoord{temp.length()+1, oldCaretY}, - "", - SynSelectionMode::smNormal - ); - } - } - } else - // Remove TabWidth of indent of the current line when typing a } - if (AChar == '}') { - QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); - QString right = mLines->getString(oldCaretY-1).mid(oldCaretX); - // and the first nonblank char is this new } - if (temp.trimmed().isEmpty()) { - int indentSpaces = calcIndentSpaces(oldCaretY,"}", true); - QString line = mLines->getString(oldCaretY-1); - if (indentSpaces != leftSpaces(temp)) { + QString line = mLines->getString(oldCaretY-1); QString temp = GetLeftSpacing(indentSpaces,true) + right; int i = temp.length(); mLines->putString(oldCaretY-1,temp); @@ -2747,7 +2717,37 @@ void SynEdit::doAddChar(QChar AChar) SynChangeReason::crInsert, BufferCoord{1, oldCaretY}, BufferCoord{temp.length()+1, oldCaretY}, - "", + temp, + SynSelectionMode::smNormal + ); + } + } + } else + // Remove TabWidth of indent of the current line when typing a } + if (AChar == '}') { + QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); + QString right = mLines->getString(oldCaretY-1).mid(oldCaretX); + // and the first nonblank char is this new } + if (temp.trimmed().isEmpty()) { + int indentSpaces = calcIndentSpaces(oldCaretY,"}", true); + if (indentSpaces != leftSpaces(temp)) { + QString line = mLines->getString(oldCaretY-1); + QString temp = GetLeftSpacing(indentSpaces,true) + right; + int i = temp.length(); + mLines->putString(oldCaretY-1,temp); + internalSetCaretXY(BufferCoord{i+1,oldCaretY}); + mUndoList->AddChange( + SynChangeReason::crDelete, + BufferCoord{1, oldCaretY}, + BufferCoord{line.length()+1, oldCaretY}, + line, + SynSelectionMode::smNormal + ); + mUndoList->AddChange( + SynChangeReason::crInsert, + BufferCoord{1, oldCaretY}, + BufferCoord{temp.length()+1, oldCaretY}, + temp, SynSelectionMode::smNormal ); } From 33ba4afc20ee33a1a73475e7915aaa4248415d20 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Sat, 13 Nov 2021 10:38:48 +0800 Subject: [PATCH 5/5] - enhancement: auto indent line to column 1 when enter '#' at beginning of line - fix: when enter '{' or '}' at beginning of line, auto indent will remove all contents of the line - fix: auto indent should be turned off when reformat code - fix: auto indent should be turned off when replace in code --- NEWS.md | 6 ++ RedPandaIDE/qsynedit/SynEdit.cpp | 104 +++++++------------------------ 2 files changed, 29 insertions(+), 81 deletions(-) diff --git a/NEWS.md b/NEWS.md index a5a351aa..d62addf8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +Version 0.8.7 For Dev-C++ 7 Beta + - enhancement: auto indent line to column 1 when enter '#' at beginning of line + - fix: when enter '{' or '}' at beginning of line, auto indent will remove all contents of the line + - fix: auto indent should be turned off when reformat code + - fix: auto indent should be turned off when replace in code + Version 0.8.6 For Dev-C++ 7 Beta - enhancement: greatly reduces memory usage for symbol parsing ( memory needed for bits/stdc++.h reduced from 150m+ to 80m+) - fix: currect compiler set not correctly updated when switch between normal file and project file diff --git a/RedPandaIDE/qsynedit/SynEdit.cpp b/RedPandaIDE/qsynedit/SynEdit.cpp index 92c25e8a..cbaed964 100644 --- a/RedPandaIDE/qsynedit/SynEdit.cpp +++ b/RedPandaIDE/qsynedit/SynEdit.cpp @@ -2632,49 +2632,21 @@ void SynEdit::doAddChar(QChar AChar) } mUndoList->BeginBlock(); + // auto if (mOptions.testFlag(eoAutoIndent) && mHighlighter && mHighlighter->getClass()==SynHighlighterClass::CppHighlighter && (oldCaretY<=mLines->count()) ) { - if (AChar == '#') { - QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); - QString right = mLines->getString(oldCaretY-1).mid(oldCaretX); - // and the first nonblank char is this new { - if (temp.trimmed().isEmpty()) { - int indentSpaces = calcIndentSpaces(oldCaretY,"#", true); - QString line = mLines->getString(oldCaretY-1); - if (indentSpaces==0 && leftSpaces(temp)!=0) { - QString temp = right; - int i = temp.length(); - mLines->putString(oldCaretY-1,""); - internalSetCaretXY(BufferCoord{i+1,oldCaretY}); - mUndoList->AddChange( - SynChangeReason::crDelete, - BufferCoord{1, oldCaretY}, - BufferCoord{line.length()+1, oldCaretY}, - line, - SynSelectionMode::smNormal - ); - mUndoList->AddChange( - SynChangeReason::crInsert, - BufferCoord{1, oldCaretY}, - BufferCoord{temp.length()+1, oldCaretY}, - temp, - SynSelectionMode::smNormal - ); - } - } - } else //unindent if ':' at end of the line if (AChar == ':') { QString line = mLines->getString(oldCaretY-1); if (line.length() < oldCaretX) { int indentSpaces = calcIndentSpaces(oldCaretY,line+":", true); if (indentSpaces != leftSpaces(line)) { - QString temp = GetLeftSpacing(indentSpaces,true) + TrimLeft(line); - int i = temp.length(); - mLines->putString(oldCaretY-1,temp); + QString newLine = GetLeftSpacing(indentSpaces,true) + TrimLeft(line); + int i = newLine.length(); + mLines->putString(oldCaretY-1,newLine); internalSetCaretXY(BufferCoord{i+1,oldCaretY}); mUndoList->AddChange( SynChangeReason::crDelete, @@ -2686,68 +2658,35 @@ void SynEdit::doAddChar(QChar AChar) mUndoList->AddChange( SynChangeReason::crInsert, BufferCoord{1, oldCaretY}, - BufferCoord{temp.length()+1, oldCaretY}, - temp, + BufferCoord{newLine.length()+1, oldCaretY}, + newLine, SynSelectionMode::smNormal ); } } - } else - //unindent if '{' is after an statement like 'if' 'for' - if (AChar == '{') { - QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); - QString right = mLines->getString(oldCaretY-1).mid(oldCaretX); + } else if (AChar == '{' || AChar == '}' || AChar == '#') { + //Reindent line when add '{' '}' and '#' at the beginning + QString left = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); // and the first nonblank char is this new { - if (temp.trimmed().isEmpty()) { - int indentSpaces = calcIndentSpaces(oldCaretY,"{", true); - if (indentSpaces != leftSpaces(temp)) { - QString line = mLines->getString(oldCaretY-1); - QString temp = GetLeftSpacing(indentSpaces,true) + right; - int i = temp.length(); - mLines->putString(oldCaretY-1,temp); - internalSetCaretXY(BufferCoord{i+1,oldCaretY}); + if (left.trimmed().isEmpty()) { + int indentSpaces = calcIndentSpaces(oldCaretY,AChar, true); + if (indentSpaces != leftSpaces(left)) { + QString right = mLines->getString(oldCaretY-1).mid(oldCaretX); + QString newLeft = GetLeftSpacing(indentSpaces,true); + mLines->putString(oldCaretY-1,newLeft+right); + internalSetCaretXY(BufferCoord{newLeft.length()+1,oldCaretY}); mUndoList->AddChange( SynChangeReason::crDelete, BufferCoord{1, oldCaretY}, - BufferCoord{line.length()+1, oldCaretY}, - line, + BufferCoord{left.length()+1, oldCaretY}, + left, SynSelectionMode::smNormal ); mUndoList->AddChange( SynChangeReason::crInsert, BufferCoord{1, oldCaretY}, - BufferCoord{temp.length()+1, oldCaretY}, - temp, - SynSelectionMode::smNormal - ); - } - } - } else - // Remove TabWidth of indent of the current line when typing a } - if (AChar == '}') { - QString temp = mLines->getString(oldCaretY-1).mid(0,oldCaretX-1); - QString right = mLines->getString(oldCaretY-1).mid(oldCaretX); - // and the first nonblank char is this new } - if (temp.trimmed().isEmpty()) { - int indentSpaces = calcIndentSpaces(oldCaretY,"}", true); - if (indentSpaces != leftSpaces(temp)) { - QString line = mLines->getString(oldCaretY-1); - QString temp = GetLeftSpacing(indentSpaces,true) + right; - int i = temp.length(); - mLines->putString(oldCaretY-1,temp); - internalSetCaretXY(BufferCoord{i+1,oldCaretY}); - mUndoList->AddChange( - SynChangeReason::crDelete, - BufferCoord{1, oldCaretY}, - BufferCoord{line.length()+1, oldCaretY}, - line, - SynSelectionMode::smNormal - ); - mUndoList->AddChange( - SynChangeReason::crInsert, - BufferCoord{1, oldCaretY}, - BufferCoord{temp.length()+1, oldCaretY}, - temp, + BufferCoord{newLeft.length()+1, oldCaretY}, + newLeft, SynSelectionMode::smNormal ); } @@ -4923,6 +4862,8 @@ int SynEdit::searchReplace(const QString &sSearch, const QString &sReplace, SynS mUndoList->BeginBlock(); dobatchReplace = true; } + bool oldAutoIndent = mOptions.testFlag(SynEditorOption::eoAutoIndent); + mOptions.setFlag(SynEditorOption::eoAutoIndent,false); doSetSelText(replaceText); nReplaceLen = caretX() - nFound; // fix the caret position and the remaining results @@ -4936,6 +4877,7 @@ int SynEdit::searchReplace(const QString &sSearch, const QString &sReplace, SynS } } } + mOptions.setFlag(SynEditorOption::eoAutoIndent,oldAutoIndent); } } // search next / previous line