Skip to content

Commit 207e82b

Browse files
committed
enhance: keep repository tree sorted by name
1 parent 15456f0 commit 207e82b

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/ViewModels/EditRepositoryNode.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.ComponentModel.DataAnnotations;
1+
using System.ComponentModel.DataAnnotations;
32
using System.Threading.Tasks;
43

54
namespace SourceGit.ViewModels
@@ -50,8 +49,13 @@ public EditRepositoryNode(RepositoryNode node)
5049

5150
public override Task<bool> Sure()
5251
{
52+
bool needSort = _node.Name != _name;
5353
_node.Name = _name;
5454
_node.Bookmark = _bookmark;
55+
56+
if (needSort)
57+
Preference.SortByRenamedNode(_node);
58+
5559
return null;
5660
}
5761

src/ViewModels/Preference.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,30 @@ public static void RemoveNode(RepositoryNode node)
362362
RemoveNodeRecursive(node, _instance._repositoryNodes);
363363
}
364364

365+
public static void SortByRenamedNode(RepositoryNode node)
366+
{
367+
var container = FindNodeContainer(node, _instance._repositoryNodes);
368+
if (container == null)
369+
return;
370+
371+
var list = new List<RepositoryNode>();
372+
list.AddRange(container);
373+
list.Sort((l, r) =>
374+
{
375+
if (l.IsRepository != r.IsRepository)
376+
{
377+
return l.IsRepository ? 1 : -1;
378+
}
379+
else
380+
{
381+
return l.Name.CompareTo(r.Name);
382+
}
383+
});
384+
385+
container.Clear();
386+
foreach (var one in list) container.Add(one);
387+
}
388+
365389
public static Repository FindRepository(string path)
366390
{
367391
foreach (var repo in _instance.Repositories)
@@ -417,6 +441,21 @@ private static RepositoryNode FindNodeRecursive(string id, AvaloniaList<Reposito
417441
return null;
418442
}
419443

444+
private static AvaloniaList<RepositoryNode> FindNodeContainer(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
445+
{
446+
foreach (var sub in collection)
447+
{
448+
if (node == sub)
449+
return collection;
450+
451+
var subCollection = FindNodeContainer(node, sub.SubNodes);
452+
if (subCollection != null)
453+
return subCollection;
454+
}
455+
456+
return null;
457+
}
458+
420459
private static bool RemoveNodeRecursive(RepositoryNode node, AvaloniaList<RepositoryNode> collection)
421460
{
422461
if (collection.Contains(node))

0 commit comments

Comments
 (0)