コード例 #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",
                            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.ConvertConditionalOperatorToIfElse))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    (CodeAction codeAction, CodeAction recursiveCodeAction) = ConvertConditionalOperatorToIfElseRefactoring.ComputeRefactoring(
                        context.Document,
                        conditionalExpression,
                        new CodeActionData(ConvertConditionalOperatorToIfElseRefactoring.Title, RefactoringIdentifiers.ConvertConditionalOperatorToIfElse),
                        new CodeActionData(ConvertConditionalOperatorToIfElseRefactoring.RecursiveTitle, ConvertConditionalOperatorToIfElseRecursiveEquivalenceKey),
                        semanticModel,
                        context.CancellationToken);

                    if (codeAction != null)
                    {
                        context.RegisterRefactoring(codeAction);
                    }

                    if (recursiveCodeAction != null)
                    {
                        context.RegisterRefactoring(recursiveCodeAction);
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.InvertConditionalExpression) &&
                (context.Span.IsBetweenSpans(conditionalExpression) ||
                 context.Span.IsEmptyAndContainedInSpan(conditionalExpression.QuestionToken, conditionalExpression.ColonToken)) &&
                InvertConditionalExpressionRefactoring.CanRefactor(conditionalExpression))
            {
                context.RegisterRefactoring(
                    "Invert ?:",
                    ct => InvertConditionalExpressionRefactoring.RefactorAsync(context.Document, conditionalExpression, ct),
                    RefactoringIdentifiers.InvertConditionalExpression);
            }
        }
コード例 #2
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));
                });
            }
        }
コード例 #3
0
        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));
                });
            }
        }
コード例 #4
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",
                            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);
            }
        }