Beispiel #1
0
        public override void Initialize(AnalysisContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            base.Initialize(context);

            context.RegisterSyntaxNodeAction(f => UseCoalesceExpressionRefactoring.AnalyzeIfStatement(f), SyntaxKind.IfStatement);
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

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

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

            if (statement == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AddEmptyLineAfterLastStatementInDoStatement:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Add empty line",
                        cancellationToken =>
                        {
                            return(AddEmptyLineAfterLastStatementInDoStatementRefactoring.RefactorAsync(
                                       context.Document,
                                       statement,
                                       cancellationToken));
                        },
                        diagnostic.Id + EquivalenceKeySuffix);

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

                case DiagnosticIdentifiers.ReplaceReturnStatementWithExpressionStatement:
                {
                    switch (statement.Kind())
                    {
                    case SyntaxKind.ReturnStatement:
                    {
                        CodeAction codeAction = CodeAction.Create(
                            "Remove 'return'",
                            cancellationToken => ReplaceReturnStatementWithExpressionStatementRefactoring.RefactorAsync(context.Document, (ReturnStatementSyntax)statement, cancellationToken),
                            diagnostic.Id + EquivalenceKeySuffix);

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

                    case SyntaxKind.YieldReturnStatement:
                    {
                        CodeAction codeAction = CodeAction.Create(
                            "Remove 'yield return'",
                            cancellationToken => ReplaceReturnStatementWithExpressionStatementRefactoring.RefactorAsync(context.Document, (YieldStatementSyntax)statement, cancellationToken),
                            diagnostic.Id + EquivalenceKeySuffix);

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

                    default:
                    {
                        Debug.Assert(false, statement.Kind().ToString());
                        break;
                    }
                    }

                    break;
                }

                case DiagnosticIdentifiers.UseCoalesceExpression:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Use coalesce expression",
                        cancellationToken =>
                        {
                            return(UseCoalesceExpressionRefactoring.RefactorAsync(
                                       context.Document,
                                       statement,
                                       cancellationToken));
                        },
                        diagnostic.Id + EquivalenceKeySuffix);

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

                case DiagnosticIdentifiers.RemoveRedundantDisposeOrCloseCall:
                {
                    var expressionStatement = (ExpressionStatementSyntax)statement;
                    var invocation          = (InvocationExpressionSyntax)expressionStatement.Expression;
                    var memberAccess        = (MemberAccessExpressionSyntax)invocation.Expression;

                    CodeAction codeAction = CodeAction.Create(
                        $"Remove redundant '{memberAccess.Name?.Identifier.ValueText}' call",
                        cancellationToken => RemoveRedundantDisposeOrCloseCallRefactoring.RefactorAsync(context.Document, expressionStatement, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

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

                case DiagnosticIdentifiers.RemoveRedundantContinueStatement:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove continue;",
                        cancellationToken => RemoveRedundantContinueStatementRefactoring.RefactorAsync(context.Document, (ContinueStatementSyntax)statement, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

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

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out StatementSyntax statement))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AddEmptyLineAfterLastStatementInDoStatement:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Add empty line",
                        cancellationToken =>
                        {
                            return(AddEmptyLineAfterLastStatementInDoStatementRefactoring.RefactorAsync(
                                       context.Document,
                                       statement,
                                       cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

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

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

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

                case DiagnosticIdentifiers.InlineLazyInitialization:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Inline lazy initialization",
                        cancellationToken =>
                        {
                            return(UseCoalesceExpressionRefactoring.InlineLazyInitializationAsync(
                                       context.Document,
                                       (IfStatementSyntax)statement,
                                       cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

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

                case DiagnosticIdentifiers.RemoveRedundantDisposeOrCloseCall:
                {
                    var expressionStatement = (ExpressionStatementSyntax)statement;
                    var invocation          = (InvocationExpressionSyntax)expressionStatement.Expression;
                    var memberAccess        = (MemberAccessExpressionSyntax)invocation.Expression;

                    CodeAction codeAction = CodeAction.Create(
                        $"Remove redundant '{memberAccess.Name?.Identifier.ValueText}' call",
                        cancellationToken => RemoveRedundantDisposeOrCloseCallRefactoring.RefactorAsync(context.Document, expressionStatement, cancellationToken),
                        GetEquivalenceKey(diagnostic));

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

                case DiagnosticIdentifiers.RemoveRedundantStatement:
                {
                    CodeAction codeAction = CodeAction.Create(
                        $"Remove redundant {statement.GetTitle()}",
                        cancellationToken => RemoveRedundantStatementRefactoring.RefactorAsync(context.Document, statement, cancellationToken),
                        GetEquivalenceKey(diagnostic));

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

                case DiagnosticIdentifiers.UseMethodChaining:
                {
                    var expressionStatement = (ExpressionStatementSyntax)statement;

                    Func <CancellationToken, Task <Document> > createChangedDocument;
                    if (expressionStatement.Expression.IsKind(SyntaxKind.InvocationExpression))
                    {
                        createChangedDocument = cancellationToken => UseMethodChainingRefactoring.WithoutAssignment.RefactorAsync(context.Document, expressionStatement, cancellationToken);
                    }
                    else
                    {
                        createChangedDocument = cancellationToken => UseMethodChainingRefactoring.WithAssignment.RefactorAsync(context.Document, expressionStatement, cancellationToken);
                    }

                    CodeAction codeAction = CodeAction.Create(
                        "Use method chaining",
                        createChangedDocument,
                        GetEquivalenceKey(diagnostic));

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