Beispiel #1
0
        public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);

            ArrowExpressionClauseSyntax arrowExpressionClause = root
                                                                .FindNode(context.Span)?
                                                                .FirstAncestorOrSelf <ArrowExpressionClauseSyntax>();

            if (arrowExpressionClause?.Parent?.SupportsExpressionBody() == true)
            {
                context.RegisterRefactoring(
                    "Expand expression-bodied member",
                    cancellationToken => ExpandExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, arrowExpressionClause, cancellationToken));
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

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

            if (arrowExpressionClause == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Expand expression-bodied member",
                cancellationToken => ExpandExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, arrowExpressionClause, cancellationToken),
                DiagnosticIdentifiers.AvoidMultilineExpressionBody + EquivalenceKeySuffix);

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }