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

            if (!TryFindFirstAncestorOrSelf(
                    root,
                    context.Span,
                    out ExpressionSyntax expression,
                    predicate: f => f.IsKind(
                        SyntaxKind.InvocationExpression,
                        SyntaxKind.ElementAccessExpression,
                        SyntaxKind.ConditionalAccessExpression)))
            {
                return;
            }

            Document   document   = context.Document;
            Diagnostic diagnostic = context.Diagnostics[0];

            CodeAction codeAction = CodeAction.Create(
                "Fix formatting",
                ct =>
            {
                TextLineCollection lines = expression.SyntaxTree.GetText().Lines;

                TextLine line = lines.GetLineFromPosition(expression.SpanStart);

                TextSpan span = TextSpan.FromBounds(line.EndIncludingLineBreak, expression.Span.End);

                return(CodeFixHelpers.FixCallChainAsync(document, expression, span, ct));
            },
                GetEquivalenceKey(diagnostic));

            context.RegisterCodeFix(codeAction, diagnostic);
        }