Example #1
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.RemoveTypeParameter))
            {
                return;
            }

            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 CompilerDiagnosticIdentifiers.TypeParameterHasSameNameAsTypeParameterFromOuterType:
                {
                    TypeParameterInfo info;
                    if (TypeParameterInfo.TryCreate(typeParameter, out info))
                    {
                        CodeAction codeAction = CodeAction.Create(
                            $"Remove type parameter '{info.Name}'",
                            cancellationToken =>
                            {
                                SeparatedSyntaxList <TypeParameterSyntax> parameters = info.TypeParameterList.Parameters;

                                TypeParameterListSyntax newTypeParameterList = (parameters.Count == 1)
                                            ? default(TypeParameterListSyntax)
                                            : info.TypeParameterList.WithParameters(parameters.Remove(typeParameter));

                                SyntaxNode newNode = GenericDeclarationHelper.WithTypeParameterList(info.Declaration, newTypeParameterList);

                                TypeParameterConstraintClauseSyntax constraintClause = info.ConstraintClause;

                                if (constraintClause != null)
                                {
                                    newNode = GenericDeclarationHelper.WithConstraintClauses(newNode, info.ConstraintClauses.Remove(constraintClause));
                                }

                                return(context.Document.ReplaceNodeAsync(info.Declaration, newNode, cancellationToken));
                            },
                            GetEquivalenceKey(diagnostic));

                        context.RegisterCodeFix(codeAction, diagnostic);
                    }

                    break;
                }
                }
            }
        }
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.RemoveTypeParameter))
            {
                return;
            }

            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 CompilerDiagnosticIdentifiers.TypeParameterHasSameNameAsTypeParameterFromOuterType:
                {
                    TypeParameterInfo typeParameterInfo = SyntaxInfo.TypeParameterInfo(typeParameter);

                    if (!typeParameterInfo.Success)
                    {
                        break;
                    }

                    CodeAction codeAction = CodeAction.Create(
                        $"Remove type parameter '{typeParameterInfo.Name}'",
                        cancellationToken =>
                        {
                            GenericInfo genericInfo = SyntaxInfo.GenericInfo(typeParameterInfo.Declaration);

                            GenericInfo newGenericInfo = genericInfo.RemoveTypeParameter(typeParameter);

                            TypeParameterConstraintClauseSyntax constraintClause = typeParameterInfo.ConstraintClause;

                            if (constraintClause != null)
                            {
                                newGenericInfo = newGenericInfo.RemoveConstraintClause(constraintClause);
                            }

                            return(context.Document.ReplaceNodeAsync(genericInfo.Declaration, newGenericInfo.Declaration, cancellationToken));
                        },
                        GetEquivalenceKey(diagnostic));

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