rename symbol/batch replace correctly support undo

This commit is contained in:
Roy Qu 2023-01-12 13:46:09 +08:00
parent bc1b22ad25
commit 93a37a2bc6
4 changed files with 22 additions and 1 deletions

View File

@ -281,6 +281,9 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
if (oldEditor){ if (oldEditor){
QSynedit::PSyntaxer syntaxer = syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::CPP); QSynedit::PSyntaxer syntaxer = syntaxerManager.getSyntaxer(QSynedit::ProgrammingLanguage::CPP);
int posY = 0; int posY = 0;
oldEditor->clearSelection();
oldEditor->addGroupBreak();
oldEditor->beginUndoBlock();
while (posY < oldEditor->document()->count()) { while (posY < oldEditor->document()->count()) {
QString line = oldEditor->document()->getLine(posY); QString line = oldEditor->document()->getLine(posY);
if (posY == 0) { if (posY == 0) {
@ -317,6 +320,7 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
oldEditor->replaceLine(posY+1,newLine); oldEditor->replaceLine(posY+1,newLine);
posY++; posY++;
} }
oldEditor->endUndoBlock();
} else { } else {
Editor editor(nullptr); Editor editor(nullptr);
QByteArray encoding; QByteArray encoding;
@ -326,7 +330,6 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
int posY = 0; int posY = 0;
while (posY < editor.document()->count()) { while (posY < editor.document()->count()) {
QString line = editor.document()->getLine(posY); QString line = editor.document()->getLine(posY);
if (posY == 0) { if (posY == 0) {
editor.syntaxer()->resetState(); editor.syntaxer()->resetState();
} else { } else {

View File

@ -7713,6 +7713,9 @@ void MainWindow::on_btnReplace_clicked()
tr("Can't open file '%1' for replace!").arg(file->filename)); tr("Can't open file '%1' for replace!").arg(file->filename));
return; return;
} }
editor->clearSelection();
editor->addGroupBreak();
editor->beginUndoBlock();
for (int i=file->results.count()-1;i>=0;i--) { for (int i=file->results.count()-1;i>=0;i--) {
const PSearchResultTreeItem& item = file->results[i]; const PSearchResultTreeItem& item = file->results[i];
if (!item->selected) if (!item->selected)
@ -7722,12 +7725,14 @@ void MainWindow::on_btnReplace_clicked()
QMessageBox::critical(editor, QMessageBox::critical(editor,
tr("Replace Error"), tr("Replace Error"),
tr("Contents has changed since last search!")); tr("Contents has changed since last search!"));
editor->endUndoBlock();
return; return;
} }
line.remove(item->start-1,results->keyword.length()); line.remove(item->start-1,results->keyword.length());
line.insert(item->start-1, newWord); line.insert(item->start-1, newWord);
editor->replaceLine(item->line,line); editor->replaceLine(item->line,line);
} }
editor->endUndoBlock();
} }
showSearchReplacePanel(false); showSearchReplacePanel(false);
stretchMessagesPanel(false); stretchMessagesPanel(false);

View File

@ -431,6 +431,11 @@ bool QSynEdit::getTokenAttriAtRowColEx(const BufferCoord &pos, QString &token, i
return false; return false;
} }
void QSynEdit::addGroupBreak()
{
mUndoList->addGroupBreak();
}
void QSynEdit::beginUndoBlock() void QSynEdit::beginUndoBlock()
{ {
mUndoList->beginBlock(); mUndoList->beginBlock();
@ -6787,6 +6792,12 @@ BufferCoord QSynEdit::blockEnd() const
return mBlockEnd; return mBlockEnd;
} }
void QSynEdit::clearSelection()
{
setActiveSelectionMode(SelectionMode::Normal);
setBlockBegin(caretXY());
}
void QSynEdit::setBlockEnd(BufferCoord value) void QSynEdit::setBlockEnd(BufferCoord value)
{ {
//setActiveSelectionMode(mSelectionMode); //setActiveSelectionMode(mSelectionMode);

View File

@ -249,6 +249,7 @@ public:
bool getTokenAttriAtRowColEx(const BufferCoord& pos, QString& token, bool getTokenAttriAtRowColEx(const BufferCoord& pos, QString& token,
int &start, PTokenAttribute& attri); int &start, PTokenAttribute& attri);
void addGroupBreak();
void beginUndoBlock(); void beginUndoBlock();
void endUndoBlock(); void endUndoBlock();
void addCaretToUndo(); void addCaretToUndo();
@ -320,6 +321,7 @@ public:
BufferCoord blockBegin() const; BufferCoord blockBegin() const;
BufferCoord blockEnd() const; BufferCoord blockEnd() const;
void clearSelection();
void setBlockBegin(BufferCoord value); void setBlockBegin(BufferCoord value);
void setBlockEnd(BufferCoord Value); void setBlockEnd(BufferCoord Value);