diff --git a/src/Generator/Generators/Template.cs b/src/Generator/Generators/Template.cs index 6dcf2caa..31a16f0d 100644 --- a/src/Generator/Generators/Template.cs +++ b/src/Generator/Generators/Template.cs @@ -161,6 +161,55 @@ namespace CppSharp.Generators return builder.ToString(); } + public StringBuilder GenerateUnformatted(DriverOptions options) + { + if (CheckGenerate != null && !CheckGenerate()) + return new StringBuilder(0); + + if (Blocks.Count == 0) + return Text.StringBuilder; + + var builder = new StringBuilder(); + Block previousBlock = null; + + var blockIndex = 0; + foreach (var childBlock in Blocks) + { + var childText = childBlock.GenerateUnformatted(options); + + var nextBlock = (++blockIndex < Blocks.Count) + ? Blocks[blockIndex] + : null; + + if (nextBlock != null) + { + var nextText = nextBlock.GenerateUnformatted(options); + if (nextText.Length == 0 && + childBlock.NewLineKind == NewLineKind.IfNotEmpty) + continue; + } + + if (childText.Length == 0) + continue; + + if (previousBlock != null && + previousBlock.NewLineKind == NewLineKind.BeforeNextBlock) + builder.AppendLine(); + + builder.Append(childText); + + if (childBlock.NewLineKind == NewLineKind.Always) + builder.AppendLine(); + + previousBlock = childBlock; + } + + if (Text.StringBuilder.Length != 0) + builder.Append(Text.StringBuilder); + + return builder; + } + public bool IsEmpty { get @@ -267,6 +316,8 @@ namespace CppSharp.Generators public string Generate() { + if (Options.IsCSharpGenerator && Options.CompileCode) + return RootBlock.GenerateUnformatted(Options).ToString(); return RootBlock.Generate(Options); }