<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CppSharp.AST;

namespace CppSharp.Generators
{
    public enum NewLineKind
    {
        Never,
        Always,
        BeforeNextBlock,
        IfNotEmpty
    }

    public class BlockKind
    {
        public const int Unknown = 0;
        public const int BlockComment = 1;
        public const int InlineComment = 2;
        public const int Header = 3;
        public const int Footer = 4;
        public const int LAST = 5;
    }

    public class Block : ITextGenerator
    {
        public TextGenerator Text { get; set; }
        public int Kind { get; set; }
        public NewLineKind NewLineKind { get; set; }

        public Block Parent { get; set; }
        private List<Block> Blocks { get; set; }

        public Declaration Declaration { get; set; }

        private bool hasIndentChanged;
        private bool isSubBlock;

        public Block() : this(BlockKind.Unknown)
        {

        }

        public Block(int kind)
        {
            Kind = kind;
            Blocks = new List<Block>();
            Text = new TextGenerator();
            hasIndentChanged = false;
            isSubBlock = false;
        }

        public void AddBlock(Block block)
        {
            if (Text.StringBuilder.Length != 0 || hasIndentChanged)
            {
                hasIndentChanged = false;
                var newBlock = new Block { Text = Text.Clone(), isSubBlock = true };
                Text.StringBuilder.Clear();

                AddBlock(newBlock);
            }

            block.Parent = this;
            Blocks.Add(block);
        }

        public IEnumerable<Block> FindBlocks(int kind)
        {
            foreach (var block in Blocks)
            {
                if (block.Kind ==  kind)
                    yield return block;

                foreach (var childBlock in block.FindBlocks(kind))
                    yield return childBlock;
            }
        }

        public virtual string Generate(DriverOptions options)
        {
            if (Blocks.Count == 0)
                return Text.ToString();

            var builder = new StringBuilder();
            uint totalIndent = 0;
            Block previousBlock = null;

            var blockIndex = 0;
            foreach (var childBlock in Blocks)
            {
                var childText = childBlock.Generate(options);

                var nextBlock = (++blockIndex < Blocks.Count)
                    ? Blocks[blockIndex]
                    : null;

                var skipBlock = false;
                if (nextBlock != null)
                {
                    var nextText = nextBlock.Generate(options);
                    if (string.IsNullOrEmpty(nextText) &&
                        childBlock.NewLineKind == NewLineKind.IfNotEmpty)
                        skipBlock = true;
                }

                if (skipBlock)
                    continue;

                if (string.IsNullOrEmpty(childText))
                    continue;

                var lines = childText.SplitAndKeep(Environment.NewLine).ToList();

                if (previousBlock != null &&
                    previousBlock.NewLineKind == NewLineKind.BeforeNextBlock)
                    builder.AppendLine();

                if (childBlock.isSubBlock)
                    totalIndent = 0;

                if (childBlock.Kind == BlockKind.BlockComment)
                {
                    // Wrap the comment to the max line width.
                    var maxSize = options.MaxIndent - totalIndent;
                    maxSize -= options.CommentPrefix.Length + 2;

                    lines = StringHelpers.WordWrapLines(childText, (int)maxSize);

                    for (var i = 0; i < lines.Count; ++i)
                    {
                        var line = lines[i];
                        if (!line.StartsWith(options.CommentPrefix))
                            lines[i] = options.CommentPrefix + " " + line;
                    }
                }

                foreach (var line in lines)
                {
                    if (string.IsNullOrEmpty(line))
                        continue;

                    if (!string.IsNullOrWhiteSpace(line))
                        builder.Append(new string(' ', (int)totalIndent));

                    builder.Append(line);

                    if (!line.EndsWith(Environment.NewLine))
                        builder.AppendLine();
                }

                if (childBlock.NewLineKind == NewLineKind.Always)
                    builder.AppendLine();

                totalIndent += childBlock.Text.Indent;

                previousBlock = childBlock;
            }

            if (Text.StringBuilder.Length != 0)
                builder.Append(Text.StringBuilder);

            return builder.ToString();
        }

        #region ITextGenerator implementation

        public uint Indent { get { return Text.Indent; } }

        public void Write(string msg, params object[] args)
        {
            Text.Write(msg, args);
        }

        public void WriteLine(string msg, params object[] args)
        {
            Text.WriteLine(msg, args);
        }

        public void WriteLineIndent(string msg, params object[] args)
        {
            Text.WriteLineIndent(msg, args);
        }
<commit_msg>Added property IsEmpty to Block.<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CppSharp.AST;

namespace CppSharp.Generators
{
    public enum NewLineKind
    {
        Never,
        Always,
        BeforeNextBlock,
        IfNotEmpty
    }

