Ejemplo n.º 1
0
        protected override TDeclarationNode AddProperty <TDeclarationNode>(TDeclarationNode destination, IPropertySymbol property, CodeGenerationOptions options, IList <bool> availableIndices)
        {
            CheckDeclarationNode <TypeDeclarationSyntax, CompilationUnitSyntax>(destination);

            // Can't generate a property with parameters.  So generate the setter/getter individually.
            if (!PropertyGenerator.CanBeGenerated(property))
            {
                var members = new List <ISymbol>();
                if (property.GetMethod != null)
                {
                    var getMethod = property.GetMethod;

                    if (property is CodeGenerationSymbol codeGenSymbol)
                    {
                        foreach (var annotation in codeGenSymbol.GetAnnotations())
                        {
                            getMethod = annotation.AddAnnotationToSymbol(getMethod);
                        }
                    }

                    members.Add(getMethod);
                }

                if (property.SetMethod != null)
                {
                    var setMethod = property.SetMethod;

                    if (property is CodeGenerationSymbol codeGenSymbol)
                    {
                        foreach (var annotation in codeGenSymbol.GetAnnotations())
                        {
                            setMethod = annotation.AddAnnotationToSymbol(setMethod);
                        }
                    }

                    members.Add(setMethod);
                }

                if (members.Count > 1)
                {
                    options = CreateOptionsForMultipleMembers(options);
                }

                return(AddMembers(destination, members, availableIndices, options, CancellationToken.None));
            }

            if (destination is TypeDeclarationSyntax)
            {
                return(Cast <TDeclarationNode>(PropertyGenerator.AddPropertyTo(
                                                   Cast <TypeDeclarationSyntax>(destination), property, Workspace, options, availableIndices)));
            }
            else
            {
                return(Cast <TDeclarationNode>(PropertyGenerator.AddPropertyTo(
                                                   Cast <CompilationUnitSyntax>(destination), property, Workspace, options, availableIndices)));
            }
        }
Ejemplo n.º 2
0
        public override SyntaxNode CreateMethodDeclaration(
            IMethodSymbol method, CodeGenerationDestination destination, CodeGenerationOptions options)
        {
            // Synthesized methods for properties/events are not things we actually generate
            // declarations for.
            if (method.AssociatedSymbol is IEventSymbol)
            {
                return(null);
            }

            if (method.AssociatedSymbol is IPropertySymbol)
            {
                // we will ignore the method if the associated property can be generated.
                var property = (IPropertySymbol)method.AssociatedSymbol;
                if (PropertyGenerator.CanBeGenerated(property))
                {
                    return(null);
                }
            }

            if (method.IsConstructor())
            {
                return(ConstructorGenerator.GenerateConstructorDeclaration(
                           method, destination, Workspace, options, options.ParseOptions));
            }
            else if (method.IsDestructor())
            {
                return(DestructorGenerator.GenerateDestructorDeclaration(method, destination, options));
            }
            else if (method.IsUserDefinedOperator())
            {
                return(OperatorGenerator.GenerateOperatorDeclaration(
                           method, destination, Workspace, options, options.ParseOptions));
            }
            else if (method.IsConversion())
            {
                return(ConversionGenerator.GenerateConversionDeclaration(
                           method, destination, Workspace, options, options.ParseOptions));
            }
            else
            {
                return(MethodGenerator.GenerateMethodDeclaration(
                           method, destination, Workspace, options, options.ParseOptions));
            }
        }
