Exemplo n.º 1
0
        internal static SyntaxNode GenerateNamespaceDeclaration(
            ICodeGenerationService service,
            INamespaceSymbol @namespace,
            CodeGenerationOptions options,
            CancellationToken cancellationToken
            )
        {
            options ??= CodeGenerationOptions.Default;
            GetNameAndInnermostNamespace(
                @namespace,
                options,
                out var name,
                out var innermostNamespace
                );

            var declaration = GetDeclarationSyntaxWithoutMembers(
                @namespace,
                innermostNamespace,
                name,
                options
                );

            declaration = options.GenerateMembers
                ? service.AddMembers(
                declaration,
                innermostNamespace.GetMembers(),
                options,
                cancellationToken
                )
                : declaration;

            return(AddFormatterAndCodeGeneratorAnnotationsTo(declaration));
        }
Exemplo n.º 2
0
        public static MemberDeclarationSyntax GenerateNamedTypeDeclaration(
            ICodeGenerationService service,
            INamedTypeSymbol namedType,
            CodeGenerationDestination destination,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            options ??= CodeGenerationOptions.Default;

            var declaration = GetDeclarationSyntaxWithoutMembers(namedType, destination, options);

            if (namedType.IsComImport)
            {
                // If we're generating a ComImport type, then do not attempt to do any
                // reordering of members.
                options = options.With(autoInsertionLocation: false, sortMembers: false);
            }

            // If we are generating members then make sure to exclude properties that cannot be generated.
            // Reason: Calling AddProperty on a propertysymbol that can't be generated (like one with params) causes
            // the getter and setter to get generated instead. Since the list of members is going to include
            // the method symbols for the getter and setter, we don't want to generate them twice.
            declaration = options.GenerateMembers && namedType.TypeKind != TypeKind.Delegate
                ? service.AddMembers(declaration,
                                     GetMembers(namedType).Where(s => s.Kind != SymbolKind.Property || PropertyGenerator.CanBeGenerated((IPropertySymbol)s)),
                                     options,
                                     cancellationToken)
                : declaration;

            return(AddFormatterAndCodeGeneratorAnnotationsTo(ConditionallyAddDocumentationCommentTo(declaration, namedType, options, cancellationToken)));
        }
Exemplo n.º 3
0
        public static MemberDeclarationSyntax GenerateNamedTypeDeclaration(
            ICodeGenerationService service,
            INamedTypeSymbol namedType,
            CodeGenerationDestination destination,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            options = options ?? CodeGenerationOptions.Default;

            var declaration = GetDeclarationSyntaxWithoutMembers(namedType, destination, options);

            if (namedType.IsComImport)
            {
                // If we're generating a ComImport type, then do not attempt to do any
                // reordering of members.
                options = options.With(autoInsertionLocation: false, sortMembers: false);
            }

            // If we are generating members then make sure to exclude properties that cannot be generated.
            // Reason: Calling AddProperty on a propertysymbol that can't be generated (like one with params) causes
            // the getter and setter to get generated instead. Since the list of members is going to include
            // the method symbols for the getter and setter, we don't want to generate them twice.
            declaration = options.GenerateMembers && namedType.TypeKind != TypeKind.Delegate
                ? service.AddMembers(declaration,
                                     GetMembers(namedType).Where(s => s.Kind != SymbolKind.Property || PropertyGenerator.CanBeGenerated((IPropertySymbol)s)),
                                     options,
                                     cancellationToken)
                : declaration;

            return AddCleanupAnnotationsTo(ConditionallyAddDocumentationCommentTo(declaration, namedType, options));
        }
Exemplo n.º 4
0
 public static MemberDeclarationSyntax UpdateNamedTypeDeclaration(
     ICodeGenerationService service,
     MemberDeclarationSyntax declaration,
     IList <ISymbol> newMembers,
     CodeGenerationOptions options,
     CancellationToken cancellationToken)
 {
     declaration = RemoveAllMembers(declaration);
     declaration = service.AddMembers(declaration, newMembers, options, cancellationToken);
     return(AddFormatterAndCodeGeneratorAnnotationsTo(declaration));
 }
