示例#1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, InvocationExpressionSyntax invocation)
        {
            ExpressionSyntax expression = invocation.Expression;

            if (expression?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true)
            {
                ArgumentListSyntax argumentList = invocation.ArgumentList;

                if (argumentList != null)
                {
                    var memberAccess = (MemberAccessExpressionSyntax)expression;

                    switch (memberAccess.Name?.Identifier.ValueText)
                    {
                    case "First":
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        if (argumentList.Arguments.Count == 0 &&
                            UseElementAccessInsteadOfFirstRefactoring.CanRefactor(invocation, memberAccess, semanticModel, context.CancellationToken))
                        {
                            context.RegisterRefactoring(
                                "Use [] instead of calling 'First'",
                                cancellationToken => UseElementAccessInsteadOfFirstRefactoring.RefactorAsync(context.Document, invocation, cancellationToken));
                        }

                        break;
                    }

                    case "Last":
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        if (argumentList.Arguments.Count == 0 &&
                            UseElementAccessInsteadOfLastRefactoring.CanRefactor(invocation, memberAccess, semanticModel, context.CancellationToken))
                        {
                            string propertyName = UseElementAccessInsteadOfLastRefactoring.GetCountOrLengthPropertyName(memberAccess.Expression, semanticModel, context.CancellationToken);

                            if (propertyName != null)
                            {
                                context.RegisterRefactoring(
                                    "Use [] instead of calling 'Last'",
                                    cancellationToken => UseElementAccessInsteadOfLastRefactoring.RefactorAsync(context.Document, invocation, propertyName, cancellationToken));
                            }
                        }

                        break;
                    }

                    case "ElementAt":
                    {
                        SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                        if (argumentList.Arguments.Count == 1 &&
                            UseElementAccessInsteadOfElementAtRefactoring.CanRefactor(invocation, argumentList, memberAccess, semanticModel, context.CancellationToken))
                        {
                            context.RegisterRefactoring(
                                "Use [] instead of calling 'ElementAt'",
                                cancellationToken => UseElementAccessInsteadOfElementAtRefactoring.RefactorAsync(context.Document, invocation, cancellationToken));
                        }

                        break;
                    }
                    }
                }
            }
        }
示例#2
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, InvocationExpressionSyntax invocation)
        {
            MemberInvocationExpressionInfo invocationInfo = SyntaxInfo.MemberInvocationExpressionInfo(invocation);

            if (!invocationInfo.Success)
            {
                return;
            }

            switch (invocationInfo.NameText)
            {
            case "First":
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (invocationInfo.Arguments.Any())
                {
                    break;
                }

                if (!UseElementAccessInsteadOfFirstRefactoring.CanRefactor(invocationInfo, semanticModel, context.CancellationToken))
                {
                    break;
                }

                context.RegisterRefactoring(
                    "Use [] instead of calling 'First'",
                    cancellationToken => UseElementAccessInsteadOfFirstRefactoring.RefactorAsync(context.Document, invocation, cancellationToken));

                break;
            }

            case "Last":
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (invocationInfo.Arguments.Any())
                {
                    break;
                }

                if (!UseElementAccessInsteadOfLastRefactoring.CanRefactor(invocationInfo, semanticModel, context.CancellationToken))
                {
                    break;
                }

                string propertyName = CSharpUtility.GetCountOrLengthPropertyName(invocationInfo.Expression, semanticModel, context.CancellationToken);

                if (propertyName == null)
                {
                    break;
                }

                context.RegisterRefactoring(
                    "Use [] instead of calling 'Last'",
                    cancellationToken => UseElementAccessInsteadOfLastRefactoring.RefactorAsync(context.Document, invocation, propertyName, cancellationToken));

                break;
            }

            case "ElementAt":
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (invocationInfo.Arguments.Count != 1)
                {
                    break;
                }

                if (!UseElementAccessInsteadOfElementAtRefactoring.CanRefactor(invocationInfo, semanticModel, context.CancellationToken))
                {
                    break;
                }

                context.RegisterRefactoring(
                    "Use [] instead of calling 'ElementAt'",
                    cancellationToken => UseElementAccessInsteadOfElementAtRefactoring.RefactorAsync(context.Document, invocation, cancellationToken));

                break;
            }
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, InvocationExpressionSyntax invocation)
        {
            MemberInvocationExpression memberInvocation;

            if (MemberInvocationExpression.TryCreate(invocation, out memberInvocation) &&
                memberInvocation.ArgumentList != null)
            {
                switch (memberInvocation.Name?.Identifier.ValueText)
                {
                case "First":
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    if (memberInvocation.ArgumentList.Arguments.Count == 0 &&
                        UseElementAccessInsteadOfFirstRefactoring.CanRefactor(memberInvocation, semanticModel, context.CancellationToken))
                    {
                        context.RegisterRefactoring(
                            "Use [] instead of calling 'First'",
                            cancellationToken => UseElementAccessInsteadOfFirstRefactoring.RefactorAsync(context.Document, invocation, cancellationToken));
                    }

                    break;
                }

                case "Last":
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    if (memberInvocation.ArgumentList.Arguments.Count == 0 &&
                        UseElementAccessInsteadOfLastRefactoring.CanRefactor(memberInvocation, semanticModel, context.CancellationToken))
                    {
                        string propertyName = UseElementAccessInsteadOfLastRefactoring.GetCountOrLengthPropertyName(memberInvocation.Expression, semanticModel, context.CancellationToken);

                        if (propertyName != null)
                        {
                            context.RegisterRefactoring(
                                "Use [] instead of calling 'Last'",
                                cancellationToken => UseElementAccessInsteadOfLastRefactoring.RefactorAsync(context.Document, invocation, propertyName, cancellationToken));
                        }
                    }

                    break;
                }

                case "ElementAt":
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    if (memberInvocation.ArgumentList.Arguments.Count == 1 &&
                        UseElementAccessInsteadOfElementAtRefactoring.CanRefactor(memberInvocation, semanticModel, context.CancellationToken))
                    {
                        context.RegisterRefactoring(
                            "Use [] instead of calling 'ElementAt'",
                            cancellationToken => UseElementAccessInsteadOfElementAtRefactoring.RefactorAsync(context.Document, invocation, cancellationToken));
                    }

                    break;
                }
                }
            }
        }