private static Accessibility GetAccessibility(SyntaxNodeAnalysisContext context, MemberDeclarationSyntax declaration, SyntaxTokenList modifiers)
        {
            if (!modifiers.Any(f => SyntaxFacts.IsAccessibilityModifier(f.Kind())))
            {
                if (modifiers.Any(SyntaxKind.PartialKeyword))
                {
                    if (!declaration.IsKind(SyntaxKind.MethodDeclaration))
                    {
                        Accessibility?accessibility = GetPartialAccessModifier(context, declaration);

                        if (accessibility != null)
                        {
                            if (accessibility == Accessibility.NotApplicable)
                            {
                                return(SyntaxAccessibility.GetDefaultExplicitAccessibility(declaration));
                            }
                            else
                            {
                                return(accessibility.Value);
                            }
                        }
                    }
                }
                else
                {
                    return(SyntaxAccessibility.GetDefaultExplicitAccessibility(declaration));
                }
            }

            return(Accessibility.NotApplicable);
        }
Пример #2
0
        internal override bool ShouldIncludeAccessibilityModifier(SyntaxNode typeNode)
        {
            var typeDeclaration = typeNode as TypeDeclarationSyntax;

            return(typeDeclaration.Modifiers.Any(
                       m => SyntaxFacts.IsAccessibilityModifier(m.Kind())
                       ));
        }
Пример #3
0
        private TypeDeclarationSyntax MakeTypeNodePublic(TypeDeclarationSyntax nonPublicTypeNode)
        {
            var modifiersWithPublicAccesibility =
                SyntaxFactory.TokenList(
                    SyntaxFactory.Token(SyntaxKind.PublicKeyword));

            var modifiersToTransfer = nonPublicTypeNode.Modifiers.Where(token => !SyntaxFacts.IsAccessibilityModifier(token.Kind()));

            modifiersWithPublicAccesibility = modifiersWithPublicAccesibility.AddRange(modifiersToTransfer);
            return(nonPublicTypeNode.WithModifiers(modifiersWithPublicAccesibility));
        }
Пример #4
0
            private static SyntaxKind?GetVisibilityModifier(SyntaxTokenList list)
            {
                foreach (var token in list)
                {
                    if (SyntaxFacts.IsAccessibilityModifier(token.Kind()))
                    {
                        return(token.Kind());
                    }
                }

                return(null);
            }
            /// <summary>
            /// Return a node declaration that has a visibility modifier.  If one isn't present it will be added as the
            /// first modifier.  Any trivia before the node will be added as leading trivia to the added <see cref="SyntaxToken"/>.
            /// </summary>
            private static SyntaxNode EnsureVisibility <T>(T node, SyntaxTokenList originalModifiers, Func <T, SyntaxTokenList, T> withModifiers, Func <SyntaxKind> getDefaultVisibility) where T : SyntaxNode
            {
                if (originalModifiers.Any(x => SyntaxFacts.IsAccessibilityModifier(x.Kind())))
                {
                    return(node);
                }

                SyntaxKind visibilityKind = getDefaultVisibility();

                Debug.Assert(SyntaxFacts.IsAccessibilityModifier(visibilityKind));

                SyntaxTokenList modifierList;

                if (originalModifiers.Count == 0)
                {
                    var leadingTrivia = node.GetLeadingTrivia();
                    node = node.WithLeadingTrivia();

                    var visibilityToken = SyntaxFactory.Token(
                        leadingTrivia,
                        visibilityKind,
                        SyntaxFactory.TriviaList(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " ")));

                    modifierList = SyntaxFactory.TokenList(visibilityToken);
                }
                else
                {
                    var leadingTrivia   = originalModifiers[0].LeadingTrivia;
                    var visibilityToken = SyntaxFactory.Token(
                        leadingTrivia,
                        visibilityKind,
                        SyntaxFactory.TriviaList(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " ")));

                    var list = new List <SyntaxToken>();
                    list.Add(visibilityToken);
                    list.Add(originalModifiers[0].WithLeadingTrivia());
                    for (int i = 1; i < originalModifiers.Count; i++)
                    {
                        list.Add(originalModifiers[i]);
                    }

                    modifierList = SyntaxFactory.TokenList(list);
                }

                return(withModifiers(node, modifierList));
            }
            private static T EnsureVisibilityCore <T>(
                T originalNode,
                SyntaxTokenList originalModifierList,
                Func <T, SyntaxKind, T> withFirstModifier,
                Func <T, SyntaxTokenList, T> withModifiers,
                Func <SyntaxKind> getDefaultVisibility) where T : CSharpSyntaxNode
            {
                if (originalModifierList.Any(x => SyntaxFacts.IsAccessibilityModifier(x.Kind())))
                {
                    return(originalNode);
                }

                SyntaxKind visibilityKind = getDefaultVisibility();

                Debug.Assert(SyntaxFacts.IsAccessibilityModifier(visibilityKind));

                if (originalModifierList.Count == 0)
                {
                    return(withFirstModifier(originalNode, visibilityKind));
                }

                var leadingTrivia   = originalModifierList[0].LeadingTrivia;
                var visibilityToken = SyntaxFactory.Token(
                    leadingTrivia,
                    visibilityKind,
                    SyntaxFactory.TriviaList(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " ")));

                var list = new List <SyntaxToken>();

                list.Add(visibilityToken);
                list.Add(originalModifierList[0].WithLeadingTrivia());
                for (int i = 1; i < originalModifierList.Count; i++)
                {
                    list.Add(originalModifierList[i]);
                }

                return(withModifiers(originalNode, SyntaxFactory.TokenList(list)));
            }
Пример #7
0
        private static SyntaxTokenList UpdateDeclarationAccessibility(SyntaxTokenList modifiersList, Accessibility newAccessibility, CodeGenerationOptions options)
        {
            var newModifierTokens = ArrayBuilder <SyntaxToken> .GetInstance();

            CSharpCodeGenerationHelpers.AddAccessibilityModifiers(newAccessibility, newModifierTokens, options, Accessibility.NotApplicable);
            if (newModifierTokens.Count == 0)
            {
                return(modifiersList);
            }

            // TODO: Move more APIs to use pooled ArrayBuilder
            // https://github.com/dotnet/roslyn/issues/34960
            var list = newModifierTokens.ToList();

            newModifierTokens.Free();
            return(GetUpdatedDeclarationAccessibilityModifiers(list, modifiersList, (SyntaxToken modifier) => SyntaxFacts.IsAccessibilityModifier(modifier.Kind()))
                   .ToSyntaxTokenList());
        }
        private static SyntaxTokenList UpdateDeclarationAccessibility(SyntaxTokenList modifiersList, Accessibility newAccessibility, CodeGenerationOptions options)
        {
            var newModifierTokens = new List <SyntaxToken>();

            CSharpCodeGenerationHelpers.AddAccessibilityModifiers(newAccessibility, newModifierTokens, options, Accessibility.NotApplicable);
            if (newModifierTokens.Count == 0)
            {
                return(modifiersList);
            }

            return(GetUpdatedDeclarationAccessibilityModifiers(newModifierTokens, modifiersList, (SyntaxToken modifier) => SyntaxFacts.IsAccessibilityModifier(modifier.Kind()))
                   .ToSyntaxTokenList());
        }