diff --git a/src/editor/dtextedit.cpp b/src/editor/dtextedit.cpp index 00bc6d77..cc2a52d3 100644 --- a/src/editor/dtextedit.cpp +++ b/src/editor/dtextedit.cpp @@ -3815,7 +3815,7 @@ void TextEdit::undo_() m_pUndoStack->undo(); qCDebug(appLog) << "Undo operation executed, new stack index:" << m_pUndoStack->index(); - const bool needUpdate = refreshUndoRedoColumnStatus(); + const bool needUpdate = refreshUndoRedoColumnStatusAfterUndo(); if (needUpdate) { renderAllSelections(); update(); @@ -9224,6 +9224,41 @@ bool TextEdit::refreshUndoRedoColumnStatus() return false; } +/** + @brief Call this function after undo, refresh column edit status. + After undo(), index() has decremented, so command(index()) points to the + just-undone (redo-pending) command. To determine the current edit mode, + check command(index()-1) — the current top of the applied stack. + @return True if need update selection status. + */ +bool TextEdit::refreshUndoRedoColumnStatusAfterUndo() +{ + if (const QUndoCommand *cmd = m_pUndoStack->command(m_pUndoStack->index() - 1)) { + const int id = cmd->id(); + const bool columnEdit = (id != Utils::IdDefault) && (Utils::IdColumnEdit & id); + if (columnEdit != m_bIsAltMod) { + // update selection + m_bIsAltMod = columnEdit; + if (!m_bIsAltMod) { + m_altModSelections.clear(); + } + + return true; + } + + // in column editing, update every time. + if (columnEdit) { + return true; + } + } else { + // No commands applied after undo — leave column edit mode state + // unchanged; column mode entry/exit is driven by user actions, + // orthogonal to the undo stack state. + } + + return false; +} + /** * @brief 文档内容变更时触发 * 当前用于记录中键更新前后的动作并压入撤销栈,需要注意鼠标中键不会移除字符仅会插入选中的字符。 diff --git a/src/editor/dtextedit.h b/src/editor/dtextedit.h index b3cce739..7a7bb5d5 100644 --- a/src/editor/dtextedit.h +++ b/src/editor/dtextedit.h @@ -628,6 +628,7 @@ public slots: bool findFoldBlock(int line, QTextBlock &beginBlock, QTextBlock &endBlock, QTextBlock &curBlock); bool refreshUndoRedoColumnStatus(); + bool refreshUndoRedoColumnStatusAfterUndo(); private slots: // 文档内容变更时触发