Ejemplo n.º 1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ConditionalExpressionSyntax conditionalExpression)
        {
            if (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(conditionalExpression))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatConditionalExpression))
                {
                    if (conditionalExpression.IsSingleLine())
                    {
                        context.RegisterRefactoring(
                            "Format ?: on separate lines",
                            cancellationToken =>
                        {
                            return(DocumentFormatter.ToMultiLineAsync(
                                       context.Document,
                                       conditionalExpression,
                                       cancellationToken));
                        });
                    }
                    else
                    {
                        context.RegisterRefactoring(
                            "Format ?: on a single line",
                            cancellationToken =>
                        {
                            return(DocumentFormatter.ToSingleLineAsync(
                                       context.Document,
                                       conditionalExpression,
                                       cancellationToken));
                        });
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceConditionalExpressionWithIfElse))
                {
                    await ReplaceConditionalExpressionWithIfElseRefactoring.ComputeRefactoringAsync(context, conditionalExpression).ConfigureAwait(false);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapExpressionsInConditionalExpression) &&
                (context.Span.IsBetweenSpans(conditionalExpression) ||
                 context.Span.IsEmptyAndContainedInSpan(conditionalExpression.QuestionToken) ||
                 context.Span.IsEmptyAndContainedInSpan(conditionalExpression.ColonToken)) &&
                SwapExpressionsInConditionalExpressionRefactoring.CanRefactor(conditionalExpression))
            {
                context.RegisterRefactoring(
                    "Swap expressions in ?:",
                    cancellationToken =>
                {
                    return(SwapExpressionsInConditionalExpressionRefactoring.RefactorAsync(
                               context.Document,
                               conditionalExpression,
                               cancellationToken));
                });
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, ConditionalExpressionSyntax conditionalExpression)
        {
            if (context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(conditionalExpression))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatConditionalExpression))
                {
                    if (conditionalExpression.IsSingleLine())
                    {
                        context.RegisterRefactoring(
                            "Format ?: on separate lines",
                            ct => SyntaxFormatter.ToMultiLineAsync(context.Document, conditionalExpression, ct),
                            RefactoringIdentifiers.FormatConditionalExpression);
                    }
                    else if (conditionalExpression.DescendantTrivia(conditionalExpression.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                    {
                        context.RegisterRefactoring(
                            "Format ?: on a single line",
                            ct => SyntaxFormatter.ToSingleLineAsync(context.Document, conditionalExpression, ct),
                            RefactoringIdentifiers.FormatConditionalExpression);
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceConditionalExpressionWithIfElse))
                {
                    await ReplaceConditionalExpressionWithIfElseRefactoring.ComputeRefactoringAsync(context, conditionalExpression).ConfigureAwait(false);
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapExpressionsInConditionalExpression) &&
                (context.Span.IsBetweenSpans(conditionalExpression) ||
                 context.Span.IsEmptyAndContainedInSpan(conditionalExpression.QuestionToken, conditionalExpression.ColonToken)) &&
                SwapExpressionsInConditionalExpressionRefactoring.CanRefactor(conditionalExpression))
            {
                context.RegisterRefactoring(
                    "Swap expressions in ?:",
                    ct => SwapExpressionsInConditionalExpressionRefactoring.RefactorAsync(context.Document, conditionalExpression, ct),
                    RefactoringIdentifiers.SwapExpressionsInConditionalExpression);
            }
        }
Ejemplo n.º 3
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, LocalDeclarationStatementSyntax localDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddIdentifierToVariableDeclaration))
            {
                await AddIdentifierToLocalDeclarationRefactoring.ComputeRefactoringAsync(context, localDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.InitializeLocalWithDefaultValue))
            {
                await InitializeLocalWithDefaultValueRefactoring.ComputeRefactoringAsync(context, localDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.PromoteLocalToParameter))
            {
                await PromoteLocalToParameterRefactoring.ComputeRefactoringAsync(context, localDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStatementWithIfStatement) &&
                context.Span.IsBetweenSpans(localDeclaration))
            {
                await ReplaceConditionalExpressionWithIfElseRefactoring.ComputeRefactoringAsync(context, localDeclaration).ConfigureAwait(false);
            }
        }