CppEditor: Fix over-eager undo

We must not call QTextDocument::joinPreviousEditBlock() unless the
symbol name was actually edited, i.e. key presses that don't change the
content do not count. Otherwise, QTextDocument will merge the renaming
action with the previous change to the document in its undo stack.

Fixes: QTCREATORBUG-16350
Change-Id: Ie5a3a6a2f5a15caa038bf111673d4c32077fe4ba
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2020-10-21 17:45:48 +02:00
parent baf25e4cdb
commit 5a8501ffaf

View File

@@ -193,16 +193,22 @@ bool CppLocalRenaming::handleKeyPressEvent(QKeyEvent *e)
startRenameChange(); startRenameChange();
const bool wantEditBlock = isWithinRenameSelection(cursorPosition); const bool wantEditBlock = isWithinRenameSelection(cursorPosition);
const int undoSizeBeforeEdit = m_editorWidget->document()->availableUndoSteps();
if (wantEditBlock) { if (wantEditBlock) {
if (m_firstRenameChangeExpected) // Change inside rename selection if (m_firstRenameChangeExpected) // Change inside rename selection
cursor.beginEditBlock(); cursor.beginEditBlock();
else else
cursor.joinPreviousEditBlock(); cursor.joinPreviousEditBlock();
m_firstRenameChangeExpected = false;
} }
emit processKeyPressNormally(e); emit processKeyPressNormally(e);
if (wantEditBlock) if (wantEditBlock) {
cursor.endEditBlock(); cursor.endEditBlock();
if (m_firstRenameChangeExpected
// QTCREATORBUG-16350
&& m_editorWidget->document()->availableUndoSteps() != undoSizeBeforeEdit) {
m_firstRenameChangeExpected = false;
}
}
finishRenameChange(); finishRenameChange();
return true; return true;
} }