Example #1
0
        public sealed 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.ConditionalExpression) || f is BinaryExpressionSyntax))
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                $"Parenthesize '{expression}'",
                cancellationToken => AddParenthesesAccordingToOperatorPrecedenceRefactoring.RefactorAsync(context.Document, expression, cancellationToken),
                GetEquivalenceKey(DiagnosticIdentifiers.AddParenthesesAccordingToOperatorPrecedence));

            context.RegisterCodeFix(codeAction, context.Diagnostics);
        }
Example #2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            var expression = (ExpressionSyntax)root
                             .FindNode(context.Span, getInnermostNodeForTie: true)?
                             .FirstAncestorOrSelf(f => f.IsKind(SyntaxKind.ConditionalExpression) || f is BinaryExpressionSyntax);

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

            if (expression == null)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                $"Parenthesize '{expression}'",
                cancellationToken => AddParenthesesAccordingToOperatorPrecedenceRefactoring.RefactorAsync(context.Document, expression, cancellationToken),
                DiagnosticIdentifiers.AddParenthesesAccordingToOperatorPrecedence + EquivalenceKeySuffix);

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