ファイル

44 行
2.1 KiB
C#

namespace UndoTree;
internal static class UndoTreeArchive
{
public static IReadOnlyList<UndoTreeNodeViewModel> Restore(UndoTreeRecord record)
{
var restored = new Dictionary<int, UndoTreeNodeViewModel>();
var nodes = new List<UndoTreeNodeViewModel>();
foreach (var item in record.Nodes.OrderBy(node => node.Id))
{
restored.TryGetValue(item.Parent, out var parent);
var commands = item.Commands
.Select(command => new UndoTreeCommandInfo(Enum.TryParse<CommandKind>(command.Kind, out var kind) ? kind : CommandKind.Other, null, command.Target, command.Member, command.Detail, null, false, null, command.Path ?? string.Empty, command.Before ?? string.Empty, command.After ?? string.Empty, command.AddedCount, command.RemovedCount))
.ToArray();
var node = new UndoTreeNodeViewModel(item.Id, null, parent, item.Label, commands, item.Time, 0, item.Steps) { IsArchived = true, IsOnPath = true, Tag = item.Tag ?? string.Empty, IsSaved = item.Saved, ProjectHash = item.ProjectHash ?? string.Empty };
parent?.Children.Add(node);
restored[item.Id] = node;
nodes.Add(node);
}
foreach (var branch in record.NamedBranches)
{
if (restored.TryGetValue(branch.Base, out var start))
start.BranchMarks += UndoTreeText.BranchMark(branch.Name);
}
return nodes;
}
public static IReadOnlyList<UndoTreeRecordedBranchViewModel> BranchesOf(UndoTreeRecord record, IReadOnlyList<UndoTreeNodeViewModel> nodes)
{
var byId = nodes.ToDictionary(node => node.Id);
var branches = new List<UndoTreeRecordedBranchViewModel>();
for (var index = 0; index < record.NamedBranches.Count; index++)
{
var item = record.NamedBranches[index];
if (byId.TryGetValue(item.Base, out var start) && byId.TryGetValue(item.Head, out var head) && head.PathFromRoot().Contains(start))
branches.Add(new UndoTreeRecordedBranchViewModel(item.Name, start, head, index == record.ActiveBranch));
}
return branches;
}
}