Ejemplo n.º 3
0
        protected override TDeclarationNode AddMethod <TDeclarationNode>(TDeclarationNode destination, IMethodSymbol method, CodeGenerationOptions options, IList <bool> availableIndices)
        {
            CheckDeclarationNode <TypeDeclarationSyntax, CompilationUnitSyntax, NamespaceDeclarationSyntax>(destination);

            // Synthesized methods for properties/events are not things we actually generate
            // declarations for.
            if (method.AssociatedSymbol is IEventSymbol)
            {
                return(destination);
            }
            // we will ignore the method if the associated property can be generated.

            if (method.AssociatedSymbol is IPropertySymbol property)
            {
                if (PropertyGenerator.CanBeGenerated(property))
                {
                    return(destination);
                }
            }

            if (destination is TypeDeclarationSyntax typeDeclaration)
            {
                if (method.IsConstructor())
                {
                    return(Cast <TDeclarationNode>(ConstructorGenerator.AddConstructorTo(
                                                       typeDeclaration, method, Workspace, options, availableIndices)));
                }

                if (method.IsDestructor())
                {
                    return(Cast <TDeclarationNode>(DestructorGenerator.AddDestructorTo(typeDeclaration, method, options, availableIndices)));
                }

                if (method.MethodKind == MethodKind.Conversion)
                {
                    return(Cast <TDeclarationNode>(ConversionGenerator.AddConversionTo(
                                                       typeDeclaration, method, Workspace, options, availableIndices)));
                }

                if (method.MethodKind == MethodKind.UserDefinedOperator)
                {
                    return(Cast <TDeclarationNode>(OperatorGenerator.AddOperatorTo(
                                                       typeDeclaration, method, Workspace, options, availableIndices)));
                }

                return(Cast <TDeclarationNode>(MethodGenerator.AddMethodTo(
                                                   typeDeclaration, method, Workspace, options, availableIndices)));
            }

            if (method.IsConstructor() ||
                method.IsDestructor())
            {
                return(destination);
            }

            if (destination is CompilationUnitSyntax compilationUnit)
            {
                return(Cast <TDeclarationNode>(
                           MethodGenerator.AddMethodTo(compilationUnit, method, Workspace, options, availableIndices)));
            }

            var ns = Cast <NamespaceDeclarationSyntax>(destination);

            return(Cast <TDeclarationNode>(
                       MethodGenerator.AddMethodTo(ns, method, Workspace, options, availableIndices)));
        }
Ejemplo n.º 4
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)));
        }
Ejemplo n.º 5
0
        protected override TDeclarationNode AddMethod <TDeclarationNode>(TDeclarationNode destination, IMethodSymbol method, CodeGenerationOptions options, IList <bool> availableIndices)
        {
            // https://github.com/dotnet/roslyn/issues/44425: Add handling for top level statements
            if (destination is GlobalStatementSyntax)
            {
                return(destination);
            }

            CheckDeclarationNode <TypeDeclarationSyntax, CompilationUnitSyntax, BaseNamespaceDeclarationSyntax>(destination);

            options = options.With(options: options.Options ?? Workspace.Options);

            // Synthesized methods for properties/events are not things we actually generate
            // declarations for.
            if (method.AssociatedSymbol is IEventSymbol)
            {
                return(destination);
            }
            // we will ignore the method if the associated property can be generated.

            if (method.AssociatedSymbol is IPropertySymbol property)
            {
                if (PropertyGenerator.CanBeGenerated(property))
                {
                    return(destination);
                }
            }

            if (destination is TypeDeclarationSyntax typeDeclaration)
            {
                if (method.IsConstructor())
                {
                    return(Cast <TDeclarationNode>(ConstructorGenerator.AddConstructorTo(
                                                       typeDeclaration, method, options, availableIndices)));
                }

                if (method.IsDestructor())
                {
                    return(Cast <TDeclarationNode>(DestructorGenerator.AddDestructorTo(typeDeclaration, method, options, availableIndices)));
                }

                if (method.MethodKind == MethodKind.Conversion)
                {
                    return(Cast <TDeclarationNode>(ConversionGenerator.AddConversionTo(
                                                       typeDeclaration, method, options, availableIndices)));
                }

                if (method.MethodKind == MethodKind.UserDefinedOperator)
                {
                    return(Cast <TDeclarationNode>(OperatorGenerator.AddOperatorTo(
                                                       typeDeclaration, method, options, availableIndices)));
                }

                return(Cast <TDeclarationNode>(MethodGenerator.AddMethodTo(
                                                   typeDeclaration, method, options, availableIndices)));
            }

            if (method.IsConstructor() ||
                method.IsDestructor())
            {
                return(destination);
            }

            if (destination is CompilationUnitSyntax compilationUnit)
            {
                return(Cast <TDeclarationNode>(
                           MethodGenerator.AddMethodTo(compilationUnit, method, options, availableIndices)));
            }

            var ns = Cast <BaseNamespaceDeclarationSyntax>(destination);

            return(Cast <TDeclarationNode>(
                       MethodGenerator.AddMethodTo(ns, method, options, availableIndices)));
        }
Ejemplo n.º 6
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)));
        }
Ejemplo n.º 7
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)));
        }