Beispiel #1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CallToMethod) &&
                yieldStatement.Kind() == SyntaxKind.YieldReturnStatement)
            {
                ExpressionSyntax expression = yieldStatement.Expression;

                if (expression?.IsMissing == false &&
                    expression.Span.Contains(context.Span))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    (ISymbol memberSymbol, ITypeSymbol memberTypeSymbol) = ReturnExpressionRefactoring.GetContainingSymbolAndType(expression, semanticModel, context.CancellationToken);

                    if (memberSymbol != null &&
                        (memberTypeSymbol is INamedTypeSymbol namedTypeSymbol) &&
                        namedTypeSymbol.SpecialType != SpecialType.System_Collections_IEnumerable &&
                        namedTypeSymbol.OriginalDefinition.IsIEnumerableOfT())
                    {
                        ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0];

                        ITypeSymbol expressionTypeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken);

                        if (argumentSymbol != expressionTypeSymbol)
                        {
                            ModifyExpressionRefactoring.ComputeRefactoring(
                                context,
                                expression,
                                argumentSymbol,
                                semanticModel,
                                addCastExpression: false);
                        }
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ConvertReturnToIf) &&
                (context.Span.IsEmptyAndContainedInSpan(yieldStatement.YieldKeyword) ||
                 context.Span.IsEmptyAndContainedInSpan(yieldStatement.ReturnOrBreakKeyword) ||
                 context.Span.IsBetweenSpans(yieldStatement)))
            {
                await ConvertReturnToIfRefactoring.ConvertYieldReturnToIfElse.ComputeRefactoringAsync(context, yieldStatement).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseListInsteadOfYield) &&
                yieldStatement.IsYieldReturn() &&
                context.Span.IsEmptyAndContainedInSpan(yieldStatement.YieldKeyword))
            {
                SyntaxNode declaration = yieldStatement.FirstAncestor(SyntaxKind.MethodDeclaration, SyntaxKind.LocalFunctionStatement, SyntaxKind.GetAccessorDeclaration, ascendOutOfTrivia: false);

                Debug.Assert(declaration != null);

                if (declaration != null)
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    UseListInsteadOfYieldRefactoring.ComputeRefactoring(context, declaration, semanticModel);
                }
            }
        }
Beispiel #2
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertReturnStatementToIf) &&
                (context.Span.IsEmptyAndContainedInSpan(yieldStatement.YieldKeyword) ||
                 context.Span.IsEmptyAndContainedInSpan(yieldStatement.ReturnOrBreakKeyword) ||
                 context.Span.IsBetweenSpans(yieldStatement)))
            {
                await ConvertReturnStatementToIfRefactoring.ConvertYieldReturnToIfElse.ComputeRefactoringAsync(context, yieldStatement).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.UseListInsteadOfYield) &&
                yieldStatement.IsYieldReturn() &&
                context.Span.IsEmptyAndContainedInSpan(yieldStatement.YieldKeyword))
            {
                SyntaxNode declaration = yieldStatement.FirstAncestor(SyntaxKind.MethodDeclaration, SyntaxKind.LocalFunctionStatement, SyntaxKind.GetAccessorDeclaration, ascendOutOfTrivia: false);

                Debug.Assert(declaration != null);

                if (declaration != null)
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    UseListInsteadOfYieldRefactoring.ComputeRefactoring(context, declaration, semanticModel);
                }
            }
        }
        public static bool CanRefactor(YieldStatementSyntax yieldStatement, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            if (yieldStatement.IsYieldReturn())
            {
                ExpressionSyntax expression = yieldStatement.Expression;

                return(expression?.IsMissing == false &&
                       semanticModel
                       .GetTypeSymbol(expression, cancellationToken)?
                       .IsVoid() == true);
            }

            return(false);
        }
Beispiel #4
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CallToMethod) &&
                yieldStatement.IsYieldReturn())
            {
                ExpressionSyntax expression = yieldStatement.Expression;

                if (expression?.Span.Contains(context.Span) == true)
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    (ISymbol memberSymbol, ITypeSymbol memberTypeSymbol) = ReturnExpressionRefactoring.GetContainingSymbolAndType(expression, semanticModel, context.CancellationToken);

                    if (memberSymbol != null &&
                        (memberTypeSymbol is INamedTypeSymbol namedTypeSymbol) &&
                        namedTypeSymbol.SpecialType != SpecialType.System_Collections_IEnumerable &&
                        namedTypeSymbol.IsConstructedFromIEnumerableOfT())
                    {
                        ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0];

                        ITypeSymbol expressionTypeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken);

                        if (argumentSymbol != expressionTypeSymbol)
                        {
                            ModifyExpressionRefactoring.ComputeRefactoring(
                                context,
                                expression,
                                argumentSymbol,
                                semanticModel,
                                addCastExpression: false);
                        }
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStatementWithIfElse) &&
                (context.Span.IsEmptyAndContainedInSpan(yieldStatement.YieldKeyword) ||
                 context.Span.IsEmptyAndContainedInSpan(yieldStatement.ReturnOrBreakKeyword) ||
                 context.Span.IsBetweenSpans(yieldStatement)))
            {
                await ReplaceStatementWithIfStatementRefactoring.ReplaceYieldReturnWithIfElse.ComputeRefactoringAsync(context, yieldStatement).ConfigureAwait(false);
            }
        }
