@@ -21,20 +21,15 @@ public static class AsyncPackageExtensions
2121 /// <returns>A collection of the command instances</returns>
2222 public static async Task < IEnumerable < object > > RegisterCommandsAsync ( this AsyncPackage package , params Assembly [ ] assemblies )
2323 {
24- List < Assembly > assembliesList = assemblies . ToList ( ) ;
25- Assembly packageAssembly = package . GetType ( ) . Assembly ;
26- if ( ! assembliesList . Contains ( packageAssembly ) )
27- assembliesList . Add ( packageAssembly ) ;
28-
2924 Type baseCommandType = typeof ( BaseCommand < > ) ;
30- IEnumerable < Type > commandTypes = assembliesList . SelectMany ( x => x . GetTypes ( ) )
25+ IEnumerable < Type > commandTypes = IncludePackageAssembly ( assemblies , package ) . SelectMany ( x => x . GetTypes ( ) )
3126 . Where ( x =>
3227 ! x . IsAbstract
3328 && x . IsAssignableToGenericType ( baseCommandType )
3429 && x . GetCustomAttribute < CommandAttribute > ( ) != null ) ;
3530
3631 List < object > commands = new ( ) ;
37- foreach ( Type ? commandType in commandTypes )
32+ foreach ( Type commandType in commandTypes )
3833 {
3934 MethodInfo initializeAsyncMethod = commandType . GetMethod (
4035 nameof ( BaseCommand < object > . InitializeAsync ) ,
@@ -56,18 +51,13 @@ public static async Task<IEnumerable<object>> RegisterCommandsAsync(this AsyncPa
5651 /// <returns></returns>
5752 public static void RegisterToolWindows ( this AsyncPackage package , params Assembly [ ] assemblies )
5853 {
59- List < Assembly > assembliesList = assemblies . ToList ( ) ;
60- Assembly packageAssembly = package . GetType ( ) . Assembly ;
61- if ( ! assembliesList . Contains ( packageAssembly ) )
62- assembliesList . Add ( packageAssembly ) ;
63-
6454 Type baseToolWindowType = typeof ( BaseToolWindow < > ) ;
65- IEnumerable < Type > toolWindowTypes = assembliesList . SelectMany ( x => x . GetTypes ( ) )
55+ IEnumerable < Type > toolWindowTypes = IncludePackageAssembly ( assemblies , package ) . SelectMany ( x => x . GetTypes ( ) )
6656 . Where ( x =>
6757 ! x . IsAbstract
6858 && x . IsAssignableToGenericType ( baseToolWindowType ) ) ;
6959
70- foreach ( Type ? toolWindowtype in toolWindowTypes )
60+ foreach ( Type toolWindowtype in toolWindowTypes )
7161 {
7262 MethodInfo initializeMethod = toolWindowtype . GetMethod (
7363 "Initialize" ,
@@ -76,5 +66,38 @@ public static void RegisterToolWindows(this AsyncPackage package, params Assembl
7666 initializeMethod . Invoke ( null , new object [ ] { package } ) ;
7767 }
7868 }
69+
70+ /// <summary>
71+ /// Finds, creates and registers <see cref="BaseFontAndColorProvider"/> implementations.
72+ /// </summary>
73+ /// <param name="package">The package to register the provider in.</param>
74+ /// <param name="assemblies">Additional assemblies to scan. The assembly that <paramref name="package"/> is in will always be scanned.</param>
75+ /// <returns>A task.</returns>
76+ public static async Task RegisterFontAndColorProvidersAsync ( this AsyncPackage package , params Assembly [ ] assemblies )
77+ {
78+ Type baseProviderType = typeof ( BaseFontAndColorProvider ) ;
79+ IEnumerable < Type > providerTypes = IncludePackageAssembly ( assemblies , package )
80+ . SelectMany ( x => x . GetTypes ( ) )
81+ . Where ( x => ! x . IsAbstract && baseProviderType . IsAssignableFrom ( x ) ) ;
82+
83+ foreach ( Type providerType in providerTypes )
84+ {
85+ ConstructorInfo ? ctor = providerType . GetConstructor ( Type . EmptyTypes )
86+ ?? throw new InvalidOperationException ( $ "The type '{ providerType . Name } ' must have a parameterless constructor.") ;
87+ BaseFontAndColorProvider provider = ( BaseFontAndColorProvider ) ctor . Invoke ( Array . Empty < object > ( ) ) ;
88+ await provider . InitializeAsync ( package ) ;
89+ }
90+ }
91+
92+ private static IReadOnlyList < Assembly > IncludePackageAssembly ( IEnumerable < Assembly > assemblies , AsyncPackage package )
93+ {
94+ List < Assembly > list = assemblies . ToList ( ) ;
95+ Assembly packageAssembly = package . GetType ( ) . Assembly ;
96+ if ( ! list . Contains ( packageAssembly ) )
97+ {
98+ list . Add ( packageAssembly ) ;
99+ }
100+ return list ;
101+ }
79102 }
80103}
0 commit comments