@@ -38,7 +38,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3838
3939 return false ;
4040 } , ( context , ct ) => new RunContext ( ( InvocationExpressionSyntax ) context . Node , context . SemanticModel ) )
41- . WithTrackingName ( "ConsoleApp.Run.CreateSyntaxProvider" ) ;
41+ . WithTrackingName ( "ConsoleApp.Run.CreateSyntaxProvider" ) ; // annotate for IncrementalGeneratorTest
4242
4343 context . RegisterSourceOutput ( runSource , EmitConsoleAppRun ) ;
4444
@@ -584,7 +584,7 @@ static void EmitConsoleAppRun(SourceProductionContext sourceProductionContext, R
584584 sb . AppendLine ( GeneratedCodeHeader ) ;
585585 using ( sb . BeginBlock ( "internal static partial class ConsoleApp" ) )
586586 {
587- var emitter = new Emitter ( wellKnownTypes ) ;
587+ var emitter = new Emitter ( ) ;
588588 var withId = new Emitter . CommandWithId ( null , command , - 1 ) ;
589589 emitter . EmitRun ( sb , withId , isRunAsync ) ;
590590 }
@@ -594,7 +594,7 @@ static void EmitConsoleAppRun(SourceProductionContext sourceProductionContext, R
594594 help . AppendLine ( GeneratedCodeHeader ) ;
595595 using ( help . BeginBlock ( "internal static partial class ConsoleApp" ) )
596596 {
597- var emitter = new Emitter ( wellKnownTypes ) ;
597+ var emitter = new Emitter ( ) ;
598598 emitter . EmitHelp ( help , command ) ;
599599 }
600600 sourceProductionContext . AddSource ( "ConsoleApp.Run.Help.g.cs" , help . ToString ( ) ) ;
@@ -604,9 +604,9 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
604604 {
605605 if ( generatorSyntaxContexts . Length == 0 ) return ;
606606
607- var model = generatorSyntaxContexts [ 0 ] . Model ;
607+ // var model = generatorSyntaxContexts[0].Model;
608608
609- var wellKnownTypes = new WellKnownTypes ( model . Compilation ) ;
609+ // var wellKnownTypes = new WellKnownTypes(model.Compilation);
610610
611611 // validation, invoke in loop is not allowed.
612612 foreach ( var item in generatorSyntaxContexts )
@@ -638,7 +638,7 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
638638 {
639639 var genericName = ( x . Node . Expression as MemberAccessExpressionSyntax ) ? . Name as GenericNameSyntax ;
640640 var genericType = genericName ! . TypeArgumentList . Arguments [ 0 ] ;
641- var type = model . GetTypeInfo ( genericType ) . Type ;
641+ var type = x . Model . GetTypeInfo ( genericType ) . Type ;
642642 if ( type == null ) return null ! ;
643643
644644 var filter = FilterInfo . Create ( type ) ;
@@ -663,6 +663,7 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
663663 var commands1 = methodGroup [ "Add" ]
664664 . Select ( x =>
665665 {
666+ var wellKnownTypes = new WellKnownTypes ( x . Model . Compilation ) ;
666667 var parser = new Parser ( sourceProductionContext , x . Node , x . Model , wellKnownTypes , DelegateBuildType . OnlyActionFunc , globalFilters ) ;
667668 var command = parser . ParseAndValidateForBuilderDelegateRegistration ( ) ;
668669
@@ -680,6 +681,7 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
680681 var commands2 = methodGroup [ "Add<T>" ]
681682 . SelectMany ( x =>
682683 {
684+ var wellKnownTypes = new WellKnownTypes ( x . Model . Compilation ) ;
683685 var parser = new Parser ( sourceProductionContext , x . Node , x . Model , wellKnownTypes , DelegateBuildType . None , globalFilters ) ;
684686 var commands = parser . ParseAndValidateForBuilderClassRegistration ( ) ;
685687
@@ -728,7 +730,7 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
728730
729731 using ( sb . BeginBlock ( "internal static partial class ConsoleApp" ) )
730732 {
731- var emitter = new Emitter ( wellKnownTypes ) ;
733+ var emitter = new Emitter ( ) ;
732734 emitter . EmitBuilder ( sb , commandIds , hasRun , hasRunAsync ) ;
733735 }
734736 sourceProductionContext . AddSource ( "ConsoleApp.Builder.g.cs" , sb . ToString ( ) ) ;
@@ -740,7 +742,7 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
740742 using ( help . BeginBlock ( "internal static partial class ConsoleApp" ) )
741743 using ( help . BeginBlock ( "internal partial struct ConsoleAppBuilder" ) )
742744 {
743- var emitter = new Emitter ( wellKnownTypes ) ;
745+ var emitter = new Emitter ( ) ;
744746 emitter . EmitHelp ( help , commandIds ! ) ;
745747 }
746748 sourceProductionContext . AddSource ( "ConsoleApp.Builder.Help.g.cs" , help . ToString ( ) ) ;
@@ -755,6 +757,18 @@ public bool Equals(RunContext other)
755757 {
756758 if ( ! SyntaxNodeTextEqualityComparer . Default . Equals ( node . Expression , other . Node . Expression ) ) return false ;
757759
760+ return DelegateEquals ( node , model , ( other . Node , other . SemanticModel ) ) ;
761+ }
762+
763+ public override int GetHashCode ( )
764+ {
765+ // maybe this does not called so don't care impl.
766+ return SyntaxNodeTextEqualityComparer . Default . GetHashCode ( node ) ;
767+ }
768+
769+ // use for both Run and Builder.Add
770+ public static bool DelegateEquals ( InvocationExpressionSyntax node , SemanticModel model , ( InvocationExpressionSyntax Node , SemanticModel SemanticModel ) other )
771+ {
758772 var args1 = node . ArgumentList . Arguments ;
759773 var args2 = other . Node . ArgumentList . Arguments ;
760774
@@ -836,11 +850,6 @@ public bool Equals(RunContext other)
836850
837851 return false ;
838852 }
839-
840- public override int GetHashCode ( )
841- {
842- return SyntaxNodeTextEqualityComparer . Default . GetHashCode ( node ) ;
843- }
844853 }
845854
846855 readonly struct BuilderContext ( InvocationExpressionSyntax node , string name , SemanticModel model ) : IEquatable < BuilderContext >
@@ -851,15 +860,61 @@ readonly struct BuilderContext(InvocationExpressionSyntax node, string name, Sem
851860
852861 public bool Equals ( BuilderContext other )
853862 {
854- // TODO:
855- return node == other . Node ;
863+ if ( this . Name != other . Name ) return false ;
864+
865+ var typeInfo = Model . GetTypeInfo ( ( Node . Expression as MemberAccessExpressionSyntax ) ! . Expression ) ;
866+ if ( typeInfo . Type ? . Name != "ConsoleAppBuilder" )
867+ {
868+ return false ;
869+ }
870+
871+ switch ( Name )
872+ {
873+ case "Add" : // Add or Add<T>
874+ if ( ( Node . Expression as MemberAccessExpressionSyntax ) ? . Name . IsKind ( SyntaxKind . GenericName ) ?? false )
875+ {
876+ return EqualsAddClass ( other ) ;
877+ }
878+ else
879+ {
880+ return RunContext . DelegateEquals ( node , model , ( other . Node , other . Model ) ) ;
881+ }
882+ case "UseFilter" :
883+ return EqualsUseFilter ( other ) ;
884+ case "Run" :
885+ case "RunAsync" :
886+ return true ; // only check name
887+ default :
888+ break ;
889+ }
890+
891+ return false ;
892+ }
893+
894+ bool EqualsAddClass ( BuilderContext other )
895+ {
896+ return true ; // TODO:final
897+ }
898+
899+ bool EqualsUseFilter ( BuilderContext other )
900+ {
901+ var l = GetType ( Node , model ) ;
902+ var r = GetType ( other . Node , other . Model ) ;
903+
904+ return l . EqualsNamespaceAndName ( r ) ;
905+
906+ static ITypeSymbol ? GetType ( InvocationExpressionSyntax expression , SemanticModel model )
907+ {
908+ var genericName = ( expression . Expression as MemberAccessExpressionSyntax ) ? . Name as GenericNameSyntax ;
909+ var genericType = genericName ! . TypeArgumentList . Arguments [ 0 ] ;
910+ return model . GetTypeInfo ( genericType ) . Type ;
911+ }
856912 }
857913
858914 public override int GetHashCode ( )
859915 {
860- // TODO:
861- return base . GetHashCode ( ) ;
916+ // maybe this does not called so don't care impl.
917+ return SyntaxNodeTextEqualityComparer . Default . GetHashCode ( node ) ;
862918 }
863919 }
864-
865920}
0 commit comments