コード例 #1
0
        public static async Task ComputeRefactoringAsync(RefactoringContext context, LocalDeclarationStatementSyntax localDeclaration)
        {
            SyntaxNode method = localDeclaration.GetContainingMethod();

            if (method?.IsKind(SyntaxKind.MethodDeclaration) == true)
            {
                VariableDeclarationSyntax declaration = localDeclaration.Declaration;

                if (declaration != null)
                {
                    VariableDeclaratorSyntax variable = declaration
                                                        .Variables
                                                        .FirstOrDefault(f => !f.IsMissing && f.Identifier.Span.Contains(context.Span));

                    if (variable != null)
                    {
                        TypeSyntax type = declaration.Type;

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

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

                                if (typeSymbol?.SupportsExplicitDeclaration() == true)
                                {
                                    type = Type(typeSymbol);
                                }
                                else
                                {
                                    type = null;
                                }
                            }

                            if (type != null)
                            {
                                context.RegisterRefactoring(
                                    $"Promote '{variable.Identifier.ValueText}' to parameter",
                                    cancellationToken =>
                                {
                                    return(RefactorAsync(
                                               context.Document,
                                               (MethodDeclarationSyntax)method,
                                               localDeclaration,
                                               type.WithoutTrivia(),
                                               variable,
                                               cancellationToken));
                                });
                            }
                        }
                    }
                }
            }
        }