Browse Source

Simplified the logic for indentation by using numbers.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1131/head
Dimitar Dobrev 7 years ago committed by João Matos
parent
commit
760caf4d83
  1. 13
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 11
      src/Generator/Generators/Marshal.cs
  3. 4
      src/Generator/Utils/BlockGenerator.cs
  4. 15
      src/Generator/Utils/TextGenerator.cs
  5. 13
      src/Generator/Utils/Utils.cs

13
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using CppSharp.AST; using CppSharp.AST;
@ -11,17 +10,15 @@ namespace CppSharp.Generators.CSharp
{ {
public class CSharpMarshalContext : MarshalContext public class CSharpMarshalContext : MarshalContext
{ {
public CSharpMarshalContext(BindingContext context, Stack<uint> indentation) public CSharpMarshalContext(BindingContext context, uint indentation)
: base(context, indentation) : base(context, indentation)
{ {
ArgumentPrefix = new TextGenerator(); ArgumentPrefix = new TextGenerator { CurrentIndentation = indentation };
indentation.PushTo(ArgumentPrefix.CurrentIndentation); Cleanup = new TextGenerator { CurrentIndentation = indentation };
Cleanup = new TextGenerator();
indentation.PushTo(Cleanup.CurrentIndentation);
} }
public TextGenerator ArgumentPrefix { get; private set; } public TextGenerator ArgumentPrefix { get; }
public TextGenerator Cleanup { get; private set; } public TextGenerator Cleanup { get; }
public bool HasCodeBlock { get; set; } public bool HasCodeBlock { get; set; }
} }

11
src/Generator/Generators/Marshal.cs

@ -1,17 +1,14 @@
using CppSharp.AST; using CppSharp.AST;
using System.Collections.Generic;
namespace CppSharp.Generators namespace CppSharp.Generators
{ {
public class MarshalContext : TypePrinter public class MarshalContext : TypePrinter
{ {
public MarshalContext(BindingContext context, Stack<uint> indentation) public MarshalContext(BindingContext context, uint indentation)
{ {
Context = context; Context = context;
Before = new TextGenerator(); Before = new TextGenerator { CurrentIndentation = indentation };
indentation.PushTo(Before.CurrentIndentation); Return = new TextGenerator { CurrentIndentation = indentation };
Return = new TextGenerator();
indentation.PushTo(Return.CurrentIndentation);
MarshalVarPrefix = string.Empty; MarshalVarPrefix = string.Empty;
this.Indentation = indentation; this.Indentation = indentation;
} }
@ -31,7 +28,7 @@ namespace CppSharp.Generators
public Function Function { get; set; } public Function Function { get; set; }
public string MarshalVarPrefix { get; set; } public string MarshalVarPrefix { get; set; }
public Stack<uint> Indentation { get; } public uint Indentation { get; }
} }
public abstract class MarshalPrinter<T> : AstVisitor where T : MarshalContext public abstract class MarshalPrinter<T> : AstVisitor where T : MarshalContext

4
src/Generator/Utils/BlockGenerator.cs

@ -240,7 +240,7 @@ namespace CppSharp
{ {
public Block RootBlock { get; } public Block RootBlock { get; }
public Block ActiveBlock { get; private set; } public Block ActiveBlock { get; private set; }
public Stack<uint> CurrentIndentation => ActiveBlock.Text.CurrentIndentation; public uint CurrentIndentation => ActiveBlock.Text.CurrentIndentation;
protected BlockGenerator() protected BlockGenerator()
{ {
@ -263,7 +263,7 @@ namespace CppSharp
public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null) public void PushBlock(BlockKind kind = BlockKind.Unknown, object obj = null)
{ {
var block = new Block { Kind = kind, Object = obj }; 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.IsStartOfLine = ActiveBlock.Text.IsStartOfLine;
block.Text.NeedsNewLine = ActiveBlock.Text.NeedsNewLine; block.Text.NeedsNewLine = ActiveBlock.Text.NeedsNewLine;
PushBlock(block); PushBlock(block);

15
src/Generator/Utils/TextGenerator.cs

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
namespace CppSharp namespace CppSharp
@ -27,7 +25,7 @@ namespace CppSharp
public StringBuilder StringBuilder = new StringBuilder(); public StringBuilder StringBuilder = new StringBuilder();
public bool IsStartOfLine { get; set; } public bool IsStartOfLine { get; set; }
public bool NeedsNewLine { get; set; } public bool NeedsNewLine { get; set; }
public Stack<uint> CurrentIndentation { get; } = new Stack<uint>(); public uint CurrentIndentation { get; set; }
public TextGenerator() public TextGenerator()
{ {
@ -38,7 +36,7 @@ namespace CppSharp
StringBuilder = new StringBuilder(generator); StringBuilder = new StringBuilder(generator);
IsStartOfLine = generator.IsStartOfLine; IsStartOfLine = generator.IsStartOfLine;
NeedsNewLine = generator.NeedsNewLine; NeedsNewLine = generator.NeedsNewLine;
CurrentIndentation = new Stack<uint>(generator.CurrentIndentation); CurrentIndentation = generator.CurrentIndentation;
} }
public TextGenerator Clone() public TextGenerator Clone()
@ -55,7 +53,8 @@ namespace CppSharp
msg = string.Format(msg, args); msg = string.Format(msg, args);
if (IsStartOfLine && !string.IsNullOrWhiteSpace(msg)) 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) if (msg.Length > 0)
IsStartOfLine = msg.EndsWith(Environment.NewLine); IsStartOfLine = msg.EndsWith(Environment.NewLine);
@ -100,14 +99,14 @@ namespace CppSharp
NeedsNewLine = false; NeedsNewLine = false;
} }
public void Indent(uint indentation = DefaultIndentation) public void Indent(uint indentation = 1)
{ {
CurrentIndentation.Push(indentation); CurrentIndentation++;
} }
public void Unindent() public void Unindent()
{ {
CurrentIndentation.Pop(); CurrentIndentation--;
} }
public void WriteOpenBraceAndIndent() public void WriteOpenBraceAndIndent()

13
src/Generator/Utils/Utils.cs

@ -128,17 +128,4 @@ namespace CppSharp
return uri1.MakeRelativeUri(uri2).ToString(); return uri1.MakeRelativeUri(uri2).ToString();
} }
} }
public static class CollectionExtensions
{
public static void PushTo<T>(this Stack<T> source, Stack<T> destination)
{
var array = new T[source.Count];
source.CopyTo(array, 0);
foreach (var element in array.Reverse())
{
destination.Push(element);
}
}
}
} }

Loading…
Cancel
Save