コード例 #1
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);
        }
コード例 #2
0
        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);
                }
            }
        }