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

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

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

            if (typeParameter == null)
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AddTypeParameterToDocumentationComment:
                {
                    var refactoring = new AddTypeParameterToDocumentationCommentRefactoring();

                    CodeAction codeAction = CodeAction.Create(
                        "Add type parameter to documentation comment",
                        cancellationToken => refactoring.RefactorAsync(context.Document, typeParameter, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

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

                case DiagnosticIdentifiers.UnusedTypeParameter:
                {
                    CodeAction codeAction = CodeAction.Create(
                        $"Remove unused type parameter '{typeParameter.Identifier}'",
                        cancellationToken => UnusedTypeParameterRefactoring.RefactorAsync(context.Document, typeParameter, cancellationToken),
                        diagnostic.Id + EquivalenceKeySuffix);

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

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out TypeParameterSyntax typeParameter))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case DiagnosticIdentifiers.AddTypeParameterToDocumentationComment:
                {
                    var refactoring = new AddTypeParameterToDocumentationCommentRefactoring();

                    CodeAction codeAction = CodeAction.Create(
                        "Add type parameter to documentation comment",
                        cancellationToken => refactoring.RefactorAsync(context.Document, typeParameter, cancellationToken),
                        GetEquivalenceKey(diagnostic));

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

                case DiagnosticIdentifiers.UnusedTypeParameter:
                {
                    CodeAction codeAction = CodeAction.Create(
                        $"Remove unused type parameter '{typeParameter.Identifier}'",
                        cancellationToken => UnusedParameterRefactoring.RefactorAsync(context.Document, typeParameter, cancellationToken),
                        GetEquivalenceKey(diagnostic));

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