public void Initialize(IncrementalGeneratorInitializationContext context) { context.RegisterPostInitializationOutput(PostInitializationCallback); IncrementalValueProvider <ImmutableArray <MethodDeclarationSyntax> > syntaxProvider = context.SyntaxProvider .CreateSyntaxProvider(SyntaxProviderPredicate, SyntaxProviderTransform) .Where(static method => method is not null)
public void Initialize(IncrementalGeneratorInitializationContext context) { IncrementalValuesProvider <TypeDeclarationSyntax> providerTypes = context.SyntaxProvider.CreateSyntaxProvider( (node, _) => SyntaxCollector.IsCandidateType(node), (syntaxContext, _) => (TypeDeclarationSyntax)syntaxContext.Node); IncrementalValuesProvider <InvocationExpressionSyntax> getServiceCalls = context.SyntaxProvider.CreateSyntaxProvider( (node, _) => SyntaxCollector.IsGetServiceExpression(node), (syntaxContext, _) => (InvocationExpressionSyntax)syntaxContext.Node); var collectedServiceCalls = getServiceCalls.Collect(); var providers = providerTypes.Combine(collectedServiceCalls).Combine(context.CompilationProvider); context.RegisterSourceOutput(providers, (productionContext, inputs) => Execute(new GeneratorContext( productionContext, ImmutableArray.Create(inputs.Item1.Item1), inputs.Item1.Item2, inputs.Item2))); context.RegisterPostInitializationOutput(c => { c.AddSource("Attributes.cs", ReadAttributesFile()); }); }
/// <inheritdoc /> public void Initialize(IncrementalGeneratorInitializationContext context) { // Register the attribute source context.RegisterPostInitializationOutput(ctx => ctx.AddSource("TagAttribute.g.cs", Sources.Attributes)); IncrementalValuesProvider <ClassDeclarationSyntax> classDeclarations = context.SyntaxProvider.CreateSyntaxProvider(IsAttributedProperty, GetPotentialClassesForGeneration) .Where(static m => m is not null) !;
public void Initialize(IncrementalGeneratorInitializationContext context) { context.RegisterPostInitializationOutput(GenerateInitialCode); var options = context.AnalyzerConfigOptionsProvider .Select(Options.Select) .WithComparer(EqualityComparer <Options> .Default); var file = context.CompilationProvider .Select(static (compilation, token) =>
public void Initialize(IncrementalGeneratorInitializationContext context) => context.RegisterPostInitializationOutput(ctx => { ctx.AddSource("RequiredPropertiesSourceGen.cs", @" using System; [AttributeUsage(AttributeTargets.Property)] public class RequiredPropertyAttribute: Attribute { }"); });
public void Initialize(IncrementalGeneratorInitializationContext context) { context.RegisterPostInitializationOutput(ctx => ctx.AddSource( "Attributes.g.cs", Templates.UnionTypeTemplates.StaticCode)); var unionTypeClasses = context.SyntaxProvider .CreateSyntaxProvider( predicate: static (s, _) => s.IsTypeDeclarationWithAttributes(), transform: static (ctx, _) => GeneratorHelper.GetSemanticTargetForGeneration(ctx, UnionTypeAttribute) ) .Where(static target => target != null) .Select(static (target, _) => target !); var compilationAndClasses = context.CompilationProvider.Combine(unionTypeClasses.Collect()); context.RegisterSourceOutput( compilationAndClasses,
/// <summary> /// Registers a post-initialization step for the source generator context to add some embedded resources to the /// compilation. /// </summary> /// <param name="context">The source generator context.</param> /// <param name="prefix">The prefix of the embedded sources to add.</param> public static void RegisterEmbeddedSourceCodeInjection( this IncrementalGeneratorInitializationContext context, string prefix) { var assembly = Assembly.GetExecutingAssembly(); var sourcesToInject = assembly .GetManifestResourceNames() .Where(m => m.StartsWith(prefix)); context.RegisterPostInitializationOutput(ctx => { foreach (var source in sourcesToInject) { ctx.AddSource( source, SourceText.From(assembly.ReadManifestResourceText(source), Encoding.UTF8)); } }); }