Beispiel #1
0
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out ConditionalExpressionSyntax conditionalExpression))
            {
                return;
            }

            Document document = context.Document;

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.UseCoalesceExpressionInsteadOfConditionalExpression:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Use coalesce expression",
                        ct =>
                        {
                            return(SimplifyNullCheckRefactoring.RefactorAsync(
                                       document,
                                       conditionalExpression,
                                       ct));
                        },
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.SimplifyConditionalExpression:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Simplify conditional expression",
                        ct => SimplifyConditionalExpressionAsync(document, conditionalExpression, ct),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.UseConditionalAccessInsteadOfConditionalExpression:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Use conditional access",
                        ct =>
                        {
                            return(SimplifyNullCheckRefactoring.RefactorAsync(
                                       document,
                                       conditionalExpression,
                                       ct));
                        },
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.AvoidNestedConditionalOperators:
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    (CodeAction codeAction, CodeAction recursiveCodeAction) = ConvertConditionalExpressionToIfElseRefactoring.ComputeRefactoring(
                        document,
                        conditionalExpression,
                        data: default,
                        recursiveData: new CodeActionData(ConvertConditionalExpressionToIfElseRefactoring.Title, GetEquivalenceKey(diagnostic)),
                        semanticModel,
                        context.CancellationToken);

                    if (recursiveCodeAction != null)
                    {
                        context.RegisterCodeFix(recursiveCodeAction, diagnostic);
                    }

                    break;
                }
                }
            }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out ConditionalExpressionSyntax conditionalExpression))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.ParenthesizeConditionInConditionalExpression:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Wrap condition in parentheses",
                        cancellationToken => ParenthesizeConditionInConditionalExpressionRefactoring.RefactorAsync(context.Document, conditionalExpression, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.UseCoalesceExpressionInsteadOfConditionalExpression:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Use coalesce expression",
                        cancellationToken =>
                        {
                            return(SimplifyNullCheckRefactoring.RefactorAsync(
                                       context.Document,
                                       conditionalExpression,
                                       cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.SimplifyConditionalExpression:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Simplify conditional expression",
                        cancellationToken =>
                        {
                            return(SimplifyConditionalExpressionRefactoring.RefactorAsync(
                                       context.Document,
                                       conditionalExpression,
                                       cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.FormatConditionalExpression:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Format ? and : on next line",
                        cancellationToken =>
                        {
                            return(FormatConditionalExpressionRefactoring.RefactorAsync(
                                       context.Document,
                                       conditionalExpression,
                                       cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }

                case DiagnosticIdentifiers.UseConditionalAccessInsteadOfConditionalExpression:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Use conditional access",
                        cancellationToken =>
                        {
                            return(SimplifyNullCheckRefactoring.RefactorAsync(
                                       context.Document,
                                       conditionalExpression,
                                       cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }