97 行
4.0 KiB
C#
97 行
4.0 KiB
C#
namespace UndoTree;
|
|
|
|
internal static class UndoTreeRecordDiff
|
|
{
|
|
public static IReadOnlyList<UndoTreeCommandInfo> Between(IReadOnlyList<UndoTreeNodeViewModel> currentSteps, IReadOnlyList<UndoTreeNodeViewModel> targetSteps)
|
|
{
|
|
var current = Collect(currentSteps);
|
|
var target = Collect(targetSteps);
|
|
var infos = new List<UndoTreeCommandInfo>();
|
|
foreach (var key in target.Keys.Concat(current.Keys.Where(key => !target.Changes.ContainsKey(key))))
|
|
{
|
|
current.Changes.TryGetValue(key, out var leaving);
|
|
target.Changes.TryGetValue(key, out var arriving);
|
|
var shared = (leaving ?? arriving)!;
|
|
var before = leaving is null ? shared.Before : leaving.After;
|
|
var after = arriving is null ? shared.Before : arriving.After;
|
|
if (before == after)
|
|
continue;
|
|
|
|
infos.Add(new UndoTreeCommandInfo(CommandKind.Property, null, shared.TargetLabel, shared.Member, $"{before} → {after}", null));
|
|
}
|
|
|
|
foreach (var key in target.ListKeys.Concat(current.ListKeys.Where(key => !target.Lists.ContainsKey(key))))
|
|
{
|
|
target.Lists.TryGetValue(key, out var arriving);
|
|
current.Lists.TryGetValue(key, out var leaving);
|
|
var added = (arriving?.Added ?? 0) + (leaving?.Removed ?? 0);
|
|
var removed = (arriving?.Removed ?? 0) + (leaving?.Added ?? 0);
|
|
if (added > 0 || removed > 0)
|
|
infos.Add(new UndoTreeCommandInfo(CommandKind.ListItems, null, (arriving ?? leaving)!.TargetLabel, UndoRedoCommandView.ListChangeOf(added, removed), string.Empty, null));
|
|
}
|
|
|
|
var opaque = current.Opaque + target.Opaque;
|
|
if (opaque > 0)
|
|
infos.Add(new UndoTreeCommandInfo(CommandKind.Action, null, string.Empty, string.Format(Texts.DiffOpaque, opaque), string.Empty, null));
|
|
|
|
return infos;
|
|
}
|
|
|
|
static string KeyOf(UndoTreeCommandInfo command) => $"{(command.Path.Length > 0 ? command.Path : command.TargetLabel)}|{command.Member}";
|
|
|
|
static string ListKeyOf(UndoTreeCommandInfo command) => command.Path.Length > 0 ? command.Path : command.TargetLabel;
|
|
|
|
static Side Collect(IReadOnlyList<UndoTreeNodeViewModel> steps)
|
|
{
|
|
var side = new Side();
|
|
foreach (var command in steps.SelectMany(step => step.Commands))
|
|
{
|
|
if (command.Kind is CommandKind.Property or CommandKind.Indexer && command.Before.Length + command.After.Length > 0)
|
|
{
|
|
var key = KeyOf(command);
|
|
if (side.Changes.TryGetValue(key, out var change))
|
|
{
|
|
side.Changes[key] = change with { After = command.After };
|
|
}
|
|
else
|
|
{
|
|
side.Keys.Add(key);
|
|
side.Changes[key] = new Change(command.TargetLabel, command.Member, command.Before, command.After);
|
|
}
|
|
}
|
|
else if (command.Kind == CommandKind.ListItems)
|
|
{
|
|
var key = ListKeyOf(command);
|
|
var counts = side.Lists.TryGetValue(key, out var existing) ? existing : new ListCounts(command.TargetLabel, 0, 0);
|
|
if (!side.Lists.ContainsKey(key))
|
|
side.ListKeys.Add(key);
|
|
|
|
side.Lists[key] = counts with { Added = counts.Added + command.AddedCount, Removed = counts.Removed + command.RemovedCount };
|
|
}
|
|
else
|
|
{
|
|
side.Opaque++;
|
|
}
|
|
}
|
|
|
|
return side;
|
|
}
|
|
|
|
sealed class Side
|
|
{
|
|
public List<string> Keys { get; } = [];
|
|
|
|
public Dictionary<string, Change> Changes { get; } = [];
|
|
|
|
public List<string> ListKeys { get; } = [];
|
|
|
|
public Dictionary<string, ListCounts> Lists { get; } = [];
|
|
|
|
public int Opaque { get; set; }
|
|
}
|
|
|
|
sealed record Change(string TargetLabel, string Member, string Before, string After);
|
|
|
|
sealed record ListCounts(string TargetLabel, int Added, int Removed);
|
|
}
|