diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 26d2164f..74d39131 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Text; using CppSharp.AST; @@ -11,17 +10,15 @@ namespace CppSharp.Generators.CSharp { public class CSharpMarshalContext : MarshalContext { - public CSharpMarshalContext(BindingContext context, Stack indentation) + public CSharpMarshalContext(BindingContext context, uint indentation) : base(context, indentation) { - ArgumentPrefix = new TextGenerator(); - indentation.PushTo(ArgumentPrefix.CurrentIndentation); - Cleanup = new TextGenerator(); - indentation.PushTo(Cleanup.CurrentIndentation); + ArgumentPrefix = new TextGenerator { CurrentIndentation = indentation }; + Cleanup = new TextGenerator { CurrentIndentation = indentation }; } - public TextGenerator ArgumentPrefix { get; private set; } - public TextGenerator Cleanup { get; private set; } + public TextGenerator ArgumentPrefix { get; } + public TextGenerator Cleanup { get; } public bool HasCodeBlock { get; set; } } diff --git a/src/Generator/Generators/Marshal.cs b/src/Generator/Generators/Marshal.cs index 83f3cdc4..db7aa0b4 100644 --- a/src/Generator/Generators/Marshal.cs +++ b/src/Generator/Generators/Marshal.cs @@ -1,17 +1,14 @@ using CppSharp.AST; -using System.Collections.Generic; namespace CppSharp.Generators { public class MarshalContext : TypePrinter { - public MarshalContext(BindingContext context, Stack indentation) + public MarshalContext(BindingContext context, uint indentation) { Context = context; - Before = new TextGenerator(); - indentation.PushTo(Before.CurrentIndentation); - Return = new TextGenerator(); - indentation.PushTo(Return.CurrentIndentation); + Before = new TextGenerator { CurrentIndentation = indentation }; + Return = new TextGenerator { CurrentIndentation = indentation }; MarshalVarPrefix = string.Empty; this.Indentation = indentation; } @@ -31,7 +28,7 @@ namespace CppSharp.Generators public Function Function { get; set; } public string MarshalVarPrefix { get; set; } - public Stack Indentation { get; } + public uint Indentation { get; } } public abstract class MarshalPrinter : AstVisitor where T : MarshalContext diff --git a/src/Generator/Utils/BlockGenerator.cs b/src/Generator/Utils/BlockGenerator.cs index 96afce24..f33000ce 100644 --- a/src/Generator/Utils/BlockGenerator.cs +++ b/src/Generator/Utils/BlockGenerator.cs @@ -240,7 +240,7 @@ namespace CppSharp { public Block RootBlock { get; } public Block ActiveBlock { get; private set; } - public Stack CurrentIndentation => ActiveBlock.Text.CurrentIndentation; + public uint CurrentIndentation => ActiveBlock.Text.CurrentIndentation; protected BlockGenerator() { @@ -263,7 +263,7 @@ namespace CppSharp public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null) { var block = new Block { Kind = kind, Object = obj }; - CurrentIndentation.PushTo(block.Text.CurrentIndentation); + block.Text.CurrentIndentation = CurrentIndentation; block.Text.IsStartOfLine = ActiveBlock.Text.IsStartOfLine; block.Text.NeedsNewLine = ActiveBlock.Text.NeedsNewLine; PushBlock(block); diff --git a/src/Generator/Utils/TextGenerator.cs b/src/Generator/Utils/TextGenerator.cs index 0b0a1d44..b703ee8f 100644 --- a/src/Generator/Utils/TextGenerator.cs +++ b/src/Generator/Utils/TextGenerator.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Text; namespace CppSharp @@ -27,7 +25,7 @@ namespace CppSharp public StringBuilder StringBuilder = new StringBuilder(); public bool IsStartOfLine { get; set; } public bool NeedsNewLine { get; set; } - public Stack CurrentIndentation { get; } = new Stack(); + public uint CurrentIndentation { get; set; } public TextGenerator() { @@ -38,7 +36,7 @@ namespace CppSharp StringBuilder = new StringBuilder(generator); IsStartOfLine = generator.IsStartOfLine; NeedsNewLine = generator.NeedsNewLine; - CurrentIndentation = new Stack(generator.CurrentIndentation); + CurrentIndentation = generator.CurrentIndentation; } public TextGenerator Clone() @@ -55,7 +53,8 @@ namespace CppSharp msg = string.Format(msg, args); if (IsStartOfLine && !string.IsNullOrWhiteSpace(msg)) - StringBuilder.Append(new string(' ', (int) CurrentIndentation.Sum(u => u))); + StringBuilder.Append(new string(' ', + (int) (CurrentIndentation * DefaultIndentation))); if (msg.Length > 0) IsStartOfLine = msg.EndsWith(Environment.NewLine); @@ -100,14 +99,14 @@ namespace CppSharp NeedsNewLine = false; } - public void Indent(uint indentation = DefaultIndentation) + public void Indent(uint indentation = 1) { - CurrentIndentation.Push(indentation); + CurrentIndentation++; } public void Unindent() { - CurrentIndentation.Pop(); + CurrentIndentation--; } public void WriteOpenBraceAndIndent() diff --git a/src/Generator/Utils/Utils.cs b/src/Generator/Utils/Utils.cs index 71617cc4..325c540c 100644 --- a/src/Generator/Utils/Utils.cs +++ b/src/Generator/Utils/Utils.cs @@ -128,17 +128,4 @@ namespace CppSharp return uri1.MakeRelativeUri(uri2).ToString(); } } - - public static class CollectionExtensions - { - public static void PushTo(this Stack source, Stack destination) - { - var array = new T[source.Count]; - source.CopyTo(array, 0); - foreach (var element in array.Reverse()) - { - destination.Push(element); - } - } - } }