Exemplo n.º 5
0
 public static SyntaxNode UpdateCompilationUnitOrNamespaceDeclaration(
     ICodeGenerationService service,
     SyntaxNode declaration,
     IList<ISymbol> newMembers,
     CodeGenerationOptions options,
     CancellationToken cancellationToken)
 {
     declaration = RemoveAllMembers(declaration);
     declaration = service.AddMembers(declaration, newMembers, options, cancellationToken);
     return AddCleanupAnnotationsTo(declaration);
 }
Exemplo n.º 6
0
 public static SyntaxNode UpdateCompilationUnitOrNamespaceDeclaration(
     ICodeGenerationService service,
     SyntaxNode declaration,
     IList <ISymbol> newMembers,
     CodeGenerationOptions options,
     CancellationToken cancellationToken)
 {
     declaration = RemoveAllMembers(declaration);
     declaration = service.AddMembers(declaration, newMembers, options, cancellationToken);
     return(AddCleanupAnnotationsTo(declaration));
 }
Exemplo n.º 7
0
 public static SyntaxNode UpdateCompilationUnitOrNamespaceDeclaration(
     ICodeGenerationService service,
     SyntaxNode declaration,
     IList <ISymbol> newMembers,
     CSharpCodeGenerationContextInfo info,
     CancellationToken cancellationToken)
 {
     declaration = RemoveAllMembers(declaration);
     declaration = service.AddMembers(declaration, newMembers, info, cancellationToken);
     return(AddFormatterAndCodeGeneratorAnnotationsTo(declaration));
 }
 public static MemberDeclarationSyntax UpdateNamedTypeDeclaration(
     ICodeGenerationService service,
     MemberDeclarationSyntax declaration,
     IList<ISymbol> newMembers,
     CodeGenerationOptions options,
     CancellationToken cancellationToken)
 {
     declaration = RemoveAllMembers(declaration);
     declaration = service.AddMembers(declaration, newMembers, options, cancellationToken);
     return AddCleanupAnnotationsTo(declaration);
 }
Exemplo n.º 9
0
        internal static SyntaxNode GenerateNamespaceDeclaration(
            ICodeGenerationService service,
            INamespaceSymbol @namespace,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            options = options ?? CodeGenerationOptions.Default;
            GetNameAndInnermostNamespace(@namespace, options, out var name, out var innermostNamespace);

            var declaration = GetDeclarationSyntaxWithoutMembers(@namespace, innermostNamespace, name, options);

            declaration = options.GenerateMembers
                    ? service.AddMembers(declaration, innermostNamespace.GetMembers(), options, cancellationToken)
                    : declaration;

            return AddCleanupAnnotationsTo(declaration);
        }
Exemplo n.º 10
0
        internal static SyntaxNode GenerateNamespaceDeclaration(
            ICodeGenerationService service,
            INamespaceSymbol @namespace,
            CodeGenerationDestination destination,
            CSharpCodeGenerationContextInfo info,
            CancellationToken cancellationToken)
        {
            GetNameAndInnermostNamespace(@namespace, info, out var name, out var innermostNamespace);

            var declaration = GetDeclarationSyntaxWithoutMembers(
                @namespace, innermostNamespace, name, destination, info);

            declaration = info.Context.GenerateMembers
                ? service.AddMembers(declaration, innermostNamespace.GetMembers(), info, cancellationToken)
                : declaration;

            return(AddFormatterAndCodeGeneratorAnnotationsTo(declaration));
        }
Exemplo n.º 11
0
        private static RecordDeclarationSyntax GenerateRecordMembers(
            ICodeGenerationService service,
            CodeGenerationOptions options,
            RecordDeclarationSyntax recordDeclaration,
            ImmutableArray <ISymbol> members,
            CancellationToken cancellationToken)
        {
            // For a record, add record parameters if we have a primary constructor.
            var primaryConstructor = members.OfType <IMethodSymbol>().FirstOrDefault(m => CodeGenerationConstructorInfo.GetIsPrimaryConstructor(m));

            if (primaryConstructor != null)
            {
                var parameterList = ParameterGenerator.GenerateParameterList(primaryConstructor.Parameters, isExplicit: false, options);
                recordDeclaration = recordDeclaration.WithParameterList(parameterList);

                // remove the primary constructor from the list of members to generate.
                members = members.Remove(primaryConstructor);

                // remove any properties that were created by the primary constructor
                members = members.WhereAsArray(m => m is not IPropertySymbol property || !primaryConstructor.Parameters.Any(p => p.Name == property.Name));
            }

            // remove any implicit overrides to generate.
            members = members.WhereAsArray(m => !m.IsImplicitlyDeclared);

            // If there are no members, just make a simple record with no body
            if (members.Length == 0)
            {
                recordDeclaration = recordDeclaration.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
            }
            else
            {
                // Otherwise, give the record a body.
                recordDeclaration = recordDeclaration.WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken))
                                    .WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken));
            }

            if (options.GenerateMembers)
            {
                recordDeclaration = service.AddMembers(recordDeclaration, members, options, cancellationToken);
            }

            return(recordDeclaration);
        }
