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

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out SyntaxNode node, predicate: f => f.IsKind(SyntaxKind.CastExpression, SyntaxKind.InvocationExpression)))
            {
                return;
            }

            switch (node.Kind())
            {
            case SyntaxKind.CastExpression:
            {
                CodeAction codeAction = CodeAction.Create(
                    "Remove redundant cast",
                    cancellationToken => RemoveRedundantCastRefactoring.RefactorAsync(context.Document, (CastExpressionSyntax)node, cancellationToken),
                    GetEquivalenceKey(DiagnosticIdentifiers.RemoveRedundantCast));

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

            case SyntaxKind.InvocationExpression:
            {
                CodeAction codeAction = CodeAction.Create(
                    "Remove redundant cast",
                    cancellationToken => RemoveRedundantCastRefactoring.RefactorAsync(context.Document, (InvocationExpressionSyntax)node, cancellationToken),
                    GetEquivalenceKey(DiagnosticIdentifiers.RemoveRedundantCast));

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

            SyntaxNode node = root
                              .FindNode(context.Span, getInnermostNodeForTie: true)?
                              .FirstAncestorOrSelf(f => f.IsKind(SyntaxKind.CastExpression, SyntaxKind.InvocationExpression));

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

            if (node == null)
            {
                return;
            }

            switch (node.Kind())
            {
            case SyntaxKind.CastExpression:
            {
                CodeAction codeAction = CodeAction.Create(
                    "Remove redundant cast",
                    cancellationToken => RemoveRedundantCastRefactoring.RefactorAsync(context.Document, (CastExpressionSyntax)node, cancellationToken),
                    DiagnosticIdentifiers.RemoveRedundantCast + EquivalenceKeySuffix);

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

            case SyntaxKind.InvocationExpression:
            {
                CodeAction codeAction = CodeAction.Create(
                    "Remove redundant cast",
                    cancellationToken => RemoveRedundantCastRefactoring.RefactorAsync(context.Document, (InvocationExpressionSyntax)node, cancellationToken),
                    DiagnosticIdentifiers.RemoveRedundantCast + EquivalenceKeySuffix);

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