style: add .editorconfig for code formatting. see issu #25

This commit is contained in:
leo 2024-03-18 09:37:06 +08:00
parent a8eeea4f78
commit 18aaa0a143
225 changed files with 7781 additions and 3911 deletions

View file

@ -1,15 +1,19 @@
using Avalonia.Collections;
using System;
using System;
using System.Collections.Generic;
namespace SourceGit.Models {
public enum BranchTreeNodeType {
using Avalonia.Collections;
namespace SourceGit.Models
{
public enum BranchTreeNodeType
{
Remote,
Folder,
Branch,
}
public class BranchTreeNode {
public class BranchTreeNode
{
public string Name { get; set; }
public BranchTreeNodeType Type { get; set; }
public object Backend { get; set; }
@ -17,38 +21,48 @@ namespace SourceGit.Models {
public bool IsFiltered { get; set; }
public List<BranchTreeNode> Children { get; set; } = new List<BranchTreeNode>();
public bool IsUpstreamTrackStatusVisible {
public bool IsUpstreamTrackStatusVisible
{
get => IsBranch && !string.IsNullOrEmpty((Backend as Branch).UpstreamTrackStatus);
}
public string UpstreamTrackStatus {
public string UpstreamTrackStatus
{
get => Type == BranchTreeNodeType.Branch ? (Backend as Branch).UpstreamTrackStatus : "";
}
public bool IsRemote {
public bool IsRemote
{
get => Type == BranchTreeNodeType.Remote;
}
public bool IsFolder {
public bool IsFolder
{
get => Type == BranchTreeNodeType.Folder;
}
public bool IsBranch {
public bool IsBranch
{
get => Type == BranchTreeNodeType.Branch;
}
public bool IsCurrent {
public bool IsCurrent
{
get => IsBranch && (Backend as Branch).IsCurrent;
}
}
public class Builder {
public class Builder
{
public List<BranchTreeNode> Locals => _locals;
public List<BranchTreeNode> Remotes => _remotes;
public void Run(List<Branch> branches, List<Remote> remotes) {
foreach (var remote in remotes) {
public void Run(List<Branch> branches, List<Remote> remotes)
{
foreach (var remote in remotes)
{
var path = $"remote/{remote.Name}";
var node = new BranchTreeNode() {
var node = new BranchTreeNode()
{
Name = remote.Name,
Type = BranchTreeNodeType.Remote,
Backend = remote,
@ -59,11 +73,15 @@ namespace SourceGit.Models {
_remotes.Add(node);
}
foreach (var branch in branches) {
foreach (var branch in branches)
{
var isFiltered = _filters.Contains(branch.FullName);
if (branch.IsLocal) {
if (branch.IsLocal)
{
MakeBranchNode(branch, _locals, "local", isFiltered);
} else {
}
else
{
var remote = _remotes.Find(x => x.Name == branch.Remote);
if (remote != null) MakeBranchNode(branch, remote.Children, $"remote/{remote.Name}", isFiltered);
}
@ -73,27 +91,34 @@ namespace SourceGit.Models {
SortNodes(_remotes);
}
public void SetFilters(AvaloniaList<string> filters) {
public void SetFilters(AvaloniaList<string> filters)
{
_filters.AddRange(filters);
}
public void CollectExpandedNodes(List<BranchTreeNode> nodes, bool isLocal) {
public void CollectExpandedNodes(List<BranchTreeNode> nodes, bool isLocal)
{
CollectExpandedNodes(nodes, isLocal ? "local" : "remote");
}
private void CollectExpandedNodes(List<BranchTreeNode> nodes, string prefix) {
foreach (var node in nodes) {
private void CollectExpandedNodes(List<BranchTreeNode> nodes, string prefix)
{
foreach (var node in nodes)
{
var path = prefix + "/" + node.Name;
if (node.Type != BranchTreeNodeType.Branch && node.IsExpanded) _expanded.Add(path);
CollectExpandedNodes(node.Children, path);
}
}
private void MakeBranchNode(Branch branch, List<BranchTreeNode> roots, string prefix, bool isFiltered) {
private void MakeBranchNode(Branch branch, List<BranchTreeNode> roots, string prefix, bool isFiltered)
{
var subs = branch.Name.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (subs.Length == 1) {
var node = new BranchTreeNode() {
if (subs.Length == 1)
{
var node = new BranchTreeNode()
{
Name = subs[0],
Type = BranchTreeNodeType.Branch,
Backend = branch,
@ -106,20 +131,28 @@ namespace SourceGit.Models {
BranchTreeNode lastFolder = null;
string path = prefix;
for (int i = 0; i < subs.Length - 1; i++) {
for (int i = 0; i < subs.Length - 1; i++)
{
path = string.Concat(path, "/", subs[i]);
if (_maps.ContainsKey(path)) {
if (_maps.ContainsKey(path))
{
lastFolder = _maps[path];
} else if (lastFolder == null) {
lastFolder = new BranchTreeNode() {
}
else if (lastFolder == null)
{
lastFolder = new BranchTreeNode()
{
Name = subs[i],
Type = BranchTreeNodeType.Folder,
IsExpanded = branch.IsCurrent || _expanded.Contains(path),
};
roots.Add(lastFolder);
_maps.Add(path, lastFolder);
} else {
var folder = new BranchTreeNode() {
}
else
{
var folder = new BranchTreeNode()
{
Name = subs[i],
Type = BranchTreeNodeType.Folder,
IsExpanded = branch.IsCurrent || _expanded.Contains(path),
@ -130,7 +163,8 @@ namespace SourceGit.Models {
}
}
var last = new BranchTreeNode() {
var last = new BranchTreeNode()
{
Name = subs[subs.Length - 1],
Type = BranchTreeNodeType.Branch,
Backend = branch,
@ -140,11 +174,16 @@ namespace SourceGit.Models {
lastFolder.Children.Add(last);
}
private void SortNodes(List<BranchTreeNode> nodes) {
nodes.Sort((l, r) => {
if (l.Type == r.Type) {
private void SortNodes(List<BranchTreeNode> nodes)
{
nodes.Sort((l, r) =>
{
if (l.Type == r.Type)
{
return l.Name.CompareTo(r.Name);
} else {
}
else
{
return (int)(l.Type) - (int)(r.Type);
}
});
@ -152,11 +191,11 @@ namespace SourceGit.Models {
foreach (var node in nodes) SortNodes(node.Children);
}
private List<BranchTreeNode> _locals = new List<BranchTreeNode>();
private List<BranchTreeNode> _remotes = new List<BranchTreeNode>();
private HashSet<string> _expanded = new HashSet<string>();
private List<string> _filters = new List<string>();
private Dictionary<string, BranchTreeNode> _maps = new Dictionary<string, BranchTreeNode>();
private readonly List<BranchTreeNode> _locals = new List<BranchTreeNode>();
private readonly List<BranchTreeNode> _remotes = new List<BranchTreeNode>();
private readonly HashSet<string> _expanded = new HashSet<string>();
private readonly List<string> _filters = new List<string>();
private readonly Dictionary<string, BranchTreeNode> _maps = new Dictionary<string, BranchTreeNode>();
}
}
}
}