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

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out IfStatementSyntax ifStatement))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.MergeIfStatementWithNestedIfStatement:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Merge if with nested if",
                        cancellationToken =>
                        {
                            return(MergeIfStatementWithNestedIfStatementRefactoring.RefactorAsync(
                                       context.Document,
                                       ifStatement,
                                       cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

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

                case DiagnosticIdentifiers.ReplaceIfStatementWithAssignment:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Replace if with assignment",
                        cancellationToken =>
                        {
                            return(ReplaceIfStatementWithAssignmentRefactoring.RefactorAsync(
                                       context.Document,
                                       ifStatement,
                                       cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

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

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

                    IfRefactoring refactoring = IfRefactoring.Analyze(
                        ifStatement,
                        UseCoalesceExpressionInsteadOfIfDiagnosticAnalyzer.AnalysisOptions,
                        semanticModel,
                        context.CancellationToken).First();

                    CodeAction codeAction = CodeAction.Create(
                        refactoring.Title,
                        cancellationToken => refactoring.RefactorAsync(context.Document, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Beispiel #2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out IfStatementSyntax ifStatement))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.MergeIfStatementWithNestedIfStatement:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Merge if with nested if",
                        cancellationToken =>
                        {
                            return(MergeIfStatementWithNestedIfStatementRefactoring.RefactorAsync(
                                       context.Document,
                                       ifStatement,
                                       cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

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

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

                    IfAnalysis analysis = IfAnalysis.Analyze(
                        ifStatement,
                        IfStatementAnalyzer.AnalysisOptions,
                        semanticModel,
                        context.CancellationToken).First();

                    CodeAction codeAction = CodeAction.Create(
                        analysis.Title,
                        cancellationToken => IfRefactoring.RefactorAsync(context.Document, analysis, cancellationToken),
                        GetEquivalenceKey(diagnostic));

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

                case DiagnosticIdentifiers.ReduceIfNesting:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Invert if",
                        cancellationToken =>
                        {
                            return(ReduceIfNestingRefactoring.RefactorAsync(
                                       context.Document,
                                       ifStatement,
                                       (SyntaxKind)Enum.Parse(typeof(SyntaxKind), diagnostic.Properties["JumpKind"]),
                                       recursive: true,
                                       cancellationToken: cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

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

                case DiagnosticIdentifiers.UseExceptionFilter:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Use exception filter",
                        cancellationToken =>
                        {
                            return(UseExceptionFilterAsync(
                                       context.Document,
                                       ifStatement,
                                       cancellationToken: cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Beispiel #3
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            IfStatementSyntax ifStatement = root
                                            .FindNode(context.Span, getInnermostNodeForTie: true)?
                                            .FirstAncestorOrSelf <IfStatementSyntax>();

            Debug.Assert(ifStatement != null, $"{nameof(ifStatement)} is null");

            if (ifStatement == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.MergeIfStatementWithNestedIfStatement:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Merge if with nested if",
                        cancellationToken =>
                        {
                            return(MergeIfStatementWithNestedIfStatementRefactoring.RefactorAsync(
                                       context.Document,
                                       ifStatement,
                                       cancellationToken));
                        },
                        diagnostic.Id + EquivalenceKeySuffix);

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

                case DiagnosticIdentifiers.ReplaceIfStatementWithAssignment:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Replace if with assignment",
                        cancellationToken =>
                        {
                            return(ReplaceIfStatementWithAssignmentRefactoring.RefactorAsync(
                                       context.Document,
                                       ifStatement,
                                       cancellationToken));
                        },
                        diagnostic.Id + EquivalenceKeySuffix);

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

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

                    IfRefactoring refactoring = IfRefactoring.Analyze(
                        ifStatement,
                        UseCoalesceExpressionInsteadOfIfDiagnosticAnalyzer.AnalysisOptions,
                        semanticModel,
                        context.CancellationToken).First();

                    CodeAction codeAction = CodeAction.Create(
                        refactoring.Title,
                        cancellationToken => refactoring.RefactorAsync(context.Document, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

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