Beispiel #5
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression,
                    RefactoringIdentifiers.AddCastExpression,
                    RefactoringIdentifiers.CallToMethod)
                && yieldStatement.IsYieldReturn()
                && yieldStatement.Expression != null)
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                ISymbol memberSymbol = ReturnExpressionRefactoring.GetContainingMethodOrPropertySymbol(yieldStatement.Expression, semanticModel, context.CancellationToken);

                if (memberSymbol != null)
                {
                    SyntaxNode node = await memberSymbol
                        .DeclaringSyntaxReferences[0]
                        .GetSyntaxAsync(context.CancellationToken)
                        .ConfigureAwait(false);

                    TypeSyntax type = ReturnExpressionRefactoring.GetTypeOrReturnType(node);

                    if (type != null)
                    {
                        ITypeSymbol memberTypeSymbol = semanticModel.GetTypeSymbol(type, context.CancellationToken);

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

                            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression)
                                && typeSymbol?.IsErrorType() == false
                                && !typeSymbol.IsVoid()
                                && !memberSymbol.IsOverride
                                && (memberTypeSymbol == null
                                    || memberTypeSymbol.IsErrorType()
                                    || !memberTypeSymbol.IsConstructedFromIEnumerableOfT()
                                    || !((INamedTypeSymbol)memberTypeSymbol).TypeArguments[0].Equals(typeSymbol)))
                            {
                                INamedTypeSymbol newTypeSymbol = semanticModel
                                    .Compilation
                                    .GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T)
                                    .Construct(typeSymbol);

                                TypeSyntax newType = newTypeSymbol.ToMinimalTypeSyntax(semanticModel, type.SpanStart);

                                context.RegisterRefactoring(
                                    $"Change {ReturnExpressionRefactoring.GetText(node)} type to '{SymbolDisplay.GetMinimalString(newTypeSymbol, semanticModel, type.SpanStart)}'",
                                    cancellationToken =>
                                    {
                                        return ChangeTypeRefactoring.ChangeTypeAsync(
                                            context.Document,
                                            type,
                                            newType,
                                            cancellationToken);
                                    });
                            }

                            if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod)
                                && yieldStatement.Expression.Span.Contains(context.Span)
                                && memberTypeSymbol?.IsNamedType() == true)
                            {
                                var namedTypeSymbol = (INamedTypeSymbol)memberTypeSymbol;

                                if (namedTypeSymbol.IsConstructedFromIEnumerableOfT())
                                {
                                    ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0];

                                    if (argumentSymbol != typeSymbol)
                                    {
                                        ModifyExpressionRefactoring.ComputeRefactoring(
                                           context,
                                           yieldStatement.Expression,
                                           argumentSymbol,
                                           semanticModel);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStatementWithIfElse)
                && (context.Span.IsEmptyAndContainedInSpan(yieldStatement.YieldKeyword)
                    || context.Span.IsEmptyAndContainedInSpan(yieldStatement.ReturnOrBreakKeyword)
                    || context.Span.IsBetweenSpans(yieldStatement)))
            {
                await ReplaceStatementWithIfStatementRefactoring.ReplaceYieldReturnWithIfElse.ComputeRefactoringAsync(context, yieldStatement).ConfigureAwait(false);
            }
        }
