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

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

            if (statement == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Add empty line",
                cancellationToken =>
            {
                return(AddEmptyLineAfterLastStatementInDoStatementRefactoring.RefactorAsync(
                           context.Document,
                           statement,
                           cancellationToken));
            },
                DiagnosticIdentifiers.AddEmptyLineAfterLastStatementInDoStatement + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
        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;
                }
                }
            }
        }
示例#3
0
        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.InlineLazyInitialization:
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Inline lazy initialization",
                        cancellationToken =>
                        {
                            return(InlineLazyInitializationRefactoring.RefactorAsync(
                                       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 {CSharpFacts.GetTitle(statement)}",
                        cancellationToken => RemoveRedundantStatementRefactoring.RefactorAsync(context.Document, statement, cancellationToken),
                        GetEquivalenceKey(diagnostic));

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

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

                    UseMethodChainingAnalysis analysis;
                    if (expressionStatement.Expression.Kind() == SyntaxKind.InvocationExpression)
                    {
                        analysis = UseMethodChainingAnalysis.WithoutAssignmentAnalysis;
                    }
                    else
                    {
                        analysis = UseMethodChainingAnalysis.WithAssignmentAnalysis;
                    }

                    CodeAction codeAction = CodeAction.Create(
                        "Use method chaining",
                        cancellationToken => UseMethodChainingRefactoring.RefactorAsync(context.Document, analysis, expressionStatement, cancellationToken),
                        GetEquivalenceKey(diagnostic));

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