Ejemplo n.º 1
0
        public GenericParameterDeclarationSyntax WithConstraints(
            SeparatedSyntaxList <TypeParameterConstraintSyntax> constraints)
        {
            TypeParameterConstraintClauseSyntax newConstraintClause = null;

            if (constraints.Count != 0)
            {
                if (constraintClause == null)
                {
                    newConstraintClause = SyntaxFactory.TypeParameterConstraintClause(
                        SyntaxFactory.IdentifierName(typeParameter.Identifier.Text),
                        constraints);
                }
                else
                {
                    newConstraintClause = constraintClause.WithConstraints(constraints);
                }
            }

            return(new GenericParameterDeclarationSyntax(TypeParameter, newConstraintClause));
        }
Ejemplo n.º 2
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            if (!Settings.IsAnyEnabled(
                    CodeFixIdentifiers.RemoveConstraintClauses,
                    CodeFixIdentifiers.CombineConstraintClauses))
            {
                return;
            }

            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out TypeParameterConstraintClauseSyntax constraintClause))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case CompilerDiagnosticIdentifiers.ConstraintsAreNotAllowedOnNonGenericDeclarations:
                {
                    if (!Settings.IsEnabled(CodeFixIdentifiers.RemoveConstraintClauses))
                    {
                        break;
                    }

                    GenericInfo genericInfo = SyntaxInfo.GenericInfo(constraintClause);

                    if (!genericInfo.Success)
                    {
                        break;
                    }

                    CodeAction codeAction = CodeAction.Create(
                        "Remove constraints",
                        cancellationToken =>
                        {
                            GenericInfo newGenericInfo = genericInfo.RemoveAllConstraintClauses();

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

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

                case CompilerDiagnosticIdentifiers.ConstraintClauseHasAlreadyBeenSpecified:
                {
                    if (!Settings.IsEnabled(CodeFixIdentifiers.CombineConstraintClauses))
                    {
                        break;
                    }

                    GenericInfo genericInfo = SyntaxInfo.GenericInfo(constraintClause);

                    int index = genericInfo.ConstraintClauses.IndexOf(constraintClause);

                    string name = constraintClause.Name.Identifier.ValueText;

                    TypeParameterConstraintClauseSyntax constraintClause2 = genericInfo.FindConstraintClause(name);

                    if (constraintClause2 == null)
                    {
                        break;
                    }

                    CodeAction codeAction = CodeAction.Create(
                        $"Combine constraints for '{name}'",
                        cancellationToken =>
                        {
                            TypeParameterConstraintClauseSyntax newConstraintClause = constraintClause2.WithConstraints(constraintClause2.Constraints.AddRange(constraintClause.Constraints));

                            SyntaxList <TypeParameterConstraintClauseSyntax> newConstraintClauses = genericInfo.ConstraintClauses
                                                                                                    .Replace(constraintClause2, newConstraintClause)
                                                                                                    .RemoveAt(index);

                            GenericInfo newGenericInfo = genericInfo.WithConstraintClauses(newConstraintClauses);

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

                    context.RegisterCodeFix(codeAction, diagnostic);
                    break;
                }
                }
            }
        }
Ejemplo n.º 3
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            if (!Settings.IsAnyCodeFixEnabled(
                    CodeFixIdentifiers.RemoveConstraintClauses,
                    CodeFixIdentifiers.CombineConstraintClauses))
            {
                return;
            }

            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out TypeParameterConstraintClauseSyntax constraintClause))
            {
                return;
            }

            foreach (Diagnostic diagnostic in context.Diagnostics)
            {
                switch (diagnostic.Id)
                {
                case CompilerDiagnosticIdentifiers.ConstraintsAreNotAllowedOnNonGenericDeclarations:
                {
                    if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.RemoveConstraintClauses))
                    {
                        break;
                    }

                    CodeAction codeAction = CodeAction.Create(
                        "Remove constraints",
                        cancellationToken =>
                        {
                            SyntaxNode node = constraintClause.Parent;

                            SyntaxNode newNode = GenericDeclarationHelper.RemoveConstraintClauses(node);

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

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

                case CompilerDiagnosticIdentifiers.ConstraintClauseHasAlreadyBeenSpecified:
                {
                    if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.CombineConstraintClauses))
                    {
                        break;
                    }

                    SyntaxList <TypeParameterConstraintClauseSyntax> constraintClauses = GenericDeclarationHelper.GetContainingList(constraintClause);

                    int index = constraintClauses.IndexOf(constraintClause);

                    string name = constraintClause.NameText();

                    TypeParameterConstraintClauseSyntax constraintClause2 = constraintClauses.FirstOrDefault(f => string.Equals(name, f.NameText(), StringComparison.Ordinal));

                    if (constraintClause2 == null)
                    {
                        break;
                    }

                    CodeAction codeAction = CodeAction.Create(
                        $"Combine constraints for '{name}'",
                        cancellationToken =>
                        {
                            SyntaxNode node = constraintClause.Parent;

                            TypeParameterConstraintClauseSyntax newConstraintClause = constraintClause2.WithConstraints(constraintClause2.Constraints.AddRange(constraintClause.Constraints));

                            SyntaxList <TypeParameterConstraintClauseSyntax> newConstraintClauses = constraintClauses
                                                                                                    .Replace(constraintClause2, newConstraintClause)
                                                                                                    .RemoveAt(index);

                            SyntaxNode newNode = GenericDeclarationHelper.WithConstraintClauses(node, newConstraintClauses);

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

                    context.RegisterCodeFix(codeAction, diagnostic);

                    break;
                }
                }
            }
        }