1
1
using System ;
2
2
using System . Collections . Generic ;
3
-
3
+ using System . IO ;
4
4
using Avalonia . Collections ;
5
5
6
6
namespace SourceGit . ViewModels
@@ -58,6 +58,8 @@ public class Builder
58
58
59
59
public void Run ( List < Models . Branch > branches , List < Models . Remote > remotes , bool bForceExpanded )
60
60
{
61
+ var folders = new Dictionary < string , BranchTreeNode > ( ) ;
62
+
61
63
foreach ( var remote in remotes )
62
64
{
63
65
var path = $ "remote/{ remote . Name } ";
@@ -69,7 +71,7 @@ public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool
69
71
IsExpanded = bForceExpanded || _expanded . Contains ( path ) ,
70
72
} ;
71
73
72
- _maps . Add ( path , node ) ;
74
+ folders . Add ( path , node ) ;
73
75
_remotes . Add ( node ) ;
74
76
}
75
77
@@ -78,16 +80,17 @@ public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool
78
80
var isFiltered = _filters . Contains ( branch . FullName ) ;
79
81
if ( branch . IsLocal )
80
82
{
81
- MakeBranchNode ( branch , _locals , "local" , isFiltered , bForceExpanded ) ;
83
+ MakeBranchNode ( branch , _locals , folders , "local" , isFiltered , bForceExpanded ) ;
82
84
}
83
85
else
84
86
{
85
87
var remote = _remotes . Find ( x => x . Name == branch . Remote ) ;
86
88
if ( remote != null )
87
- MakeBranchNode ( branch , remote . Children , $ "remote/{ remote . Name } ", isFiltered , bForceExpanded ) ;
89
+ MakeBranchNode ( branch , remote . Children , folders , $ "remote/{ remote . Name } ", isFiltered , bForceExpanded ) ;
88
90
}
89
91
}
90
92
93
+ folders . Clear ( ) ;
91
94
SortNodes ( _locals ) ;
92
95
SortNodes ( _remotes ) ;
93
96
}
@@ -113,67 +116,69 @@ private void CollectExpandedNodes(List<BranchTreeNode> nodes, string prefix)
113
116
}
114
117
}
115
118
116
- private void MakeBranchNode ( Models . Branch branch , List < BranchTreeNode > roots , string prefix , bool isFiltered , bool bForceExpanded )
119
+ private void MakeBranchNode ( Models . Branch branch , List < BranchTreeNode > roots , Dictionary < string , BranchTreeNode > folders , string prefix , bool isFiltered , bool bForceExpanded )
117
120
{
118
- var subs = branch . Name . Split ( new char [ ] { '/' } , StringSplitOptions . RemoveEmptyEntries ) ;
119
-
120
- if ( subs . Length == 1 )
121
+ var sepIdx = branch . Name . IndexOf ( '/' , StringComparison . Ordinal ) ;
122
+ if ( sepIdx == - 1 )
121
123
{
122
- var node = new BranchTreeNode ( )
124
+ roots . Add ( new BranchTreeNode ( )
123
125
{
124
- Name = subs [ 0 ] ,
126
+ Name = branch . Name ,
125
127
Type = BranchTreeNodeType . Branch ,
126
128
Backend = branch ,
127
129
IsExpanded = false ,
128
130
IsFiltered = isFiltered ,
129
- } ;
130
- roots . Add ( node ) ;
131
+ } ) ;
131
132
return ;
132
133
}
133
134
134
- BranchTreeNode lastFolder = null ;
135
- var path = prefix ;
136
- for ( var i = 0 ; i < subs . Length - 1 ; i ++ )
135
+ var lastFolder = null as BranchTreeNode ;
136
+ var start = 0 ;
137
+
138
+ while ( sepIdx != - 1 )
137
139
{
138
- path = string . Concat ( path , "/" , subs [ i ] ) ;
139
- if ( _maps . TryGetValue ( path , out var value ) )
140
+ var folder = string . Concat ( prefix , "/" , branch . Name . Substring ( 0 , sepIdx ) ) ;
141
+ var name = branch . Name . Substring ( start , sepIdx - start ) ;
142
+ if ( folders . TryGetValue ( folder , out var val ) )
140
143
{
141
- lastFolder = value ;
144
+ lastFolder = val ;
142
145
}
143
146
else if ( lastFolder == null )
144
147
{
145
148
lastFolder = new BranchTreeNode ( )
146
149
{
147
- Name = subs [ i ] ,
150
+ Name = name ,
148
151
Type = BranchTreeNodeType . Folder ,
149
- IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( path ) ,
152
+ IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( folder ) ,
150
153
} ;
151
154
roots . Add ( lastFolder ) ;
152
- _maps . Add ( path , lastFolder ) ;
155
+ folders . Add ( folder , lastFolder ) ;
153
156
}
154
157
else
155
158
{
156
- var folder = new BranchTreeNode ( )
159
+ var cur = new BranchTreeNode ( )
157
160
{
158
- Name = subs [ i ] ,
161
+ Name = name ,
159
162
Type = BranchTreeNodeType . Folder ,
160
- IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( path ) ,
163
+ IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( folder ) ,
161
164
} ;
162
- _maps . Add ( path , folder ) ;
163
- lastFolder . Children . Add ( folder ) ;
164
- lastFolder = folder ;
165
+ lastFolder . Children . Add ( cur ) ;
166
+ folders . Add ( folder , cur ) ;
167
+ lastFolder = cur ;
165
168
}
169
+
170
+ start = sepIdx + 1 ;
171
+ sepIdx = branch . Name . IndexOf ( '/' , start ) ;
166
172
}
167
173
168
- var last = new BranchTreeNode ( )
174
+ lastFolder . Children . Add ( new BranchTreeNode ( )
169
175
{
170
- Name = subs [ subs . Length - 1 ] ,
176
+ Name = Path . GetFileName ( branch . Name ) ,
171
177
Type = BranchTreeNodeType . Branch ,
172
178
Backend = branch ,
173
179
IsExpanded = false ,
174
180
IsFiltered = isFiltered ,
175
- } ;
176
- lastFolder . Children . Add ( last ) ;
181
+ } ) ;
177
182
}
178
183
179
184
private void SortNodes ( List < BranchTreeNode > nodes )
@@ -198,7 +203,6 @@ private void SortNodes(List<BranchTreeNode> nodes)
198
203
private readonly List < BranchTreeNode > _remotes = new List < BranchTreeNode > ( ) ;
199
204
private readonly HashSet < string > _expanded = new HashSet < string > ( ) ;
200
205
private readonly List < string > _filters = new List < string > ( ) ;
201
- private readonly Dictionary < string , BranchTreeNode > _maps = new Dictionary < string , BranchTreeNode > ( ) ;
202
206
}
203
207
}
204
208
}
0 commit comments