Exemple #1
0
        internal static EnumDeclarationSyntax AddEnumMemberTo(EnumDeclarationSyntax destination, IFieldSymbol enumMember, CodeGenerationOptions options)
        {
            var members = new List <SyntaxNodeOrToken>();

            members.AddRange(destination.Members.GetWithSeparators());

            var member = GenerateEnumMemberDeclaration(enumMember, destination, options);

            if (members.Count == 0)
            {
                members.Add(member);
            }
            else if (members.LastOrDefault().CSharpKind() == SyntaxKind.CommaToken)
            {
                members.Add(member);
                members.Add(SyntaxFactory.Token(SyntaxKind.CommaToken));
            }
            else
            {
                var lastMember     = members.Last();
                var trailingTrivia = lastMember.GetTrailingTrivia();
                members[members.Count - 1] = lastMember.WithTrailingTrivia();
                members.Add(SyntaxFactory.Token(SyntaxKind.CommaToken).WithTrailingTrivia(trailingTrivia));
                members.Add(member);
            }

            SyntaxToken openBrace;
            SyntaxToken closeBrace;

            GetBraceTokens(destination, out openBrace, out closeBrace);

            return(destination.WithOpenBraceToken(openBrace)
                   .WithMembers(SyntaxFactory.SeparatedList <EnumMemberDeclarationSyntax>(members))
                   .WithCloseBraceToken(closeBrace));
        }
        internal static EnumDeclarationSyntax AddEnumMemberTo(EnumDeclarationSyntax destination, IFieldSymbol enumMember, CodeGenerationOptions options)
        {
            var members = new List<SyntaxNodeOrToken>();
            members.AddRange(destination.Members.GetWithSeparators());

            var member = GenerateEnumMemberDeclaration(enumMember, destination, options);

            if (members.Count == 0)
            {
                members.Add(member);
            }
            else if (members.LastOrDefault().CSharpKind() == SyntaxKind.CommaToken)
            {
                members.Add(member);
                members.Add(SyntaxFactory.Token(SyntaxKind.CommaToken));
            }
            else
            {
                var lastMember = members.Last();
                var trailingTrivia = lastMember.GetTrailingTrivia();
                members[members.Count - 1] = lastMember.WithTrailingTrivia();
                members.Add(SyntaxFactory.Token(SyntaxKind.CommaToken).WithTrailingTrivia(trailingTrivia));
                members.Add(member);
            }

            SyntaxToken openBrace;
            SyntaxToken closeBrace;
            GetBraceTokens(destination, out openBrace, out closeBrace);

            return destination.WithOpenBraceToken(openBrace)
                                  .WithMembers(SyntaxFactory.SeparatedList<EnumMemberDeclarationSyntax>(members))
                                  .WithCloseBraceToken(closeBrace);
        }
Exemple #3
0
        public static EnumDeclarationSyntax EnsureOpenAndCloseBraceTokens(
            this EnumDeclarationSyntax typeDeclaration)
        {
            SyntaxToken openBrace, closeBrace;

            EnsureAndGetBraceTokens(typeDeclaration, typeDeclaration.Members.Count > 0, out openBrace, out closeBrace);
            return(typeDeclaration.WithOpenBraceToken(openBrace).WithCloseBraceToken(closeBrace));
        }