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 void ComputeRefactorings(RefactoringContext context, ConditionalExpressionSyntax conditionalExpression)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.FormatConditionalExpression) &&
                (context.Span.IsEmpty || context.Span.IsBetweenSpans(conditionalExpression)))
            {
                if (conditionalExpression.IsSingleLine())
                {
                    context.RegisterRefactoring(
                        "Format ?: on multiple lines",
                        cancellationToken =>
                    {
                        return(FormatConditionalExpressionOnMultipleLinesRefactoring.RefactorAsync(
                                   context.Document,
                                   conditionalExpression,
                                   cancellationToken));
                    });
                }
                else
                {
                    context.RegisterRefactoring(
                        "Format ?: on a single line",
                        cancellationToken =>
                    {
                        return(FormatConditionalExpressionOnSingleLineRefactoring.RefactorAsync(
                                   context.Document,
                                   conditionalExpression,
                                   cancellationToken));
                    });
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceConditionalExpressionWithIfElse))
            {
                ReplaceConditionalExpressionWithIfElseRefactoring.ComputeRefactoring(context, conditionalExpression);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapExpressionsInConditionalExpression) &&
                SwapExpressionsInConditionalExpressionRefactoring.CanRefactor(context, 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);
            }
        }