Exemple #1
0
        private static async Task <Document> UsePredefinedTypeAsync(
            Document document,
            SyntaxNode node,
            ITypeSymbol typeSymbol,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            TypeSyntax newType = TypeSyntaxRefactoring.CreateTypeSyntax(typeSymbol)
                                 .WithTriviaFrom(node)
                                 .WithAdditionalAnnotations(Formatter.Annotation);

            SyntaxNode newRoot = oldRoot.ReplaceNode(node, newType);

            return(document.WithSyntaxRoot(newRoot));
        }
        private static async Task <Document> ChangeDeclaredTypeToExplicitAsync(
            Document document,
            TypeSyntax type,
            ITypeSymbol typeSymbol,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            TypeSyntax newType = TypeSyntaxRefactoring.CreateTypeSyntax(typeSymbol)
                                 .WithTriviaFrom(type)
                                 .WithAdditionalAnnotations(Simplifier.Annotation);

            SyntaxNode newRoot = oldRoot.ReplaceNode(type, newType);

            return(document.WithSyntaxRoot(newRoot));
        }
Exemple #3
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            ImplicitArrayCreationExpressionSyntax node = root
                                                         .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                         .FirstAncestorOrSelf <ImplicitArrayCreationExpressionSyntax>();

            if (node == null)
            {
                return;
            }

            SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken);

            if (semanticModel == null)
            {
                return;
            }

            ITypeSymbol typeSymbol = semanticModel.GetTypeInfo(node, context.CancellationToken).Type;

            if (typeSymbol == null)
            {
                return;
            }

            var arrayType = TypeSyntaxRefactoring.CreateTypeSyntax(typeSymbol) as ArrayTypeSyntax;

            if (arrayType == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                $"Declare explicit type '{typeSymbol.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}'",
                cancellationToken => SpecifyExplicitTypeAsync(context.Document, node, arrayType, cancellationToken),
                DiagnosticIdentifiers.AvoidImplicitArrayCreation + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
Exemple #4
0
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);

            YieldStatementSyntax yieldStatement = root
                                                  .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                  .FirstAncestorOrSelf <YieldStatementSyntax>();

            if (yieldStatement == null)
            {
                return;
            }

            if (yieldStatement.IsYieldReturn() &&
                yieldStatement.Expression?.Span.Contains(context.Span) == true &&
                context.Document.SupportsSemanticModel)
            {
                MemberDeclarationSyntax declaration = GetDeclaration(yieldStatement);

                if (declaration != null)
                {
                    TypeSyntax memberType = GetMemberType(declaration);

                    if (memberType != null)
                    {
                        SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken);

                        ITypeSymbol memberTypeSymbol = semanticModel
                                                       .GetTypeInfo(memberType, context.CancellationToken)
                                                       .Type;

                        if (memberTypeSymbol.SpecialType != SpecialType.System_Collections_IEnumerable)
                        {
                            ITypeSymbol typeSymbol = semanticModel
                                                     .GetTypeInfo(yieldStatement.Expression, context.CancellationToken)
                                                     .Type;

                            if (typeSymbol?.IsKind(SymbolKind.ErrorType) == false &&
                                (memberTypeSymbol == null ||
                                 memberTypeSymbol.IsKind(SymbolKind.ErrorType) ||
                                 !memberTypeSymbol.IsGenericIEnumerable() ||
                                 !((INamedTypeSymbol)memberTypeSymbol).TypeArguments[0].Equals(typeSymbol)))
                            {
                                TypeSyntax newType = QualifiedName(
                                    ParseName("System.Collections.Generic"),
                                    GenericName(
                                        Identifier("IEnumerable"),
                                        TypeArgumentList(
                                            SingletonSeparatedList(
                                                TypeSyntaxRefactoring.CreateTypeSyntax(typeSymbol)))));

                                context.RegisterRefactoring(
                                    $"Change {GetText(declaration)} type to 'IEnumerable<{typeSymbol.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}>'",
                                    cancellationToken =>
                                {
                                    return(ChangeReturnTypeAsync(
                                               context.Document,
                                               memberType,
                                               newType,
                                               cancellationToken));
                                });
                            }
                        }
                    }
                }
            }
        }
Exemple #5
0
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);

            ReturnStatementSyntax returnStatement = root
                                                    .FindNode(context.Span, getInnermostNodeForTie: true)?
                                                    .FirstAncestorOrSelf <ReturnStatementSyntax>();

            if (returnStatement == null)
            {
                return;
            }

            if (returnStatement.Expression != null &&
                returnStatement.Expression.Span.Contains(context.Span) &&
                context.Document.SupportsSemanticModel)
            {
                MemberDeclarationSyntax declaration = GetDeclaration(returnStatement);

                if (declaration != null)
                {
                    TypeSyntax memberType = GetMemberType(declaration);

                    if (memberType != null)
                    {
                        SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken);

                        ITypeSymbol memberTypeSymbol = semanticModel
                                                       .GetTypeInfo(memberType, context.CancellationToken)
                                                       .Type;

                        if (memberTypeSymbol?.IsKind(SymbolKind.ErrorType) == false)
                        {
                            ITypeSymbol typeSymbol = semanticModel
                                                     .GetTypeInfo(returnStatement.Expression, context.CancellationToken)
                                                     .Type;

                            if (typeSymbol?.IsKind(SymbolKind.ErrorType) == false)
                            {
                                if (memberTypeSymbol.SpecialType == SpecialType.System_Boolean &&
                                    typeSymbol.IsKind(SymbolKind.NamedType))
                                {
                                    var namedTypeSymbol = (INamedTypeSymbol)typeSymbol;

                                    if (namedTypeSymbol?.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T &&
                                        namedTypeSymbol.TypeArguments[0].SpecialType == SpecialType.System_Boolean)
                                    {
                                        CodeAction codeAction = CodeAction.Create(
                                            AddBooleanComparisonRefactoring.Title,
                                            cancellationToken =>
                                        {
                                            return(AddBooleanComparisonRefactoring.RefactorAsync(
                                                       context.Document,
                                                       returnStatement.Expression,
                                                       context.CancellationToken));
                                        });

                                        context.RegisterRefactoring(codeAction);
                                    }
                                }

                                if (!memberTypeSymbol.Equals(typeSymbol))
                                {
                                    TypeSyntax newType = TypeSyntaxRefactoring.CreateTypeSyntax(typeSymbol);

                                    if (newType != null)
                                    {
                                        CodeAction codeAction = CodeAction.Create(
                                            $"Change {GetText(declaration)} type to '{typeSymbol.ToDisplayString(TypeSyntaxRefactoring.SymbolDisplayFormat)}'",
                                            cancellationToken =>
                                        {
                                            return(ChangeReturnTypeAsync(
                                                       context.Document,
                                                       memberType,
                                                       newType,
                                                       cancellationToken));
                                        });

                                        context.RegisterRefactoring(codeAction);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }