Exemplo n.º 1
0
        public static AccessibilityInfo ChangeAccessibility(
            AccessibilityInfo info,
            Accessibility newAccessibility,
            IModifierComparer comparer = null)
        {
            if (!info.Success)
            {
                return(info);
            }

            Accessibility accessibility = info.Accessibility;

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

            comparer = comparer ?? ModifierComparer.Instance;

            SyntaxNode node = info.Node;

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

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

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

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

            if (accessibility != Accessibility.NotApplicable)
            {
                node = Modifier.RemoveAt(node, Math.Max(info.TokenIndex, info.SecondTokenIndex));

                if (info.SecondTokenIndex != -1)
                {
                    node = Modifier.RemoveAt(node, Math.Min(info.TokenIndex, info.SecondTokenIndex));
                }
            }

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

            return(SyntaxInfo.AccessibilityInfo(node));
        }
Exemplo n.º 2
0
        public static TNode ChangeAccessibility <TNode>(
            TNode node,
            Accessibility newAccessibility,
            IModifierComparer comparer = null) where TNode : SyntaxNode
        {
            AccessibilityInfo info = SyntaxInfo.AccessibilityInfo(node);

            AccessibilityInfo newInfo = ChangeAccessibility(info, newAccessibility, comparer);

            return((TNode)newInfo.Node);
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, SyntaxToken modifier)
        {
            SyntaxNode node = modifier.Parent;

            if (!node.IsKind(SyntaxKind.DestructorDeclaration))
            {
                AccessibilityInfo info = SyntaxInfo.AccessibilityInfo(node);

                if (node.IsKind(
                        SyntaxKind.ClassDeclaration,
                        SyntaxKind.InterfaceDeclaration,
                        SyntaxKind.StructDeclaration))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    var symbol = (INamedTypeSymbol)semanticModel.GetDeclaredSymbol(node, context.CancellationToken);

                    ImmutableArray <SyntaxReference> syntaxReferences = symbol.DeclaringSyntaxReferences;

                    if (syntaxReferences.Length > 1)
                    {
                        ImmutableArray <MemberDeclarationSyntax> memberDeclarations = ImmutableArray.CreateRange(
                            syntaxReferences,
                            f => (MemberDeclarationSyntax)f.GetSyntax(context.CancellationToken));

                        foreach (Accessibility accessibility in _accessibilities)
                        {
                            if (accessibility != info.Accessibility &&
                                CSharpUtility.IsAllowedAccessibility(node, accessibility))
                            {
                                context.RegisterRefactoring(
                                    GetTitle(accessibility),
                                    cancellationToken => ChangeAccessibilityRefactoring.RefactorAsync(context.Solution, memberDeclarations, accessibility, cancellationToken));
                            }
                        }

                        return;
                    }
                }

                foreach (Accessibility accessibility in _accessibilities)
                {
                    if (accessibility != info.Accessibility &&
                        CSharpUtility.IsAllowedAccessibility(node, accessibility))
                    {
                        context.RegisterRefactoring(
                            GetTitle(accessibility),
                            cancellationToken => ChangeAccessibilityRefactoring.RefactorAsync(context.Document, node, accessibility, cancellationToken));
                    }
                }
            }
        }
        private static SyntaxNode WithAccessibility(MemberDeclarationSyntax node, Accessibility newAccessibility)
        {
            AccessibilityInfo info = SyntaxInfo.AccessibilityInfo(node);

            if (info.Accessibility == Accessibility.NotApplicable)
            {
                return(node);
            }

            AccessibilityInfo newInfo = info.WithAccessibility(newAccessibility, ModifierComparer.Instance);

            return(newInfo.Node);
        }
Exemplo n.º 5
0
        public static Task <Solution> RefactorAsync(
            Solution solution,
            ImmutableArray <MemberDeclarationSyntax> memberDeclarations,
            Accessibility newAccessibility,
            CancellationToken cancellationToken)
        {
            return(solution.ReplaceNodesAsync(
                       memberDeclarations,
                       (node, rewrittenNode) =>
            {
                AccessibilityInfo info = SyntaxInfo.AccessibilityInfo(node);

                if (info.Accessibility == Accessibility.NotApplicable)
                {
                    return node;
                }

                AccessibilityInfo newInfo = info.WithAccessibility(newAccessibility, ModifierComparer.Instance);

                return newInfo.Node;
            },
                       cancellationToken));
        }
        public static AccessibilityFlags GetAllowedAccessibilityFlags(MemberDeclarationSelection selectedMembers, bool allowOverride = false)
        {
            if (selectedMembers.Count < 2)
            {
                return(AccessibilityFlags.None);
            }

            var allFlags = AccessibilityFlags.None;

            AccessibilityFlags allowedFlags = AccessibilityFlags.Public
                                              | AccessibilityFlags.Internal
                                              | AccessibilityFlags.Protected
                                              | AccessibilityFlags.Private;

            foreach (MemberDeclarationSyntax member in selectedMembers)
            {
                Accessibility accessibility = SyntaxInfo.AccessibilityInfo(member).Accessibility;

                if (accessibility == Accessibility.NotApplicable)
                {
                    accessibility = member.GetDefaultExplicitAccessibility();

                    if (accessibility == Accessibility.NotApplicable)
                    {
                        return(AccessibilityFlags.None);
                    }
                }

                AccessibilityFlags flag = accessibility.GetAccessibilityFlag();

                switch (accessibility)
                {
                case Accessibility.Private:
                case Accessibility.Protected:
                case Accessibility.ProtectedAndInternal:
                case Accessibility.ProtectedOrInternal:
                case Accessibility.Internal:
                case Accessibility.Public:
                {
                    allFlags |= flag;
                    break;
                }

                default:
                {
                    Debug.Fail(accessibility.ToString());
                    return(AccessibilityFlags.None);
                }
                }

                foreach (Accessibility accessibility2 in Accessibilities)
                {
                    if (accessibility != accessibility2 &&
                        !CSharpUtility.IsAllowedAccessibility(member, accessibility2, allowOverride: allowOverride))
                    {
                        allowedFlags &= ~accessibility2.GetAccessibilityFlag();
                    }
                }
            }

            switch (allFlags)
            {
            case AccessibilityFlags.Private:
            case AccessibilityFlags.Protected:
            case AccessibilityFlags.Internal:
            case AccessibilityFlags.Public:
            {
                allowedFlags &= ~allFlags;
                break;
            }
            }

            return(allowedFlags);
        }