-
Notifications
You must be signed in to change notification settings - Fork 377
Expand file tree
/
Copy pathChangeTreeNode.cs
More file actions
164 lines (140 loc) · 4.93 KB
/
ChangeTreeNode.cs
File metadata and controls
164 lines (140 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.Collections.Generic;
using System.IO;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class ChangeTreeNode : ObservableObject
{
public string FullPath { get; set; }
public string DisplayName { get; set; }
public int Depth { get; private set; } = 0;
public Models.Change Change { get; set; } = null;
public List<ChangeTreeNode> Children { get; set; } = new List<ChangeTreeNode>();
public bool IsFolder
{
get => Change == null;
}
public bool ShowConflictMarker
{
get => Change is { IsConflicted: true };
}
public string ConflictMarker
{
get => Change?.ConflictMarker ?? string.Empty;
}
public bool IsExpanded
{
get => _isExpanded;
set => SetProperty(ref _isExpanded, value);
}
public ChangeTreeNode(Models.Change c)
{
FullPath = c.Path;
DisplayName = Path.GetFileName(c.Path);
Change = c;
IsExpanded = false;
}
public ChangeTreeNode(string path, bool isExpanded)
{
FullPath = path;
DisplayName = Path.GetFileName(path);
IsExpanded = isExpanded;
}
public static List<ChangeTreeNode> Build(IList<Models.Change> changes, HashSet<string> folded, bool compactFolders)
{
var nodes = new List<ChangeTreeNode>();
var folders = new Dictionary<string, ChangeTreeNode>();
foreach (var c in changes)
{
var sepIdx = c.Path.IndexOf('/');
if (sepIdx == -1)
{
nodes.Add(new ChangeTreeNode(c));
}
else
{
ChangeTreeNode lastFolder = null;
while (sepIdx != -1)
{
var folder = c.Path.Substring(0, sepIdx);
if (folders.TryGetValue(folder, out var value))
{
lastFolder = value;
}
else if (lastFolder == null)
{
lastFolder = new ChangeTreeNode(folder, !folded.Contains(folder));
folders.Add(folder, lastFolder);
InsertFolder(nodes, lastFolder);
}
else
{
var cur = new ChangeTreeNode(folder, !folded.Contains(folder));
folders.Add(folder, cur);
InsertFolder(lastFolder.Children, cur);
lastFolder = cur;
}
sepIdx = c.Path.IndexOf('/', sepIdx + 1);
}
lastFolder?.Children.Add(new ChangeTreeNode(c));
}
}
if (compactFolders)
{
foreach (var node in nodes)
Compact(node);
}
SortAndSetDepth(nodes, 0);
folders.Clear();
return nodes;
}
private static void InsertFolder(List<ChangeTreeNode> collection, ChangeTreeNode subFolder)
{
for (int i = 0; i < collection.Count; i++)
{
if (!collection[i].IsFolder)
{
collection.Insert(i, subFolder);
return;
}
}
collection.Add(subFolder);
}
private static void Compact(ChangeTreeNode node)
{
var childrenCount = node.Children.Count;
if (childrenCount == 0)
return;
if (childrenCount > 1)
{
foreach (var c in node.Children)
Compact(c);
return;
}
var child = node.Children[0];
if (child.Change != null)
return;
node.FullPath = $"{node.FullPath}/{child.DisplayName}";
node.DisplayName = $"{node.DisplayName} / {child.DisplayName}";
node.IsExpanded = child.IsExpanded;
node.Children = child.Children;
Compact(node);
}
private static void SortAndSetDepth(List<ChangeTreeNode> nodes, int depth)
{
foreach (var node in nodes)
{
node.Depth = depth;
if (node.IsFolder)
SortAndSetDepth(node.Children, depth + 1);
}
nodes.Sort((l, r) =>
{
if (l.IsFolder == r.IsFolder)
return Models.NumericSort.Compare(l.DisplayName, r.DisplayName);
return l.IsFolder ? -1 : 1;
});
}
private bool _isExpanded = true;
}
}