    public class BlockKind
    {
        public const int Unknown = 0;
        public const int BlockComment = 1;
        public const int InlineComment = 2;
        public const int Header = 3;
        public const int Footer = 4;
        public const int LAST = 5;
    }

    public class Block : ITextGenerator
    {
        public TextGenerator Text { get; set; }
        public int Kind { get; set; }
        public NewLineKind NewLineKind { get; set; }

        public Block Parent { get; set; }
        private List<Block> Blocks { get; set; }

        public Declaration Declaration { get; set; }

        private bool hasIndentChanged;
        private bool isSubBlock;

        public Block() : this(BlockKind.Unknown)
        {

        }

        public Block(int kind)
        {
            Kind = kind;
            Blocks = new List<Block>();
            Text = new TextGenerator();
            hasIndentChanged = false;
            isSubBlock = false;
        }

        public void AddBlock(Block block)
        {
            if (Text.StringBuilder.Length != 0 || hasIndentChanged)
            {
                hasIndentChanged = false;
                var newBlock = new Block { Text = Text.Clone(), isSubBlock = true };
                Text.StringBuilder.Clear();

                AddBlock(newBlock);
            }

            block.Parent = this;
            Blocks.Add(block);
        }

        public IEnumerable<Block> FindBlocks(int kind)
        {
            foreach (var block in Blocks)
            {
                if (block.Kind ==  kind)
                    yield return block;

                foreach (var childBlock in block.FindBlocks(kind))
                    yield return childBlock;
            }
        }

        public virtual string Generate(DriverOptions options)
        {
            if (Blocks.Count == 0)
                return Text.ToString();

            var builder = new StringBuilder();
            uint totalIndent = 0;
            Block previousBlock = null;

            var blockIndex = 0;
            foreach (var childBlock in Blocks)
            {
                var childText = childBlock.Generate(options);

                var nextBlock = (++blockIndex < Blocks.Count)
                    ? Blocks[blockIndex]
                    : null;

                var skipBlock = false;
                if (nextBlock != null)
                {
                    var nextText = nextBlock.Generate(options);
                    if (string.IsNullOrEmpty(nextText) &&
                        childBlock.NewLineKind == NewLineKind.IfNotEmpty)
                        skipBlock = true;
                }

                if (skipBlock)
                    continue;

                if (string.IsNullOrEmpty(childText))
                    continue;

                var lines = childText.SplitAndKeep(Environment.NewLine).ToList();

                if (previousBlock != null &&
                    previousBlock.NewLineKind == NewLineKind.BeforeNextBlock)
                    builder.AppendLine();

                if (childBlock.isSubBlock)
                    totalIndent = 0;

                if (childBlock.Kind == BlockKind.BlockComment)
                {
                    // Wrap the comment to the max line width.
                    var maxSize = options.MaxIndent - totalIndent;
                    maxSize -= options.CommentPrefix.Length + 2;

                    lines = StringHelpers.WordWrapLines(childText, (int)maxSize);

                    for (var i = 0; i < lines.Count; ++i)
                    {
                        var line = lines[i];
                        if (!line.StartsWith(options.CommentPrefix))
                            lines[i] = options.CommentPrefix + " " + line;
                    }
                }

                foreach (var line in lines)
                {
                    if (string.IsNullOrEmpty(line))
                        continue;

                    if (!string.IsNullOrWhiteSpace(line))
                        builder.Append(new string(' ', (int)totalIndent));

                    builder.Append(line);

                    if (!line.EndsWith(Environment.NewLine))
                        builder.AppendLine();
                }

                if (childBlock.NewLineKind == NewLineKind.Always)
                    builder.AppendLine();

                totalIndent += childBlock.Text.Indent;

                previousBlock = childBlock;
            }

            if (Text.StringBuilder.Length != 0)
                builder.Append(Text.StringBuilder);

            return builder.ToString();
        }

        public bool IsEmpty
        {
            get
            {
                if (Blocks.Any(block => !block.IsEmpty))
                    return false;

                return string.IsNullOrEmpty(Text.ToString());
            }
        }

        #region ITextGenerator implementation

        public uint Indent { get { return Text.Indent; } }

        public void Write(string msg, params object[] args)
        {
            Text.Write(msg, args);
        }

        public void WriteLine(string msg, params object[] args)
        {
            Text.WriteLine(msg, args);
        }

        public void WriteLineIndent(string msg, params object[] args)
        {
            Text.WriteLineIndent(msg, args);
        }

{'fraction_non_alphanumeric': 0.081374321880651, 'fraction_numerical': 0.002531645569620253, 'mean_word_length': 1.2796784830997527, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5308090', 'n_tokens_mistral': 2800, 'n_tokens_neox': 2573, 'n_words': 675}