Beispiel #6
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));
                                });
                            }
                        }
                    }
                }
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression,
                    RefactoringIdentifiers.AddCastExpression,
                    RefactoringIdentifiers.CallToMethod,
                    RefactoringIdentifiers.CreateConditionFromBooleanExpression) &&
                yieldStatement.IsYieldReturn() &&
                yieldStatement.Expression != null &&
                context.SupportsSemanticModel)
            {
                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression,
                        RefactoringIdentifiers.AddCastExpression,
                        RefactoringIdentifiers.CallToMethod))
                {
                    MemberDeclarationSyntax containingMember = ReturnExpressionRefactoring.GetContainingMethodOrPropertyOrIndexer(yieldStatement.Expression);

                    if (containingMember != null)
                    {
                        TypeSyntax memberType = ReturnExpressionRefactoring.GetMemberType(containingMember);

                        if (memberType != null)
                        {
                            SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                            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 (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression) &&
                                    typeSymbol?.IsErrorType() == false &&
                                    (memberTypeSymbol == null ||
                                     memberTypeSymbol.IsErrorType() ||
                                     !SyntaxAnalyzer.IsGenericIEnumerable(memberTypeSymbol) ||
                                     !((INamedTypeSymbol)memberTypeSymbol).TypeArguments[0].Equals(typeSymbol)))
                                {
                                    TypeSyntax newType = QualifiedName(
                                        ParseName("System.Collections.Generic"),
                                        GenericName(
                                            Identifier("IEnumerable"),
                                            TypeArgumentList(
                                                SingletonSeparatedList(
                                                    CSharpFactory.Type(typeSymbol)))));

                                    context.RegisterRefactoring(
                                        $"Change {ReturnExpressionRefactoring.GetText(containingMember)} type to 'IEnumerable<{typeSymbol.ToDisplayString(SyntaxUtility.DefaultSymbolDisplayFormat)}>'",
                                        cancellationToken =>
                                    {
                                        return(ChangeTypeRefactoring.ChangeTypeAsync(
                                                   context.Document,
                                                   memberType,
                                                   newType,
                                                   cancellationToken));
                                    });
                                }

                                if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) &&
                                    yieldStatement.Expression.Span.Contains(context.Span) &&
                                    memberTypeSymbol?.IsNamedType() == true)
                                {
                                    var namedTypeSymbol = (INamedTypeSymbol)memberTypeSymbol;

                                    if (namedTypeSymbol.ConstructedFrom.SpecialType == SpecialType.System_Collections_Generic_IEnumerable_T)
                                    {
                                        ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0];

                                        if (argumentSymbol != typeSymbol)
                                        {
                                            ModifyExpressionRefactoring.ComputeRefactoring(
                                                context,
                                                yieldStatement.Expression,
                                                argumentSymbol,
                                                semanticModel);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.CreateConditionFromBooleanExpression))
                {
                    await CreateConditionFromBooleanExpressionRefactoring.ComputeRefactoringAsync(context, yieldStatement.Expression).ConfigureAwait(false);
                }
            }
        }
Beispiel #8
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, YieldStatementSyntax yieldStatement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression,
                    RefactoringIdentifiers.AddCastExpression,
                    RefactoringIdentifiers.CallToMethod) &&
                yieldStatement.IsYieldReturn() &&
                yieldStatement.Expression != null)
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                MemberDeclarationSyntax containingMember = await ReturnExpressionRefactoring.GetContainingMethodOrPropertyOrIndexerAsync(yieldStatement.Expression, semanticModel, context.CancellationToken).ConfigureAwait(false);

                if (containingMember != null)
                {
                    TypeSyntax memberType = ReturnExpressionRefactoring.GetMemberType(containingMember);

                    if (memberType != null)
                    {
                        ITypeSymbol memberTypeSymbol = semanticModel.GetTypeSymbol(memberType, context.CancellationToken);

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

                            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMemberTypeAccordingToYieldReturnExpression) &&
                                typeSymbol?.IsErrorType() == false &&
                                !typeSymbol.IsVoid() &&
                                (memberTypeSymbol == null ||
                                 memberTypeSymbol.IsErrorType() ||
                                 !memberTypeSymbol.IsConstructedFromIEnumerableOfT() ||
                                 !((INamedTypeSymbol)memberTypeSymbol).TypeArguments[0].Equals(typeSymbol)))
                            {
                                INamedTypeSymbol newTypeSymbol = semanticModel
                                                                 .Compilation
                                                                 .GetSpecialType(SpecialType.System_Collections_Generic_IEnumerable_T)
                                                                 .Construct(typeSymbol);

                                TypeSyntax newType = CSharpFactory.Type(newTypeSymbol, semanticModel, memberType.SpanStart);

                                context.RegisterRefactoring(
                                    $"Change {ReturnExpressionRefactoring.GetText(containingMember)} type to '{SymbolDisplay.GetMinimalDisplayString(newTypeSymbol, memberType.SpanStart, semanticModel)}'",
                                    cancellationToken =>
                                {
                                    return(ChangeTypeRefactoring.ChangeTypeAsync(
                                               context.Document,
                                               memberType,
                                               newType,
                                               cancellationToken));
                                });
                            }

                            if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.AddCastExpression, RefactoringIdentifiers.CallToMethod) &&
                                yieldStatement.Expression.Span.Contains(context.Span) &&
                                memberTypeSymbol?.IsNamedType() == true)
                            {
                                var namedTypeSymbol = (INamedTypeSymbol)memberTypeSymbol;

                                if (namedTypeSymbol.IsConstructedFromIEnumerableOfT())
                                {
                                    ITypeSymbol argumentSymbol = namedTypeSymbol.TypeArguments[0];

                                    if (argumentSymbol != typeSymbol)
                                    {
                                        ModifyExpressionRefactoring.ComputeRefactoring(
                                            context,
                                            yieldStatement.Expression,
                                            argumentSymbol,
                                            semanticModel);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStatementWithIfStatement) &&
                context.Span.IsBetweenSpans(yieldStatement))
            {
                var refactoring = new ReplaceYieldStatementWithIfStatementRefactoring();
                await refactoring.ComputeRefactoringAsync(context, yieldStatement).ConfigureAwait(false);
            }
        }