11using System ;
22using System . Collections . Generic ;
3-
3+ using System . IO ;
44using Avalonia . Collections ;
55
66namespace SourceGit . ViewModels
@@ -58,6 +58,8 @@ public class Builder
5858
5959 public void Run ( List < Models . Branch > branches , List < Models . Remote > remotes , bool bForceExpanded )
6060 {
61+ var folders = new Dictionary < string , BranchTreeNode > ( ) ;
62+
6163 foreach ( var remote in remotes )
6264 {
6365 var path = $ "remote/{ remote . Name } ";
@@ -69,7 +71,7 @@ public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool
6971 IsExpanded = bForceExpanded || _expanded . Contains ( path ) ,
7072 } ;
7173
72- _maps . Add ( path , node ) ;
74+ folders . Add ( path , node ) ;
7375 _remotes . Add ( node ) ;
7476 }
7577
@@ -78,16 +80,17 @@ public void Run(List<Models.Branch> branches, List<Models.Remote> remotes, bool
7880 var isFiltered = _filters . Contains ( branch . FullName ) ;
7981 if ( branch . IsLocal )
8082 {
81- MakeBranchNode ( branch , _locals , "local" , isFiltered , bForceExpanded ) ;
83+ MakeBranchNode ( branch , _locals , folders , "local" , isFiltered , bForceExpanded ) ;
8284 }
8385 else
8486 {
8587 var remote = _remotes . Find ( x => x . Name == branch . Remote ) ;
8688 if ( remote != null )
87- MakeBranchNode ( branch , remote . Children , $ "remote/{ remote . Name } ", isFiltered , bForceExpanded ) ;
89+ MakeBranchNode ( branch , remote . Children , folders , $ "remote/{ remote . Name } ", isFiltered , bForceExpanded ) ;
8890 }
8991 }
9092
93+ folders . Clear ( ) ;
9194 SortNodes ( _locals ) ;
9295 SortNodes ( _remotes ) ;
9396 }
@@ -113,67 +116,69 @@ private void CollectExpandedNodes(List<BranchTreeNode> nodes, string prefix)
113116 }
114117 }
115118
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 )
117120 {
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 )
121123 {
122- var node = new BranchTreeNode ( )
124+ roots . Add ( new BranchTreeNode ( )
123125 {
124- Name = subs [ 0 ] ,
126+ Name = branch . Name ,
125127 Type = BranchTreeNodeType . Branch ,
126128 Backend = branch ,
127129 IsExpanded = false ,
128130 IsFiltered = isFiltered ,
129- } ;
130- roots . Add ( node ) ;
131+ } ) ;
131132 return ;
132133 }
133134
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 )
137139 {
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 ) )
140143 {
141- lastFolder = value ;
144+ lastFolder = val ;
142145 }
143146 else if ( lastFolder == null )
144147 {
145148 lastFolder = new BranchTreeNode ( )
146149 {
147- Name = subs [ i ] ,
150+ Name = name ,
148151 Type = BranchTreeNodeType . Folder ,
149- IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( path ) ,
152+ IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( folder ) ,
150153 } ;
151154 roots . Add ( lastFolder ) ;
152- _maps . Add ( path , lastFolder ) ;
155+ folders . Add ( folder , lastFolder ) ;
153156 }
154157 else
155158 {
156- var folder = new BranchTreeNode ( )
159+ var cur = new BranchTreeNode ( )
157160 {
158- Name = subs [ i ] ,
161+ Name = name ,
159162 Type = BranchTreeNodeType . Folder ,
160- IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( path ) ,
163+ IsExpanded = bForceExpanded || branch . IsCurrent || _expanded . Contains ( folder ) ,
161164 } ;
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 ;
165168 }
169+
170+ start = sepIdx + 1 ;
171+ sepIdx = branch . Name . IndexOf ( '/' , start ) ;
166172 }
167173
168- var last = new BranchTreeNode ( )
174+ lastFolder . Children . Add ( new BranchTreeNode ( )
169175 {
170- Name = subs [ subs . Length - 1 ] ,
176+ Name = Path . GetFileName ( branch . Name ) ,
171177 Type = BranchTreeNodeType . Branch ,
172178 Backend = branch ,
173179 IsExpanded = false ,
174180 IsFiltered = isFiltered ,
175- } ;
176- lastFolder . Children . Add ( last ) ;
181+ } ) ;
177182 }
178183
179184 private void SortNodes ( List < BranchTreeNode > nodes )
@@ -198,7 +203,6 @@ private void SortNodes(List<BranchTreeNode> nodes)
198203 private readonly List < BranchTreeNode > _remotes = new List < BranchTreeNode > ( ) ;
199204 private readonly HashSet < string > _expanded = new HashSet < string > ( ) ;
200205 private readonly List < string > _filters = new List < string > ( ) ;
201- private readonly Dictionary < string , BranchTreeNode > _maps = new Dictionary < string , BranchTreeNode > ( ) ;
202206 }
203207 }
204208}
0 commit comments