Esempio n. 1
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, LocalFunctionStatementSyntax localFunctionStatement)
        {
            if (localFunctionStatement.IsParentKind(SyntaxKind.Block))
            {
                BlockSyntax body = localFunctionStatement.Body;

                if (body != null)
                {
                    if (body.OpenBraceToken.Span.Contains(context.Span) ||
                        body.CloseBraceToken.Span.Contains(context.Span))
                    {
                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveMember))
                        {
                            context.RegisterRefactoring(CodeActionFactory.RemoveStatement(context.Document, localFunctionStatement, equivalenceKey: RefactoringIdentifiers.RemoveMember));
                        }

                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateMember))
                        {
                            context.RegisterRefactoring(
                                "Duplicate local function",
                                cancellationToken => DuplicateMemberDeclarationRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken),
                                RefactoringIdentifiers.DuplicateMember);
                        }

                        if (context.IsRefactoringEnabled(RefactoringIdentifiers.CommentOutMember))
                        {
                            CommentOutRefactoring.RegisterRefactoring(context, localFunctionStatement);
                        }
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ChangeMethodReturnTypeToVoid) &&
                context.Span.IsEmptyAndContainedInSpan(localFunctionStatement))
            {
                await ChangeMethodReturnTypeToVoidRefactoring.ComputeRefactoringAsync(context, localFunctionStatement).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddTypeParameter))
            {
                AddTypeParameterRefactoring.ComputeRefactoring(context, localFunctionStatement);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.ConvertBlockBodyToExpressionBody) &&
                ConvertBlockBodyToExpressionBodyRefactoring.CanRefactor(localFunctionStatement, context.Span))
            {
                context.RegisterRefactoring(
                    ConvertBlockBodyToExpressionBodyRefactoring.Title,
                    cancellationToken => ConvertBlockBodyToExpressionBodyRefactoring.RefactorAsync(context.Document, localFunctionStatement, cancellationToken),
                    RefactoringIdentifiers.ConvertBlockBodyToExpressionBody);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MoveUnsafeContextToContainingDeclaration))
            {
                MoveUnsafeContextToContainingDeclarationRefactoring.ComputeRefactoring(context, localFunctionStatement);
            }
        }
Esempio n. 2
0
        public static void RemoveStatement(
            CodeFixContext context,
            Diagnostic diagnostic,
            StatementSyntax statement,
            string title         = null,
            string additionalKey = null)
        {
            if (statement.IsEmbedded())
            {
                return;
            }

            CodeAction codeAction = CodeActionFactory.RemoveStatement(context.Document, statement, title: title, equivalenceKey: EquivalenceKey.Create(diagnostic, additionalKey));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Esempio n. 3
0
        private static void RegisterRefactoring(RefactoringContext context, StatementSyntax statement)
        {
            bool isEmbedded = statement.IsEmbedded();

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.RemoveStatement) &&
                !isEmbedded)
            {
                context.RegisterRefactoring(CodeActionFactory.RemoveStatement(context.Document, statement, "Remove statement", RefactoringIdentifiers.RemoveStatement));
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.DuplicateStatement))
            {
                context.RegisterRefactoring(
                    "Duplicate statement",
                    cancellationToken => DuplicateStatementAsync(context.Document, statement, cancellationToken),
                    RefactoringIdentifiers.DuplicateStatement);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CommentOutStatement) &&
                !isEmbedded)
            {
                CommentOutRefactoring.RegisterRefactoring(context, statement);
            }
        }
        private static void RegisterRefactoring(RefactoringContext context, StatementSyntax statement)
        {
            bool isEmbedded = statement.IsEmbedded();

            if (context.IsRefactoringEnabled(RefactoringDescriptors.RemoveStatement) &&
                !isEmbedded)
            {
                context.RegisterRefactoring(CodeActionFactory.RemoveStatement(context.Document, statement, "Remove statement", EquivalenceKey.Create(RefactoringDescriptors.RemoveStatement)));
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.CopyStatement))
            {
                context.RegisterRefactoring(
                    "Copy statement",
                    ct => CopyStatementAsync(context.Document, statement, ct),
                    RefactoringDescriptors.CopyStatement);
            }

            if (context.IsRefactoringEnabled(RefactoringDescriptors.CommentOutStatement) &&
                !isEmbedded)
            {
                CommentOutRefactoring.RegisterRefactoring(context, statement);
            }
        }
        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.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 = CodeActionFactory.RemoveStatement(
                        context.Document,
                        statement,
                        title: $"Remove redundant {CSharpFacts.GetTitle(statement)}",
                        equivalenceKey: 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;
                }
                }
            }
        }