Skip to content

Commit 760caf4

Browse files
ddobrevtritao
authored andcommitted
Simplified the logic for indentation by using numbers.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent b7d3d36 commit 760caf4

File tree

5 files changed

+18
-38
lines changed

5 files changed

+18
-38
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using System.Text;
54
using CppSharp.AST;
@@ -11,17 +10,15 @@ namespace CppSharp.Generators.CSharp
1110
{
1211
public class CSharpMarshalContext : MarshalContext
1312
{
14-
public CSharpMarshalContext(BindingContext context, Stack<uint> indentation)
13+
public CSharpMarshalContext(BindingContext context, uint indentation)
1514
: base(context, indentation)
1615
{
17-
ArgumentPrefix = new TextGenerator();
18-
indentation.PushTo(ArgumentPrefix.CurrentIndentation);
19-
Cleanup = new TextGenerator();
20-
indentation.PushTo(Cleanup.CurrentIndentation);
16+
ArgumentPrefix = new TextGenerator { CurrentIndentation = indentation };
17+
Cleanup = new TextGenerator { CurrentIndentation = indentation };
2118
}
2219

23-
public TextGenerator ArgumentPrefix { get; private set; }
24-
public TextGenerator Cleanup { get; private set; }
20+
public TextGenerator ArgumentPrefix { get; }
21+
public TextGenerator Cleanup { get; }
2522
public bool HasCodeBlock { get; set; }
2623
}
2724

src/Generator/Generators/Marshal.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
using CppSharp.AST;
2-
using System.Collections.Generic;
32

43
namespace CppSharp.Generators
54
{
65
public class MarshalContext : TypePrinter
76
{
8-
public MarshalContext(BindingContext context, Stack<uint> indentation)
7+
public MarshalContext(BindingContext context, uint indentation)
98
{
109
Context = context;
11-
Before = new TextGenerator();
12-
indentation.PushTo(Before.CurrentIndentation);
13-
Return = new TextGenerator();
14-
indentation.PushTo(Return.CurrentIndentation);
10+
Before = new TextGenerator { CurrentIndentation = indentation };
11+
Return = new TextGenerator { CurrentIndentation = indentation };
1512
MarshalVarPrefix = string.Empty;
1613
this.Indentation = indentation;
1714
}
@@ -31,7 +28,7 @@ public MarshalContext(BindingContext context, Stack<uint> indentation)
3128
public Function Function { get; set; }
3229

3330
public string MarshalVarPrefix { get; set; }
34-
public Stack<uint> Indentation { get; }
31+
public uint Indentation { get; }
3532
}
3633

3734
public abstract class MarshalPrinter<T> : AstVisitor where T : MarshalContext

src/Generator/Utils/BlockGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public abstract class BlockGenerator : ITextGenerator
240240
{
241241
public Block RootBlock { get; }
242242
public Block ActiveBlock { get; private set; }
243-
public Stack<uint> CurrentIndentation => ActiveBlock.Text.CurrentIndentation;
243+
public uint CurrentIndentation => ActiveBlock.Text.CurrentIndentation;
244244

245245
protected BlockGenerator()
246246
{
@@ -263,7 +263,7 @@ public void AddBlock(Block block)
263263
public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null)
264264
{
265265
var block = new Block { Kind = kind, Object = obj };
266-
CurrentIndentation.PushTo(block.Text.CurrentIndentation);
266+
block.Text.CurrentIndentation = CurrentIndentation;
267267
block.Text.IsStartOfLine = ActiveBlock.Text.IsStartOfLine;
268268
block.Text.NeedsNewLine = ActiveBlock.Text.NeedsNewLine;
269269
PushBlock(block);

src/Generator/Utils/TextGenerator.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Text;
53

64
namespace CppSharp
@@ -27,7 +25,7 @@ public class TextGenerator : ITextGenerator
2725
public StringBuilder StringBuilder = new StringBuilder();
2826
public bool IsStartOfLine { get; set; }
2927
public bool NeedsNewLine { get; set; }
30-
public Stack<uint> CurrentIndentation { get; } = new Stack<uint>();
28+
public uint CurrentIndentation { get; set; }
3129

3230
public TextGenerator()
3331
{
@@ -38,7 +36,7 @@ public TextGenerator(TextGenerator generator)
3836
StringBuilder = new StringBuilder(generator);
3937
IsStartOfLine = generator.IsStartOfLine;
4038
NeedsNewLine = generator.NeedsNewLine;
41-
CurrentIndentation = new Stack<uint>(generator.CurrentIndentation);
39+
CurrentIndentation = generator.CurrentIndentation;
4240
}
4341

4442
public TextGenerator Clone()
@@ -55,7 +53,8 @@ public void Write(string msg, params object[] args)
5553
msg = string.Format(msg, args);
5654

5755
if (IsStartOfLine && !string.IsNullOrWhiteSpace(msg))
58-
StringBuilder.Append(new string(' ', (int) CurrentIndentation.Sum(u => u)));
56+
StringBuilder.Append(new string(' ',
57+
(int) (CurrentIndentation * DefaultIndentation)));
5958

6059
if (msg.Length > 0)
6160
IsStartOfLine = msg.EndsWith(Environment.NewLine);
@@ -100,14 +99,14 @@ public void ResetNewLine()
10099
NeedsNewLine = false;
101100
}
102101

103-
public void Indent(uint indentation = DefaultIndentation)
102+
public void Indent(uint indentation = 1)
104103
{
105-
CurrentIndentation.Push(indentation);
104+
CurrentIndentation++;
106105
}
107106

108107
public void Unindent()
109108
{
110-
CurrentIndentation.Pop();
109+
CurrentIndentation--;
111110
}
112111

113112
public void WriteOpenBraceAndIndent()

src/Generator/Utils/Utils.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,4 @@ public static string GetRelativePath(string fromPath, string toPath)
128128
return uri1.MakeRelativeUri(uri2).ToString();
129129
}
130130
}
131-
132-
public static class CollectionExtensions
133-
{
134-
public static void PushTo<T>(this Stack<T> source, Stack<T> destination)
135-
{
136-
var array = new T[source.Count];
137-
source.CopyTo(array, 0);
138-
foreach (var element in array.Reverse())
139-
{
140-
destination.Push(element);
141-
}
142-
}
143-
}
144131
}

0 commit comments

Comments
 (0)