public void Initialize(IncrementalGeneratorInitializationContext context)
 {
     context.RegisterSourceOutput(context.AdditionalTextsProvider, (context, additionalText) =>
                                  context.AddSource(
                                      GetGeneratedFileName(additionalText.Path),
                                      GenerateSourceForAdditionalFile(additionalText, context.CancellationToken)));
 }
        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());
            });
        }
Esempio n. 3
0
        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            var classDeclarations = context.SyntaxProvider.CreateSyntaxProvider(
                predicate: (s, t) => s is ClassDeclarationSyntax,
                transform: GetTypeSymbols).Collect();

            context.RegisterSourceOutput(classDeclarations, GenerateSource);
        }
Esempio n. 4
0
        // Example input:
        //
        //    [EventSource(Guid = "49592C0F-5A05-516D-AA4B-A64E02026C89", Name = "System.Runtime")]
        //    [EventSourceAutoGenerate]
        //    internal sealed partial class RuntimeEventSource : EventSource
        //
        // Example generated output:
        //
        //     using System;
        //
        //     namespace System.Diagnostics.Tracing
        //     {
        //         partial class RuntimeEventSource
        //         {
        //             private RuntimeEventSource() : base(new Guid(0x49592c0f,0x5a05,0x516d,0xaa,0x4b,0xa6,0x4e,0x02,0x02,0x6c,0x89), "System.Runtime") { }
        //
        //             private protected override ReadOnlySpan<byte> ProviderMetadata => new byte[] { 0x11, 0x0, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x0, };
        //         }
        //     }

        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            IncrementalValuesProvider <EventSourceClass> eventSourceClasses =
                context.SyntaxProvider
                .CreateSyntaxProvider(IsSyntaxTargetForGeneration, GetSemanticTargetForGeneration)
                .Where(x => x is not null);

            context.RegisterSourceOutput(eventSourceClasses, EmitSourceFile);
        }
Esempio n. 5
0
        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            var syntaxProvider = context.SyntaxProvider.CreateSyntaxProvider(
                (node, _) => node is StructDeclarationSyntax tds && tds.AttributeLists.ContainsAttribute("StringEnum"),
                (syntaxContext, _) => syntaxContext
                );

            context.RegisterSourceOutput(syntaxProvider, GenerateEnum);
        }
Esempio n. 6
0
    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        var declarations = context
                           .SyntaxProvider
                           .CreateSyntaxProvider(SyntaxProvider.Filter, SyntaxProvider.Transform)
                           .Where(x => x is not null);

        var compilationsAndDeclarations = context.CompilationProvider.Combine(declarations.Collect());

        context.RegisterSourceOutput(compilationsAndDeclarations, SourceOutput.Write);
    }
