public static void ComputeRefactorings(RefactoringContext context, UsingStatementSyntax usingStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.IntroduceLocalVariable))
            {
                ExpressionSyntax expression = usingStatement.Expression;

                if (expression != null)
                {
                    context.RegisterRefactoring(
                        IntroduceLocalVariableRefactoring.GetTitle(expression),
                        cancellationToken => IntroduceLocalVariableRefactoring.RefactorAsync(context.Document, usingStatement, expression, cancellationToken));
                }
            }
        }
Esempio n. 2
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ExpressionStatementSyntax expressionStatement)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddIdentifierToVariableDeclaration))
            {
                await AddIdentifierToLocalDeclarationRefactoring.ComputeRefactoringAsync(context, expressionStatement).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.IntroduceLocalVariable))
            {
                ExpressionSyntax expression = expressionStatement.Expression;

                if (expression?.IsMissing == false &&
                    context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(expression) &&
                    !expressionStatement.IsEmbedded() &&
                    !(expression is AssignmentExpressionSyntax) &&
                    !CSharpFacts.IsIncrementOrDecrementExpression(expression.Kind()))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    if (semanticModel.GetSymbol(expression, context.CancellationToken)?.IsErrorType() == false)
                    {
                        ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(expression, context.CancellationToken);

                        if (typeSymbol?.IsErrorType() == false &&
                            !typeSymbol.HasMetadataName(MetadataNames.System_Threading_Tasks_Task) &&
                            !typeSymbol.IsVoid())
                        {
                            bool addAwait = false;

                            if (typeSymbol.OriginalDefinition.EqualsOrInheritsFromTaskOfT())
                            {
                                ISymbol enclosingSymbol = semanticModel.GetEnclosingSymbol(expressionStatement.SpanStart, context.CancellationToken);

                                addAwait = enclosingSymbol.IsAsyncMethod();
                            }

                            context.RegisterRefactoring(
                                IntroduceLocalVariableRefactoring.GetTitle(expression),
                                cancellationToken => IntroduceLocalVariableRefactoring.RefactorAsync(context.Document, expressionStatement, typeSymbol, addAwait, semanticModel, cancellationToken),
                                RefactoringIdentifiers.IntroduceLocalVariable);
                        }
                    }
                }
            }
        }