From bf2a7cd80605fe627516823ba9d4129b0bbd17bf Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 1 Nov 2026 20:02:25 +0900 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E3=82=8A=E6=B6=88=E3=81=97=E3=81=AE?= =?UTF-8?q?=E3=83=AA=E3=82=B9=E3=83=88=E3=82=92=E6=9B=B8=E3=81=8D=E6=8F=9B?= =?UTF-8?q?=E3=81=88=E3=82=89=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UndoTree.Tests/UndoRedoManagerReaderTests.cs | 55 ++++++++++++++++++++ UndoTree.Tests/UndoRedoMirrorTests.cs | 2 + UndoTree/IUndoRedoHistory.cs | 4 ++ UndoTree/UndoRedoManagerReader.cs | 24 +++++++-- UndoTree/UndoRedoMirror.cs | 4 ++ 5 files changed, 85 insertions(+), 4 deletions(-) diff --git a/UndoTree.Tests/UndoRedoManagerReaderTests.cs b/UndoTree.Tests/UndoRedoManagerReaderTests.cs index 031c823..02928f9 100644 --- a/UndoTree.Tests/UndoRedoManagerReaderTests.cs +++ b/UndoTree.Tests/UndoRedoManagerReaderTests.cs @@ -133,6 +133,46 @@ public sealed class UndoRedoManagerReaderTests Assert.Empty(reader.Redo); } + [Fact] + public async Task TheUndoListCanBeShortenedSoThatTheManagerStopsAtTheChosenOperation() + { + var (manager, document) = Histories.NewDocument(); + Histories.Edit(manager, () => document.X = 1); + Histories.Edit(manager, () => document.X = 2); + Histories.Edit(manager, () => document.X = 3); + var reader = UndoRedoManagerReader.Create(manager)!; + var all = reader.Undo.ToArray(); + + var rewritten = reader.TrySetUndo(all[1..]); + await manager.UndoAsync(); + await manager.UndoAsync(); + + Assert.True(reader.CanRewriteUndo); + Assert.True(rewritten); + Assert.False(manager.IsUndoable); + Assert.Equal(1, document.X); + Assert.Equal([all[2], all[1]], reader.Redo); + } + + [Fact] + public async Task TheHiddenPartOfTheUndoListCanBePutBack() + { + var (manager, document) = Histories.NewDocument(); + Histories.Edit(manager, () => document.X = 1); + Histories.Edit(manager, () => document.X = 2); + var reader = UndoRedoManagerReader.Create(manager)!; + var all = reader.Undo.ToArray(); + reader.TrySetUndo(all[1..]); + + var restored = reader.TrySetUndo(all); + await manager.UndoAsync(); + await manager.UndoAsync(); + + Assert.True(restored); + Assert.Equal(0, document.X); + Assert.False(manager.IsUndoable); + } + [Fact] public void RewritingTheRedoListWithAForeignObjectIsRefused() { @@ -145,6 +185,21 @@ public sealed class UndoRedoManagerReaderTests Assert.False(rewritten); } + [Fact] + public void RewritingTheUndoListWithAForeignObjectIsRefusedAndKeepsTheList() + { + var (manager, document) = Histories.NewDocument(); + Histories.Edit(manager, () => document.X = 1); + Histories.Edit(manager, () => document.X = 2); + var reader = UndoRedoManagerReader.Create(manager)!; + var before = reader.Undo.ToArray(); + + var rewritten = reader.TrySetUndo([before[0], new object()]); + + Assert.False(rewritten); + Assert.Equal(before, reader.Undo); + } + [Fact] public void ReadingDoesNotChangeTheManager() { diff --git a/UndoTree.Tests/UndoRedoMirrorTests.cs b/UndoTree.Tests/UndoRedoMirrorTests.cs index 3e38ba3..4aa02a1 100644 --- a/UndoTree.Tests/UndoRedoMirrorTests.cs +++ b/UndoTree.Tests/UndoRedoMirrorTests.cs @@ -158,6 +158,8 @@ public sealed class UndoRedoMirrorTests using var mirror = Mirror(manager, document); Assert.False(mirror.CanReplay); + Assert.False(mirror.CanRewriteUndo); + Assert.False(mirror.TrySetUndo([])); Assert.False(mirror.CanRewriteRedo); Assert.False(mirror.TrySetRedo([])); Assert.Equal(UndoRedoHistorySource.Events, mirror.Source); diff --git a/UndoTree/IUndoRedoHistory.cs b/UndoTree/IUndoRedoHistory.cs index 384fe93..722aad4 100644 --- a/UndoTree/IUndoRedoHistory.cs +++ b/UndoTree/IUndoRedoHistory.cs @@ -15,6 +15,8 @@ internal interface IUndoRedoHistory bool CanReplay { get; } + bool CanRewriteUndo { get; } + bool CanRewriteRedo { get; } bool IsConsistent { get; } @@ -25,5 +27,7 @@ internal interface IUndoRedoHistory IReadOnlyList CommandsOf(object handle); + bool TrySetUndo(IReadOnlyList oldestFirst); + bool TrySetRedo(IReadOnlyList nextFirst); } diff --git a/UndoTree/UndoRedoManagerReader.cs b/UndoTree/UndoRedoManagerReader.cs index 1aea810..b24fa3c 100644 --- a/UndoTree/UndoRedoManagerReader.cs +++ b/UndoTree/UndoRedoManagerReader.cs @@ -28,6 +28,8 @@ internal sealed class UndoRedoManagerReader : IUndoRedoHistory public bool CanReplay => true; + public bool CanRewriteUndo => true; + public bool CanRewriteRedo => true; public bool IsConsistent @@ -96,19 +98,33 @@ internal sealed class UndoRedoManagerReader : IUndoRedoHistory public IReadOnlyList CommandsOf(object handle) => ((IEnumerable)commandsField.GetValue(handle)!).OfType().ToArray(); - public bool TrySetRedo(IReadOnlyList nextFirst) + public bool TrySetUndo(IReadOnlyList oldestFirst) => TryReplace(undoField, oldestFirst); + + public bool TrySetRedo(IReadOnlyList nextFirst) => TryReplace(redoField, nextFirst.Reverse().ToArray()); + + bool TryReplace(FieldInfo field, IReadOnlyList entries) { + IList? list = null; + object[] previous = []; try { - var list = (IList)redoField.GetValue(manager)!; + list = (IList)field.GetValue(manager)!; + previous = list.Cast().ToArray(); list.Clear(); - for (var index = nextFirst.Count - 1; index >= 0; index--) - list.Add(nextFirst[index]); + foreach (var entry in entries) + list.Add(entry); return true; } catch (Exception exception) when (exception is ArgumentException or InvalidCastException or NotSupportedException) { + if (list is not null) + { + list.Clear(); + foreach (var entry in previous) + list.Add(entry); + } + return false; } } diff --git a/UndoTree/UndoRedoMirror.cs b/UndoTree/UndoRedoMirror.cs index 1acf3de..1e93f77 100644 --- a/UndoTree/UndoRedoMirror.cs +++ b/UndoTree/UndoRedoMirror.cs @@ -27,6 +27,8 @@ internal sealed class UndoRedoMirror : IUndoRedoHistory, IDisposable public bool CanReplay => false; + public bool CanRewriteUndo => false; + public bool CanRewriteRedo => false; public bool IsConsistent => (undo.Count > 0) == manager.IsUndoable && (redo.Count > 0) == manager.IsRedoable; @@ -37,6 +39,8 @@ internal sealed class UndoRedoMirror : IUndoRedoHistory, IDisposable public IReadOnlyList CommandsOf(object handle) => ((Entry)handle).Commands.ToArray(); + public bool TrySetUndo(IReadOnlyList oldestFirst) => false; + public bool TrySetRedo(IReadOnlyList nextFirst) => false; public void Dispose()