private void AddStaticModifier(CodeFixContext context, Diagnostic diagnostic, SyntaxNode node, string additionalKey = null)
        {
            if (node.IsKind(SyntaxKind.ConstructorDeclaration) &&
                ((ConstructorDeclarationSyntax)node).ParameterList?.Parameters.Any() == true)
            {
                return;
            }

            CodeAction codeAction = CodeAction.Create(
                "Add 'static' modifier",
                cancellationToken =>
            {
                SyntaxNode newNode = node;

                if (node.IsKind(SyntaxKind.ConstructorDeclaration))
                {
                    newNode = ModifierHelper.RemoveAccessModifiers(newNode);
                }

                newNode = newNode.InsertModifier(SyntaxKind.StaticKeyword, ModifierComparer.Instance);

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

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Example #2
0
        private void AddModifier(CodeFixContext context, Diagnostic diagnostic, MemberDeclarationSyntax memberDeclaration, SyntaxKind kind)
        {
            CodeAction codeAction = CodeAction.Create(
                $"Add '{ModifierHelper.GetModifierName(kind)}' modifier",
                cancellationToken => context.Document.InsertModifierAsync(memberDeclaration, kind, ModifierComparer.Instance, cancellationToken),
                GetEquivalenceKey(diagnostic, kind.ToString()));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Example #3
0
        internal static Task <Document> RemoveModifierAsync(
            this Document document,
            SyntaxNode node,
            SyntaxToken modifier,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode newNode = ModifierHelper.RemoveModifier(node, modifier);

            return(document.ReplaceNodeAsync(node, newNode, cancellationToken));
        }
        public static void AddModifier(
            CodeFixContext context,
            Diagnostic diagnostic,
            MemberDeclarationSyntax memberDeclaration,
            SyntaxKind kind)
        {
            Document document = context.Document;

            CodeAction codeAction = CodeAction.Create(
                $"Add '{ModifierHelper.GetModifierName(kind)}' modifier",
                cancellationToken => AddModifier(document, memberDeclaration, kind, cancellationToken),
                AbstractCodeFixProvider.GetEquivalenceKey(diagnostic, kind.ToString()));

            context.RegisterCodeFix(codeAction, diagnostic);
        }
Example #5
0
        internal static TNode ChangeAccessibility <TNode>(
            TNode node,
            AccessibilityInfo info,
            Accessibility newAccessibility,
            IModifierComparer comparer = null) where TNode : SyntaxNode
        {
            Accessibility accessibility = info.Accessibility;

            if (accessibility == newAccessibility)
            {
                return(node);
            }

            comparer = comparer ?? ModifierComparer.Instance;

            if (IsSingleTokenAccessibility(accessibility) &&
                IsSingleTokenAccessibility(newAccessibility))
            {
                int insertIndex = comparer.GetInsertIndex(info.Modifiers, GetKind(newAccessibility));

                if (info.Index == insertIndex ||
                    info.Index == insertIndex - 1)
                {
                    SyntaxToken newToken = CreateToken(newAccessibility).WithTriviaFrom(info.Token);

                    SyntaxTokenList newModifiers = info.Modifiers.Replace(info.Token, newToken);

                    return(node.WithModifiers(newModifiers));
                }
            }

            if (accessibility != Accessibility.NotApplicable)
            {
                node = ModifierHelper.RemoveModifierAt(node, Math.Max(info.Index, info.AdditionalIndex));

                if (accessibility == Accessibility.ProtectedOrInternal)
                {
                    node = ModifierHelper.RemoveModifierAt(node, Math.Min(info.Index, info.AdditionalIndex));
                }
            }

            if (newAccessibility != Accessibility.NotApplicable)
            {
                node = (TNode)InsertModifier(node, newAccessibility, comparer);
            }

            return(node);
        }
        private void RemoveAccessModifiers(CodeFixContext context, Diagnostic diagnostic, SyntaxNode node)
        {
            SyntaxTokenList modifiers = node.GetModifiers();

            var accessModifier = default(SyntaxToken);

            foreach (SyntaxToken modifier in modifiers)
            {
                if (modifier.IsAccessModifier())
                {
                    if (accessModifier.IsAccessModifier())
                    {
                        accessModifier = default(SyntaxToken);
                        break;
                    }
                    else
                    {
                        accessModifier = modifier;
                    }
                }
            }

            if (accessModifier.IsAccessModifier())
            {
                RemoveModifier(context, diagnostic, node, accessModifier);
            }
            else
            {
                CodeAction codeAction = CodeAction.Create(
                    "Remove accessibility modifiers",
                    cancellationToken =>
                {
                    SyntaxNode newNode = ModifierHelper.RemoveAccessModifiers(node);

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

                context.RegisterCodeFix(codeAction, diagnostic);
            }
        }
        private void RemoveModifiers(CodeFixContext context, Diagnostic diagnostic, SyntaxNode node, SyntaxTokenList modifiers, Func <SyntaxToken, bool> predicate)
        {
            List <int> indexes = null;

            for (int i = 0; i < modifiers.Count; i++)
            {
                if (predicate(modifiers[i]))
                {
                    (indexes ?? (indexes = new List <int>())).Add(i);
                }
            }

            if (indexes != null)
            {
                if (indexes.Count == 1)
                {
                    RemoveModifier(context, diagnostic, node, modifiers[indexes[0]]);
                }
                else
                {
                    CodeAction codeAction = CodeAction.Create(
                        "Remove invalid modifiers",
                        cancellationToken =>
                    {
                        SyntaxNode newNode = node;

                        for (int i = indexes.Count - 1; i >= 0; i--)
                        {
                            newNode = ModifierHelper.RemoveModifierAt(newNode, indexes[i]);
                        }

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

                    context.RegisterCodeFix(codeAction, diagnostic);
                }
            }
        }
        private void RemoveModifiers(CodeFixContext context, Diagnostic diagnostic, SyntaxNode node)
        {
            SyntaxTokenList modifiers = node.GetModifiers();

            if (modifiers.Count == 1)
            {
                RemoveModifier(context, diagnostic, node, modifiers[0]);
            }
            else
            {
                CodeAction codeAction = CodeAction.Create(
                    "Remove modifiers",
                    cancellationToken =>
                {
                    SyntaxNode newNode = ModifierHelper.RemoveModifiers(node);

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

                context.RegisterCodeFix(codeAction, diagnostic);
            }
        }