on master help sign in
Fix transaction composition order in History::changes_since (#4981)

* Add a undo/redo split test case for crossing branches * history: Switch up/down transaction chaining order The old code tends to work in practice because, usually, either up_txns or down_txns are empty. When both have contents though, we can run into a panic trying to compose them all since they will disagree on the length of the text. This fixes the panic test case in the parent commit.

commitbcdb475b71b0fbabce57344ac8d2575c23b1bbe0 authorMichael Davis <mcarsondavis@gmail.com> dateFri 2 Dec 2022, 21:09 (4 years ago) parentdc00291
2 files changed+26−1
helix-core/src/history.rs+1−1 helix-term/tests/test/splits.rs+25
helix-core/src/history.rs+1−1
@@ -131,7 +131,7 @@ impl History {
131131 .map(|&n| self.revisions[n].inversion.clone());
132132 let down_txns = down.iter().map(|&n| self.revisions[n].transaction.clone());
133133
134− up_txns.chain(down_txns).reduce(|acc, tx| tx.compose(acc))
134+ down_txns.chain(up_txns).reduce(|acc, tx| tx.compose(acc))
135135 }
136136
137137 /// Undo the last edit.
helix-term/tests/test/splits.rs+25
@@ -161,5 +161,30 @@ async fn test_changes_in_splits_apply_to_all_views() -> anyhow::Result<()> {
161161 ))
162162 .await?;
163163
164+ // See <https://github.com/helix-editor/helix/issues/4957>.
165+ // This sequence undoes part of the history and then adds new changes, creating a
166+ // new branch in the history tree. `View::sync_changes` applies transactions down
167+ // and up to the lowest common ancestor in the path between old and new revision
168+ // numbers. If we apply these up/down transactions in the wrong order, this case
169+ // panics.
170+ // The key sequence:
171+ // * 3[<space> Create three empty lines so we are at the end of the document.
172+ // * <C-w>v<C-s> Create a split and save that point at the end of the document
173+ // in the jumplist.
174+ // * <C-w>w Switch back to the first window.
175+ // * uu Undo twice (not three times which would bring us back to the
176+ // root of the tree).
177+ // * 3[<space> Create three empty lines. Now the end of the document is past
178+ // where it was on step 1.
179+ // * <C-w>q Close window 1, focusing window 2 and causing a sync. This step
180+ // panics if we don't apply in the right order.
181+ // * %d Clean up the buffer.
182+ test((
183+ "#[|]#",
184+ "3[<space><C-w>v<C-s><C-w>wuu3[<space><C-w>q%d",
185+ "#[|]#",
186+ ))
187+ .await?;
188+
164189 Ok(())
165190}