1111using JetBrains . Annotations ;
1212using PluginCore ;
1313using ProjectManager . Projects ;
14- using QuickNavigate . Collections ;
1514using QuickNavigate . Helpers ;
1615
1716namespace QuickNavigate . Forms
@@ -151,8 +150,12 @@ void RefreshTree()
151150 tree . Nodes . Clear ( ) ;
152151 if ( selectedNode == null )
153152 {
154- if ( search . Length == 0 ) FillNodes ( tree . Nodes ) ;
155- else FillNodes ( tree . Nodes , search ) ;
153+ if ( search . Length == 0 ) FillNodes ( tree . Nodes , FilterTypes ( openedTypes . ToList ( ) ) ) ;
154+ else
155+ {
156+ var separator = Settings . EnableItemSpacer ? Settings . ItemSpacer : null ;
157+ FillNodes ( tree . Nodes , search , FilterTypes ( openedTypes . ToList ( ) ) , FilterTypes ( closedTypes . ToList ( ) ) , Settings . MaxItems , separator ) ;
158+ }
156159 tree . SelectedNode = tree . TopNode ;
157160 }
158161 else
@@ -166,39 +169,35 @@ void RefreshTree()
166169 tree . EndUpdate ( ) ;
167170 }
168171
169- void FillNodes ( TreeNodeCollection nodes )
172+ static void FillNodes ( [ NotNull ] TreeNodeCollection nodes , [ NotNull ] ICollection < string > types )
170173 {
171- var types = FilterTypes ( openedTypes . ToList ( ) ) ;
172- if ( types . Count > 0 ) nodes . AddRange ( CreateNodes ( types , string . Empty ) . ToArray ( ) ) ;
174+ if ( types . Count > 0 ) nodes . AddRange ( types . Select ( it => NodeFactory . CreateTreeNode ( TypeToClassModel [ it ] ) ) . ToArray ( ) ) ;
173175 }
174176
175- void FillNodes ( TreeNodeCollection nodes , string search )
177+ static void FillNodes ( [ NotNull ] TreeNodeCollection nodes , [ NotNull ] string search , [ NotNull ] List < string > openedTypes , [ NotNull ] List < string > closedTypes , int maxItems , string separator )
176178 {
177- var openedTypes = FilterTypes ( this . openedTypes . ToList ( ) ) ;
178- var closedTypes = FilterTypes ( this . closedTypes . ToList ( ) ) ;
179- var maxItems = Settings . MaxItems ;
180- var openedMatches = openedTypes . Count > 0 ? SearchUtil . FindAll ( openedTypes , search ) : new List < string > ( ) ;
181- var closedMatches = new List < string > ( ) ;
179+ if ( openedTypes . Count > 0 ) openedTypes = SearchUtil . FindAll ( openedTypes , search ) ;
180+ TreeNode [ ] openedNodes ;
181+ TreeNode [ ] closedNodes ;
182182 if ( maxItems > 0 )
183183 {
184- if ( openedMatches . Count >= maxItems ) openedMatches = openedMatches . GetRange ( 0 , maxItems ) ;
185- maxItems -= openedMatches . Count ;
186- if ( maxItems > 0 )
187- {
188- closedMatches = SearchUtil . FindAll ( closedTypes , search ) ;
189- if ( closedMatches . Count >= maxItems ) closedMatches = closedMatches . GetRange ( 0 , maxItems ) ;
190- }
184+ openedNodes = CreateClassNodes ( search , openedTypes , maxItems ) ;
185+ maxItems -= openedTypes . Count ;
186+ closedNodes = maxItems > 0 ? CreateClassNodes ( search , SearchUtil . FindAll ( closedTypes , search ) , maxItems ) : new TreeNode [ 0 ] ;
191187 }
192- else closedMatches = SearchUtil . FindAll ( closedTypes , search ) ;
193- var hasOpenedMatches = openedMatches . Count > 0 ;
194- var hasClosedMatches = closedMatches . Count > 0 ;
195- if ( hasOpenedMatches ) nodes . AddRange ( CreateNodes ( openedMatches , search ) . ToArray ( ) ) ;
196- if ( Settings . EnableItemSpacer && hasOpenedMatches && hasClosedMatches )
197- nodes . Add ( Settings . ItemSpacer ) ;
198- if ( hasClosedMatches ) nodes . AddRange ( CreateNodes ( closedMatches , search ) . ToArray ( ) ) ;
188+ else
189+ {
190+ openedNodes = CreateClassNodes ( search , openedTypes ) ;
191+ closedNodes = CreateClassNodes ( search , SearchUtil . FindAll ( closedTypes , search ) ) ;
192+ }
193+ var hasOpenedMatches = openedNodes . Length > 0 ;
194+ var hasClosedMatches = closedNodes . Length > 0 ;
195+ if ( hasOpenedMatches ) nodes . AddRange ( openedNodes ) ;
196+ if ( ! string . IsNullOrEmpty ( separator ) && hasOpenedMatches && hasClosedMatches ) nodes . Add ( separator ) ;
197+ if ( hasClosedMatches ) nodes . AddRange ( closedNodes ) ;
199198 }
200199
201- static void FillNodes ( TreeNodeCollection nodes , ClassModel inClass , string search )
200+ static void FillNodes ( [ NotNull ] TreeNodeCollection nodes , [ NotNull ] ClassModel inClass , [ NotNull ] string search )
202201 {
203202 var inFile = inClass . InFile ;
204203 var isHaxe = inFile . haXe ;
@@ -210,48 +209,33 @@ static void FillNodes(TreeNodeCollection nodes, ClassModel inClass, string searc
210209 }
211210
212211 [ NotNull ]
213- List < string > FilterTypes ( List < string > list )
214- {
215- if ( CurrentFilter != null )
216- {
217- var flags = ( FlagType ) CurrentFilter . Tag ;
218- list . RemoveAll ( it => ( TypeToClassModel [ it ] . Flags & flags ) == 0 ) ;
219- }
220- return list ;
221- }
222-
223- [ NotNull ]
224- static IEnumerable < ClassNode > CreateNodes ( [ NotNull ] IEnumerable < string > matches , [ NotNull ] string search )
212+ static TreeNode [ ] CreateClassNodes ( [ NotNull ] string search , [ NotNull ] IEnumerable < string > source )
225213 {
226- var nodes = matches . Select ( CreateNode ) ;
227- return SortNodes ( nodes , search ) ;
214+ return source
215+ . Select ( it => TypeToClassModel [ it ] )
216+ . SortModels ( search )
217+ . Select ( NodeFactory . CreateTreeNode )
218+ . ToArray ( ) ;
228219 }
229220
230221 [ NotNull ]
231- static ClassNode CreateNode ( [ NotNull ] string type )
222+ static TreeNode [ ] CreateClassNodes ( [ NotNull ] string search , [ NotNull ] IEnumerable < string > source , int count )
232223 {
233- var classModel = TypeToClassModel [ type ] ;
234- return ( ClassNode ) NodeFactory . CreateTreeNode ( classModel ) ;
224+ return source
225+ . Select ( it => TypeToClassModel [ it ] )
226+ . SortModels ( search )
227+ . Take ( count )
228+ . Select ( NodeFactory . CreateTreeNode )
229+ . ToArray ( ) ;
235230 }
236231
237232 [ NotNull ]
238- static IEnumerable < ClassNode > SortNodes ( [ NotNull ] IEnumerable < ClassNode > nodes , [ NotNull ] string search )
233+ List < string > FilterTypes ( [ NotNull ] List < string > list )
239234 {
240- search = search . ToLower ( ) ;
241- var nodes0 = new List < ClassNode > ( ) ;
242- var nodes1 = new List < ClassNode > ( ) ;
243- var nodes2 = new List < ClassNode > ( ) ;
244- foreach ( var node in nodes )
245- {
246- var name = node . Name . ToLower ( ) ;
247- if ( name == search ) nodes0 . Add ( node ) ;
248- else if ( name . StartsWith ( search ) ) nodes1 . Add ( node ) ;
249- else nodes2 . Add ( node ) ;
250- }
251- nodes0 . Sort ( TypeExplorerNodeComparer . Package ) ;
252- nodes1 . Sort ( TypeExplorerNodeComparer . NameIgnoreCase ) ;
253- nodes2 . Sort ( TypeExplorerNodeComparer . NamePackageIgnoreCase ) ;
254- return nodes0 . Concat ( nodes1 ) . Concat ( nodes2 ) ;
235+ if ( CurrentFilter == null ) return list ;
236+ var flags = ( FlagType ) CurrentFilter . Tag ;
237+ list . RemoveAll ( it => ( TypeToClassModel [ it ] . Flags & flags ) == 0 ) ;
238+ return list ;
255239 }
256240
257241 protected override void ShowContextMenu ( )
@@ -324,7 +308,7 @@ public void AddFilter(int imageIndex, FlagType flag, Keys shortcut, string enabl
324308 }
325309 }
326310
327- void RefreshFilterTip ( Button filter )
311+ void RefreshFilterTip ( [ NotNull ] Button filter )
328312 {
329313 var text = filter == CurrentFilter ? filterToDisabledTip [ filter ] : filterToEnabledTip [ filter ] ;
330314 if ( filterToolTip == null ) filterToolTip = new ToolTip ( ) ;
@@ -548,4 +532,54 @@ void OnTimerTick(object sender, EventArgs e)
548532
549533 #endregion
550534 }
535+
536+ internal static class ListExtensions
537+ {
538+ static readonly List < ClassModel > Nodes0 = new List < ClassModel > ( ) ;
539+ static readonly List < ClassModel > Nodes1 = new List < ClassModel > ( ) ;
540+ static readonly List < ClassModel > Nodes2 = new List < ClassModel > ( ) ;
541+
542+ public static List < ClassModel > SortModels ( this IEnumerable < ClassModel > list , string search )
543+ {
544+ Nodes0 . Clear ( ) ;
545+ Nodes1 . Clear ( ) ;
546+ Nodes2 . Clear ( ) ;
547+ search = search . ToLower ( ) ;
548+ foreach ( var it in list )
549+ {
550+ var name = it . Name . ToLower ( ) ;
551+ if ( name == search ) Nodes0 . Add ( it ) ;
552+ else if ( name . StartsWith ( search ) ) Nodes1 . Add ( it ) ;
553+ else Nodes2 . Add ( it ) ;
554+ }
555+ Nodes0 . Sort ( ClassModelComparers . Package ) ;
556+ Nodes1 . Sort ( ClassModelComparers . Name ) ;
557+ Nodes2 . Sort ( ClassModelComparers . NamePackage ) ;
558+ Nodes0 . AddRange ( Nodes1 ) ;
559+ Nodes0 . AddRange ( Nodes2 ) ;
560+ return Nodes0 ;
561+ }
562+ }
563+
564+ internal static class ClassModelComparers
565+ {
566+ internal static IComparer < ClassModel > Name = new NameComparer ( ) ;
567+ internal static IComparer < ClassModel > Package = new PackageComparer ( ) ;
568+ internal static IComparer < ClassModel > NamePackage = new NamePackageComparer ( ) ;
569+
570+ public class NameComparer : IComparer < ClassModel >
571+ {
572+ public int Compare ( ClassModel x , ClassModel y ) => CaseSensitiveImportComparer . CompareImports ( x . Name , y . Name ) ;
573+ }
574+
575+ public class PackageComparer : IComparer < ClassModel >
576+ {
577+ public int Compare ( ClassModel x , ClassModel y ) => CaseSensitiveImportComparer . CompareImports ( x . InFile . Package , y . InFile . Package ) ;
578+ }
579+
580+ public class NamePackageComparer : IComparer < ClassModel >
581+ {
582+ public int Compare ( ClassModel x , ClassModel y ) => CaseSensitiveImportComparer . CompareImports ( x . Name + "." + x . InFile . Package , y . Name + "." + y . InFile . Package ) ;
583+ }
584+ }
551585}
0 commit comments