コード例 #1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, InvocationExpressionSyntax invocation)
        {
            if (CheckSpan(invocation, context.Span))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                IMethodSymbol methodSymbol = GetMethodSymbol(invocation, semanticModel, context.CancellationToken);

                if (methodSymbol != null)
                {
                    MethodDeclarationSyntax method = await GetMethodAsync(methodSymbol, context.CancellationToken).ConfigureAwait(false);

                    if (method != null)
                    {
                        ExpressionSyntax expression = GetMethodExpression(method);

                        if (expression != null)
                        {
                            List <ParameterInfo> parameterInfos = GetParameterInfos(invocation, methodSymbol, semanticModel, context.CancellationToken);

                            if (parameterInfos != null)
                            {
                                context.RegisterRefactoring(
                                    "Inline method",
                                    c => InlineMethodAsync(context.Document, invocation, expression, parameterInfos.ToArray(), c));

                                if (!method.Contains(invocation))
                                {
                                    context.RegisterRefactoring(
                                        "Inline and remove method",
                                        c => InlineAndRemoveMethodAsync(context.Document, invocation, method, expression, parameterInfos.ToArray(), c));
                                }
                            }
                        }
                        else if (methodSymbol.ReturnsVoid &&
                                 invocation.IsParentKind(SyntaxKind.ExpressionStatement))
                        {
                            StatementSyntax[] statements = method.Body?.Statements.ToArray();

                            if (statements?.Length > 0)
                            {
                                List <ParameterInfo> parameterInfos = GetParameterInfos(invocation, methodSymbol, semanticModel, context.CancellationToken);

                                if (parameterInfos != null)
                                {
                                    var expressionStatement = (ExpressionStatementSyntax)invocation.Parent;

                                    context.RegisterRefactoring(
                                        "Inline method",
                                        c => InlineMethodAsync(context.Document, expressionStatement, statements, parameterInfos.ToArray(), c));

                                    if (!method.Contains(invocation))
                                    {
                                        context.RegisterRefactoring(
                                            "Inline and remove method",
                                            c => InlineAndRemoveMethodAsync(context.Document, expressionStatement, method, statements, parameterInfos.ToArray(), c));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }