Esempio n. 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))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AvoidBoxingOfValueType:
                {
                    CodeAction codeAction = CodeAction.Create(
                        (expression.IsKind(SyntaxKind.CharacterLiteralExpression))
                                    ? "Use string literal instead of character literal"
                                    : "Call 'ToString'",
                        cancellationToken => AvoidBoxingOfValueTypeRefactoring.RefactorAsync(context.Document, expression, cancellationToken),
                        GetEquivalenceKey(diagnostic));

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Esempio n. 2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

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

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

            if (expression == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AvoidBoxingOfValueType:
                {
                    CodeAction codeAction = CodeAction.Create(
                        (expression.IsKind(SyntaxKind.CharacterLiteralExpression))
                                    ? "Use string literal instead of character literal"
                                    : "Call 'ToString'",
                        cancellationToken => AvoidBoxingOfValueTypeRefactoring.RefactorAsync(context.Document, expression, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Esempio n. 3
0
        public override void Initialize(AnalysisContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            base.Initialize(context);
            context.EnableConcurrentExecution();

            context.RegisterSyntaxNodeAction(f => AvoidBoxingOfValueTypeRefactoring.AddExpression(f), SyntaxKind.AddExpression);
            context.RegisterSyntaxNodeAction(f => AvoidBoxingOfValueTypeRefactoring.Interpolation(f), SyntaxKind.Interpolation);
        }