From 93a37a2bc66e0ceac1a87598804fd7622f4776c9 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Thu, 12 Jan 2023 13:46:09 +0800 Subject: [PATCH] rename symbol/batch replace correctly support undo --- RedPandaIDE/cpprefacter.cpp | 5 ++++- RedPandaIDE/mainwindow.cpp | 5 +++++ libs/qsynedit/qsynedit/qsynedit.cpp | 11 +++++++++++ libs/qsynedit/qsynedit/qsynedit.h | 2 ++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/RedPandaIDE/cpprefacter.cpp b/RedPandaIDE/cpprefacter.cpp index b67d6a4a..3cc51f53 100644 --- a/RedPandaIDE/cpprefacter.cpp +++ b/RedPandaIDE/cpprefacter.cpp @@ -281,6 +281,9 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement & if (oldEditor){ QSynedit::PSyntaxer syntaxer = syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::CPP); int posY = 0; + oldEditor->clearSelection(); + oldEditor->addGroupBreak(); + oldEditor->beginUndoBlock(); while (posY < oldEditor->document()->count()) { QString line = oldEditor->document()->getLine(posY); if (posY == 0) { @@ -317,6 +320,7 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement & oldEditor->replaceLine(posY+1,newLine); posY++; } + oldEditor->endUndoBlock(); } else { Editor editor(nullptr); QByteArray encoding; @@ -326,7 +330,6 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement & int posY = 0; while (posY < editor.document()->count()) { QString line = editor.document()->getLine(posY); - if (posY == 0) { editor.syntaxer()->resetState(); } else { diff --git a/RedPandaIDE/mainwindow.cpp b/RedPandaIDE/mainwindow.cpp index a9e49ec7..6a0fa032 100644 --- a/RedPandaIDE/mainwindow.cpp +++ b/RedPandaIDE/mainwindow.cpp @@ -7713,6 +7713,9 @@ void MainWindow::on_btnReplace_clicked() tr("Can't open file '%1' for replace!").arg(file->filename)); return; } + 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) @@ -7722,12 +7725,14 @@ void MainWindow::on_btnReplace_clicked() QMessageBox::critical(editor, tr("Replace Error"), tr("Contents has changed since last search!")); + editor->endUndoBlock(); return; } line.remove(item->start-1,results->keyword.length()); line.insert(item->start-1, newWord); editor->replaceLine(item->line,line); } + editor->endUndoBlock(); } showSearchReplacePanel(false); stretchMessagesPanel(false); diff --git a/libs/qsynedit/qsynedit/qsynedit.cpp b/libs/qsynedit/qsynedit/qsynedit.cpp index 8b10949b..5dc263d2 100644 --- a/libs/qsynedit/qsynedit/qsynedit.cpp +++ b/libs/qsynedit/qsynedit/qsynedit.cpp @@ -431,6 +431,11 @@ bool QSynEdit::getTokenAttriAtRowColEx(const BufferCoord &pos, QString &token, i return false; } +void QSynEdit::addGroupBreak() +{ + mUndoList->addGroupBreak(); +} + void QSynEdit::beginUndoBlock() { mUndoList->beginBlock(); @@ -6787,6 +6792,12 @@ BufferCoord QSynEdit::blockEnd() const return mBlockEnd; } +void QSynEdit::clearSelection() +{ + setActiveSelectionMode(SelectionMode::Normal); + setBlockBegin(caretXY()); +} + void QSynEdit::setBlockEnd(BufferCoord value) { //setActiveSelectionMode(mSelectionMode); diff --git a/libs/qsynedit/qsynedit/qsynedit.h b/libs/qsynedit/qsynedit/qsynedit.h index 5eb37c3d..5e33492e 100644 --- a/libs/qsynedit/qsynedit/qsynedit.h +++ b/libs/qsynedit/qsynedit/qsynedit.h @@ -249,6 +249,7 @@ public: bool getTokenAttriAtRowColEx(const BufferCoord& pos, QString& token, int &start, PTokenAttribute& attri); + void addGroupBreak(); void beginUndoBlock(); void endUndoBlock(); void addCaretToUndo(); @@ -320,6 +321,7 @@ public: BufferCoord blockBegin() const; BufferCoord blockEnd() const; + void clearSelection(); void setBlockBegin(BufferCoord value); void setBlockEnd(BufferCoord Value);