Esempio n. 7
0
        // Example input:
        //
        //    [EventSource(Guid = "49592C0F-5A05-516D-AA4B-A64E02026C89", Name = "System.Runtime")]
        //    [EventSourceAutoGenerate]
        //    internal sealed partial class RuntimeEventSource : EventSource
        //
        // Example generated output:
        //
        //     using System;
        //
        //     namespace System.Diagnostics.Tracing
        //     {
        //         partial class RuntimeEventSource
        //         {
        //             private RuntimeEventSource() : base(new Guid(0x49592c0f,0x5a05,0x516d,0xaa,0x4b,0xa6,0x4e,0x02,0x02,0x6c,0x89), "System.Runtime") { }
        //
        //             private protected override ReadOnlySpan<byte> ProviderMetadata => new byte[] { 0x11, 0x0, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x0, };
        //         }
        //     }

        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            const string EventSourceAutoGenerateAttribute = "System.Diagnostics.Tracing.EventSourceAutoGenerateAttribute";

            IncrementalValuesProvider <EventSourceClass> eventSourceClasses =
                context.SyntaxProvider.ForAttributeWithMetadataName(
                    context,
                    EventSourceAutoGenerateAttribute,
                    (node, _) => node is ClassDeclarationSyntax,
                    GetSemanticTargetForGeneration)
                .Where(x => x is not null);

            context.RegisterSourceOutput(eventSourceClasses, EmitSourceFile);
        }
        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            IncrementalValuesProvider <ClassDeclarationSyntax> classDeclarations = context.SyntaxProvider
                                                                                   .ForAttributeWithMetadataName(
                context,
                Parser.JsonSerializableAttributeFullName,
                (node, _) => node is ClassDeclarationSyntax,
                (context, _) => (ClassDeclarationSyntax)context.TargetNode);

            IncrementalValueProvider <(Compilation, ImmutableArray <ClassDeclarationSyntax>)> compilationAndClasses =
                context.CompilationProvider.Combine(classDeclarations.Collect());

            context.RegisterSourceOutput(compilationAndClasses, (spc, source) => Execute(source.Item1, source.Item2, spc));
        }
        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            var _attributes    = "GenerateHandler,GenerateRequestMethods,GenerateHandlerMethods";
            var syntaxProvider = context.SyntaxProvider.CreateSyntaxProvider(
                (syntaxNode, _) =>
                syntaxNode is TypeDeclarationSyntax tds
                and(ClassDeclarationSyntax or RecordDeclarationSyntax or InterfaceDeclarationSyntax) &&
                tds.AttributeLists.ContainsAttribute(_attributes), (syntaxContext, _) => syntaxContext
                )
                                 .Combine(context.CompilationProvider)
                                 .Select(
                (tuple, _) =>
            {
                var(syntaxContext, compilaiton) = tuple;
                var additionalUsings            = new HashSet <string>
                {
                    "System",
                    "System.Collections.Generic",
                    "System.Threading",
                    "System.Threading.Tasks",
                    "MediatR",
                    "Microsoft.Extensions.DependencyInjection"
                };

                GeneratorData?actionItem = null;
                Diagnostic?diagnostic    = null;

                try
                {
                    actionItem = GeneratorData.Create(
                        compilaiton, (TypeDeclarationSyntax)syntaxContext.Node, syntaxContext.SemanticModel, additionalUsings
                        );
                }
                catch (Exception e)
                {
                    diagnostic = Diagnostic.Create(
                        GeneratorDiagnostics.Exception, syntaxContext.Node.GetLocation(), e.Message,
                        e.StackTrace ?? string.Empty
                        );
                    Debug.WriteLine(e);
                    Debug.WriteLine(e.StackTrace);
                }

                return(actionItem, diagnostic, additionalUsings);
            }
                );

            context.RegisterSourceOutput(syntaxProvider, GenerateHandlerMethods);
            context.RegisterSourceOutput(
                syntaxProvider.Where(z => z.actionItem is { }).SelectMany((z, _) => z.actionItem !.AssemblyJsonRpcHandlersAttributeArguments).Collect(),
Esempio n. 10
0
        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            var candidatesProvider = context.SyntaxProvider.CreateSyntaxProvider(
                (syntaxNode, _) => SourceGeneratorSyntaxReceiver.IsCandidate(syntaxNode),
                (syntaxContext, _) => (ClassDeclarationSyntax)syntaxContext.Node);

            var assemblyResolverProvider = context.AnalyzerConfigOptionsProvider.Select((provider, _) => new AssemblyResolver(provider.GlobalOptions));

            var compilationProvider = context.CompilationProvider.Select((compilation, _) => compilation);

            var source = candidatesProvider
                         .Collect()
                         .Combine(assemblyResolverProvider)
                         .Combine(compilationProvider)
                         .Select((i, _) => (i.Left.Left, i.Left.Right, i.Right));

            context.RegisterSourceOutput(source, Execute);
        }
Esempio n. 11
0
        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            var syntaxProvider = context.SyntaxProvider.CreateSyntaxProvider(
                predicate: (syntaxNode, _) => IsSupportedSyntaxNode(syntaxNode),
                transform: (generatorSyntaxContext, _) => generatorSyntaxContext.Node);

            var globalConfig = ProjectFileReader.ReadApplicationConfig(context.AnalyzerConfigOptionsProvider);

            var inputs = context.CompilationProvider
                         .Combine(syntaxProvider.Collect())
                         .Combine(globalConfig)
                         .Select((data, cancellationToken)
                                 => (Compilation: data.Left.Left,
                                     Nodes: data.Left.Right,
                                     ApplicationConfig: data.Right.ApplicationConfig,
                                     ApplicationConfigDiagnostics: data.Right.Diagnostic));

            context.RegisterSourceOutput(
                inputs,
                (context, source) => Execute(context, source.Nodes, source.Compilation.Options.OutputKind, source.ApplicationConfig, source.ApplicationConfigDiagnostics));
        }
        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            var _attributes = "Method,RegistrationOptions";
            var _interfaces = "IPartialItemsRequest,IPartialItemRequest,IWorkDoneProgressParams,IHandlerIdentity";

            var syntaxProvider = context.SyntaxProvider.CreateSyntaxProvider(
                (syntaxNode, _) =>
            {
                if (syntaxNode is TypeDeclarationSyntax typeDeclarationSyntax and(ClassDeclarationSyntax or RecordDeclarationSyntax) &&
                    (typeDeclarationSyntax.AttributeLists.ContainsAttribute(_attributes) ||
                     typeDeclarationSyntax.BaseList?.Types.Any(type => type.Type.GetSyntaxName() is { } n&& _interfaces.Contains(n)) == true
                    )
                    )
                {
                    return(true);
                }

                return(false);
            }, (syntaxContext, _) => { return(syntaxContext); }
                );


            context.RegisterSourceOutput(syntaxProvider, GenerateAutoImplementedInterfaces);
        }
