Example #1
0
        public static void GenerateCodeFromCompositeTypeMember(CodeCompositeTypeMember member, CodeCompositeTypeDeclaration enclosingType, ICodeWriter writer, CodeGeneratorOptions options)
        {
            var generator = new CodeGenerator(writer, options);

            member.Generate(generator, enclosingType);
        }
        protected override void GenerateInner(CodeGenerator generator)
        {
            Modifiers.Generate(generator);

            if (IsPartial)
            {
                generator.Write(TokenType.Keyword, "partial");
                generator.Write(TokenType.Space, ' ');
            }

            generator.Write(TokenType.Keyword, Keyword);
            generator.Write(TokenType.Space, ' ');
            generator.OutputIdentifier(TokenType.TypeIdentifier, Name);

            generator.OutputTypeParameters(TypeParameters);

            var firstTypeRef = true;

            foreach (CodeTypeReference typeRef in BaseTypes)
            {
                if (firstTypeRef)
                {
                    generator.Write(TokenType.Space, ' ');
                    generator.Write(TokenType.Punctuation, ':');
                    generator.Write(TokenType.Space, ' ');
                    firstTypeRef = false;
                }
                else
                {
                    generator.Write(TokenType.Punctuation, ',');
                    generator.Write(TokenType.Space, ' ');
                }
                typeRef.Generate(generator);
            }

            foreach (var typeParameter in TypeParameters)
            {
                typeParameter.GenerateConstraints(generator);
            }

            if (Members.Count > 0)
            {
                generator.WriteOpeningBrace();
                generator.Indent++;

                CodeCompositeTypeMember previousMember = null;
                var sortedMembers = GetMembersSortedByCategory();

                foreach (var member in sortedMembers)
                {
                    if (previousMember != null && (previousMember.Category != member.Category || ShouldLineSeparateIndividualMember(previousMember.Category)))
                    {
                        generator.WriteLine();
                    }

                    member.Generate(generator, this);
                    previousMember = member;
                }

                generator.Indent--;
                generator.WriteClosingBrace();
            }
            else
            {
                generator.WriteEmptyBlock();
            }
        }