Exemplo n.º 12
0
        internal static SyntaxNode GenerateNamespaceDeclaration(
            ICodeGenerationService service,
            INamespaceSymbol @namespace,
            CodeGenerationOptions options)
        {
            options = options ?? CodeGenerationOptions.Default;

            string           name;
            INamespaceSymbol innermostNamespace;

            GetNameAndInnermostNamespace(@namespace, options, out name, out innermostNamespace);

            var declaration = GetDeclarationSyntaxWithoutMembers(@namespace, innermostNamespace, name, options);

            declaration = options.GenerateMembers
                    ? service.AddMembers(declaration, innermostNamespace.GetMembers(), options)
                    : declaration;

            return(AddCleanupAnnotationsTo(declaration));
        }
Exemplo n.º 13
0
        public static MemberDeclarationSyntax GenerateNamedTypeDeclaration(
            ICodeGenerationService service,
            INamedTypeSymbol namedType,
            CodeGenerationDestination destination,
            CodeGenerationOptions options)
        {
            options = options ?? CodeGenerationOptions.Default;

            var declaration = GetDeclarationSyntaxWithoutMembers(namedType, destination, options);

            // If we are generating members then make sure to exclude properties that cannot be generated.
            // Reason: Calling AddProperty on a propertysymbol that can't be generated (like one with params) causes
            // the getter and setter to get generated instead. Since the list of members is going to include
            // the method symbols for the getter and setter, we don't want to generate them twice.
            declaration = options.GenerateMembers && namedType.TypeKind != TypeKind.Delegate
                ? service.AddMembers(declaration,
                                     GetMembers(namedType).Where(s => s.Kind != SymbolKind.Property || PropertyGenerator.CanBeGenerated((IPropertySymbol)s)),
                                     options)
                : declaration;

            return(AddCleanupAnnotationsTo(ConditionallyAddDocumentationCommentTo(declaration, namedType, options)));
        }
Exemplo n.º 14
0
        public static MemberDeclarationSyntax GenerateNamedTypeDeclaration(
            ICodeGenerationService service,
            INamedTypeSymbol namedType,
            CodeGenerationDestination destination,
            CSharpCodeGenerationContextInfo info,
            CancellationToken cancellationToken)
        {
            var declaration = GetDeclarationSyntaxWithoutMembers(namedType, destination, info);

            // If we are generating members then make sure to exclude properties that cannot be generated.
            // Reason: Calling AddProperty on a propertysymbol that can't be generated (like one with params) causes
            // the getter and setter to get generated instead. Since the list of members is going to include
            // the method symbols for the getter and setter, we don't want to generate them twice.

            var members = GetMembers(namedType).Where(s => s.Kind != SymbolKind.Property || PropertyGenerator.CanBeGenerated((IPropertySymbol)s))
                          .ToImmutableArray();

            if (namedType.IsRecord)
            {
                declaration = GenerateRecordMembers(service, info, (RecordDeclarationSyntax)declaration, members, cancellationToken);
            }
            else
            {
                // If we're generating a ComImport type, then do not attempt to do any
                // reordering of members.
                if (namedType.IsComImport)
                {
                    info = info.WithContext(info.Context.With(autoInsertionLocation: false, sortMembers: false));
                }

                if (info.Context.GenerateMembers && namedType.TypeKind != TypeKind.Delegate)
                {
                    declaration = service.AddMembers(declaration, members, info, cancellationToken);
                }
            }

            return(AddFormatterAndCodeGeneratorAnnotationsTo(ConditionallyAddDocumentationCommentTo(declaration, namedType, info, cancellationToken)));
        }