Esempio n. 13
0
        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            var errorCodeType = context.CompilationProvider.Select((comp, _) =>
                                                                   comp.GetTypeByMetadataName("Loretta.CodeAnalysis.Lua.ErrorCode"));

            var errorCodeFields = errorCodeType.SelectMany((type, _) =>
                                                           type?.GetMembers().OfType <IFieldSymbol>() ?? Enumerable.Empty <IFieldSymbol>())
                                  .Collect();

            context.RegisterSourceOutput(errorCodeFields, (context, codes) =>
            {
                if (codes.IsEmpty)
                {
                    return;
                }
                else
                {
                    using var writer = new SourceWriter();
                    using (writer.CurlyIndenter("namespace Loretta.CodeAnalysis.Lua"))
                        using (writer.CurlyIndenter("internal static partial class ErrorFacts"))
                        {
                            using (writer.CurlyIndenter("public static partial bool IsWarning(ErrorCode code)"))
                                using (writer.CurlyIndenter("switch(code)"))
                                {
                                    var warnings = codes.Where(field => field.Name.StartsWith("WRN_", StringComparison.OrdinalIgnoreCase));
                                    if (warnings.Any())
                                    {
                                        foreach (var code in warnings)
                                        {
                                            writer.WriteLine($"case ErrorCode.{code.Name}:");
                                        }
                                        using (writer.Indenter())
                                            writer.WriteLine("return true;");
                                    }
                                    writer.WriteLine("default:");
                                    using (writer.Indenter())
                                        writer.WriteLine("return false;");
                                }
                            writer.WriteLine();
                            using (writer.CurlyIndenter("public static partial bool IsFatal(ErrorCode code)"))
                                using (writer.CurlyIndenter("switch(code)"))
                                {
                                    var fatals = codes.Where(field => field.Name.StartsWith("FTL_", StringComparison.OrdinalIgnoreCase));
                                    if (fatals.Any())
                                    {
                                        foreach (var code in fatals)
                                        {
                                            writer.WriteLine($"case ErrorCode.{code.Name}:");
                                        }
                                        using (writer.Indenter())
                                            writer.WriteLine("return true;");
                                    }
                                    writer.WriteLine("default:");
                                    using (writer.Indenter())
                                        writer.WriteLine("return false;");
                                }
                            writer.WriteLine();
                            using (writer.CurlyIndenter("public static partial bool IsInfo(ErrorCode code)"))
                                using (writer.CurlyIndenter("switch(code)"))
                                {
                                    var infos = codes.Where(field => field.Name.StartsWith("INF_", StringComparison.OrdinalIgnoreCase));
                                    if (infos.Any())
                                    {
                                        foreach (var code in infos)
                                        {
                                            writer.WriteLine($"case ErrorCode.{code.Name}:");
                                        }
                                        using (writer.Indenter())
                                            writer.WriteLine("return true;");
                                    }
                                    writer.WriteLine("default:");
                                    using (writer.Indenter())
                                        writer.WriteLine("return false;");
                                }
                            writer.WriteLine();
                            using (writer.CurlyIndenter("public static partial bool IsHidden(ErrorCode code)"))
                                using (writer.CurlyIndenter("switch(code)"))
                                {
                                    var hidden = codes.Where(field => field.Name.StartsWith("HDN_", StringComparison.OrdinalIgnoreCase));
                                    if (hidden.Any())
                                    {
                                        foreach (var code in hidden)
                                        {
                                            writer.WriteLine($"case ErrorCode.{code.Name}:");
                                        }
                                        using (writer.Indenter())
                                            writer.WriteLine("return true;");
                                    }
                                    writer.WriteLine("default:");
                                    using (writer.Indenter())
                                        writer.WriteLine("return false;");
                                }
                        }

                    context.AddSource("ErrorFacts.g.cs", writer.GetText());
                }
            });
        }
    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        var xmlFiles = context.AdditionalTextsProvider.Where(f => Path.GetExtension(f.Path) == ".xml").Collect();

        context.RegisterSourceOutput(xmlFiles, GenerateSource);
    }