Skip to content

Keep the undo state in step with a run of typing - #7

Merged
leancode merged 1 commit into
masterfrom
fix-undo-action-state
Sep 13, 2026
Merged

leancode merged 1 commit into
masterfrom
fix-undo-action-state

Conversation

@leancode

Copy link
Copy Markdown
Owner

Undo stepped back further than the last edit. Typing KEEP, pressing enter, then undoing once gave back K.

Cause

store_action_state stores a new state when the action changes, and otherwise folds continued editing into the state that run already created. The second branch was guarded by:

if self.current_state < len(self.history)-1:

That is never true while editing forwards — store_state sets current_state = len(self.history)-1 every time it appends. So a typing run was recorded as its first keystroke only, and the undo point left behind held one character of what the user had actually typed.

The original carried two FIXME comments on that branch ("this if is here just for safety", "current_state might be wrong"), so it was known to be doubtful.

Fix

Fold into the current state whenever the action is unchanged, guarding only against overwriting the initial state at index 0.

Verification

sequence before after
type KEEP, enter, undo K KEEP
type KEEP, duplicate line, undo K KEEP
type HELLO, tab, undo H HELLO
type WORD, backspace, undo W WORD
type KEEP, paste, undo K KEEP

Unchanged where it was already right, which is the risk in touching history:

sequence result
type KEEP, undo empty
type, undo, redo KEEP
three lines, five undos empty
two undos then two redos `A
undo, then type (new branch) `A

max_history trimming is untouched, test.sh passes, flake8 clean.

Found while fixing #6: the paste there inherited this, which is why its undo looked wrong.

store_action_state stores a new state when the action changes, and is
meant to fold continued editing of the same action into the state that
run already created. The guard on that second branch tested
current_state < len(history)-1, which is never true while editing
forwards: store_state leaves current_state at the end of the history.

So only the first keystroke of a run was ever recorded. Typing KEEP,
pressing enter and undoing once gave back K rather than KEEP, and the
same held for every action that followed a typing run.

Fold into the current state whenever the action is unchanged, guarding
only against writing over the initial state at index 0.
@leancode
leancode merged commit b7feda8 into master Sep 13, 2026
4 checks passed
@leancode
leancode deleted the fix-undo-action-state branch September 13, 2026 16:27
@leancode leancode mentioned this pull request Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant