From 5beb3487069559ce43fb640f0b4ea464d96efe6c Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 1 Nov 2026 09:31:59 +0900 Subject: [PATCH] =?UTF-8?q?=E6=8C=81=E3=81=A1=E4=B8=BB=E3=81=AE=E6=8E=A2?= =?UTF-8?q?=E7=B4=A2=E7=B5=90=E6=9E=9C=E3=82=92=E6=A7=8B=E9=80=A0=E3=81=8C?= =?UTF-8?q?=E5=A4=89=E3=82=8F=E3=82=8B=E3=81=BE=E3=81=A7=E4=BD=BF=E3=81=84?= =?UTF-8?q?=E5=9B=9E=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UndoTree.Tests/UndoTreeHistoryTests.cs | 89 ++++++++++++++++++++++++-- UndoTree/UndoRedoCommandView.cs | 8 ++- UndoTree/UndoTreeCommandInfo.cs | 2 +- UndoTree/UndoTreeHistory.cs | 25 +++++--- UndoTree/UndoTreeNodeViewModel.cs | 3 + 5 files changed, 113 insertions(+), 14 deletions(-) diff --git a/UndoTree.Tests/UndoTreeHistoryTests.cs b/UndoTree.Tests/UndoTreeHistoryTests.cs index c4d21f3..3700881 100644 --- a/UndoTree.Tests/UndoTreeHistoryTests.cs +++ b/UndoTree.Tests/UndoTreeHistoryTests.cs @@ -274,19 +274,100 @@ public sealed class UndoTreeHistoryTests } [Fact] - public void TheOwnersAreBuiltOncePerVersionAndRebuiltAfterTheHistoryChanges() + public async Task TheOwnersAreKeptWhileOnlyValuesChangeAndRebuiltWhenTheStructureChanges() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); var part = new Part("p"); Histories.Edit(manager, () => document.Parts.Add(part)); - var first = history.Owners; - - Assert.Same(first, history.Owners); Assert.Equal([document], first.OwnersOf(part)); Histories.Edit(manager, () => document.X = 1); + Histories.Edit(manager, () => part.Value = 2); + + Assert.Same(first, history.Owners); + + var other = new Part("q"); + Histories.Edit(manager, () => document.Parts.Add(other)); + var second = history.Owners; + + Assert.NotSame(first, second); + Assert.Equal([document], second.OwnersOf(other)); + + await manager.UndoAsync(); + var third = history.Owners; + + Assert.NotSame(second, third); + Assert.Empty(third.OwnersOf(other)); + + await manager.UndoAsync(); + + Assert.Same(third, history.Owners); + } + + [Fact] + public void ReplacingAListOrAChildObjectRebuildsTheOwners() + { + var (manager, sheet) = Histories.NewSheet(); + using var history = Track(manager, sheet); + var part = new Part("p"); + Histories.Edit(manager, () => sheet.Parts = sheet.Parts.Add(part)); + var first = history.Owners; + + Histories.Edit(manager, () => sheet.X = 1); + + Assert.Same(first, history.Owners); + Assert.Equal([sheet], first.OwnersOf(part)); + + var replacement = new Part("r"); + Histories.Edit(manager, () => sheet.Parts = [replacement]); + + Assert.NotSame(first, history.Owners); + Assert.Equal([sheet], history.Owners.OwnersOf(replacement)); + Assert.Empty(history.Owners.OwnersOf(part)); + } + + [Fact] + public async Task JumpingAcrossAStructuralStepRebuildsTheOwnersButNotAcrossValueSteps() + { + var (manager, document) = Histories.NewDocument(); + using var history = Track(manager, document); + var root = history.Cursor; + var part = new Part("p"); + Histories.Edit(manager, () => document.Parts.Add(part)); + var added = history.Cursor; + Histories.Edit(manager, () => document.X = 1); + var edited = history.Cursor; + var owners = history.Owners; + + await history.JumpAsync(added); + + Assert.Same(owners, history.Owners); + + await history.JumpAsync(root); + + Assert.NotSame(owners, history.Owners); + Assert.Empty(history.Owners.OwnersOf(part)); + + var afterRoot = history.Owners; + await history.JumpAsync(edited); + + Assert.NotSame(afterRoot, history.Owners); + Assert.Equal([document], history.Owners.OwnersOf(part)); + } + + [Fact] + public void ClearingTheManagerDropsTheOwners() + { + var (manager, document) = Histories.NewDocument(); + using var history = Track(manager, document); + var part = new Part("p"); + Histories.Edit(manager, () => document.Parts.Add(part)); + var first = history.Owners; + + manager.Clear(); + manager.Subscribe(document); Assert.NotSame(first, history.Owners); } diff --git a/UndoTree/UndoRedoCommandView.cs b/UndoTree/UndoRedoCommandView.cs index 88a66bf..9ca7a03 100644 --- a/UndoTree/UndoRedoCommandView.cs +++ b/UndoTree/UndoRedoCommandView.cs @@ -77,9 +77,15 @@ internal sealed class UndoRedoCommandView _ => Command.GetType().Name, }; var detail = Kind is CommandKind.Property or CommandKind.Indexer && IsReadable ? $"{TextOf(OldValue)} → {TextOf(NewValue)}" : string.Empty; - return new UndoTreeCommandInfo(Kind, Target, targetLabel, member, detail, Key); + return new UndoTreeCommandInfo(Kind, Target, targetLabel, member, detail, Key, ChangesOwnership); } + bool ChangesOwnership => Kind is CommandKind.Property or CommandKind.Indexer + ? !IsReadable || HoldsObject(OldValue) || HoldsObject(NewValue) + : true; + + static bool HoldsObject(object? value) => value is not null and not string && !value.GetType().IsValueType; + static UndoRedoCommandView ReadValueChange(IUndoRedoCommandBase command, CommandKind kind, string memberField) { var view = new UndoRedoCommandView(command, kind); diff --git a/UndoTree/UndoTreeCommandInfo.cs b/UndoTree/UndoTreeCommandInfo.cs index fd0c297..48009d0 100644 --- a/UndoTree/UndoTreeCommandInfo.cs +++ b/UndoTree/UndoTreeCommandInfo.cs @@ -9,7 +9,7 @@ internal enum CommandKind Other, } -internal sealed record UndoTreeCommandInfo(CommandKind Kind, object? Target, string TargetLabel, string Member, string Detail, UndoTreeChangeKey? Key) +internal sealed record UndoTreeCommandInfo(CommandKind Kind, object? Target, string TargetLabel, string Member, string Detail, UndoTreeChangeKey? Key, bool ChangesOwnership = false) { public string Text => TargetLabel.Length > 0 ? $"{TargetLabel} / {Member}" : Member; } diff --git a/UndoTree/UndoTreeHistory.cs b/UndoTree/UndoTreeHistory.cs index 9d96677..3c446f4 100644 --- a/UndoTree/UndoTreeHistory.cs +++ b/UndoTree/UndoTreeHistory.cs @@ -25,7 +25,6 @@ internal class UndoTreeHistory : IDisposable UndoTreeRecorder? recorder; UndoTreeSaveWatcher? saveWatcher; UndoTreeOwners? owners; - long ownersVersion; internal UndoTreeHistory(UndoRedoManager manager, AsyncAwaitStatus asyncAwaitStatus, IReadOnlyList roots, int capacity, UndoRedoManagerReader? reader, Action? report = null) { @@ -72,12 +71,7 @@ internal class UndoTreeHistory : IDisposable { get { - if (owners is null || ownersVersion != Version) - { - owners = new UndoTreeOwners(Roots); - ownersVersion = Version; - } - + owners ??= new UndoTreeOwners(Roots); return owners; } } @@ -482,6 +476,21 @@ internal class UndoTreeHistory : IDisposable redoNodes.Clear(); Root = CreateRoot(); Cursor = Root; + owners = null; + } + + void MoveCursor(UndoTreeNodeViewModel next) + { + if (owners is not null && ChangesOwnershipBetween(Cursor, next)) + owners = null; + + Cursor = next; + } + + bool ChangesOwnershipBetween(UndoTreeNodeViewModel from, UndoTreeNodeViewModel to) + { + var ancestor = CommonAncestor(from, to); + return PathFromRoot(from).Skip(ancestor.Depth + 1).Concat(PathFromRoot(to).Skip(ancestor.Depth + 1)).Any(node => node.ChangesOwnership); } bool Merge(IReadOnlyList undo, IReadOnlyList redo) @@ -523,7 +532,7 @@ internal class UndoTreeHistory : IDisposable if (!ReferenceEquals(Cursor, node)) { - Cursor = node; + MoveCursor(node); changed = true; } diff --git a/UndoTree/UndoTreeNodeViewModel.cs b/UndoTree/UndoTreeNodeViewModel.cs index 3aed5f7..f1964a3 100644 --- a/UndoTree/UndoTreeNodeViewModel.cs +++ b/UndoTree/UndoTreeNodeViewModel.cs @@ -15,6 +15,7 @@ internal class UndoTreeNodeViewModel : Bindable Time = time; Visit = visit; Keys = commands.Select(command => command.Key).OfType().Distinct().ToArray(); + ChangesOwnership = commands.Any(command => command.ChangesOwnership); } public int Id { get; } @@ -31,6 +32,8 @@ internal class UndoTreeNodeViewModel : Bindable public IReadOnlyList Keys { get; } + public bool ChangesOwnership { get; } + public string Label { get; } public DateTime Time { get; }