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 @@ @@ -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 @@ -11,17 +10,15 @@ namespace CppSharp.Generators.CSharp
{
public class CSharpMarshalContext : MarshalContext
{
public CSharpMarshalContext(BindingContext context, Stack<uint> 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; }
}

11
src/Generator/Generators/Marshal.cs

@ -1,17 +1,14 @@ @@ -1,17 +1,14 @@
using CppSharp.AST;
using System.Collections.Generic;
namespace CppSharp.Generators
{
public class MarshalContext : TypePrinter
{
public MarshalContext(BindingContext context, Stack<uint> 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 @@ -31,7 +28,7 @@ namespace CppSharp.Generators
public Function Function { 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

4
src/Generator/Utils/BlockGenerator.cs

@ -240,7 +240,7 @@ namespace CppSharp @@ -240,7 +240,7 @@ namespace CppSharp
{
public Block RootBlock { get; }
public Block ActiveBlock { get; private set; }
public Stack<uint> CurrentIndentation => ActiveBlock.Text.CurrentIndentation;
public uint CurrentIndentation => ActiveBlock.Text.CurrentIndentation;
protected BlockGenerator()
{
@ -263,7 +263,7 @@ namespace CppSharp @@ -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);

15
src/Generator/Utils/TextGenerator.cs

@ -1,6 +1,4 @@ @@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CppSharp
@ -27,7 +25,7 @@ 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<uint> CurrentIndentation { get; } = new Stack<uint>();
public uint CurrentIndentation { get; set; }
public TextGenerator()
{
@ -38,7 +36,7 @@ namespace CppSharp @@ -38,7 +36,7 @@ namespace CppSharp
StringBuilder = new StringBuilder(generator);
IsStartOfLine = generator.IsStartOfLine;
NeedsNewLine = generator.NeedsNewLine;
CurrentIndentation = new Stack<uint>(generator.CurrentIndentation);
CurrentIndentation = generator.CurrentIndentation;
}
public TextGenerator Clone()
@ -55,7 +53,8 @@ namespace CppSharp @@ -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 @@ -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()

13
src/Generator/Utils/Utils.cs

@ -128,17 +128,4 @@ namespace CppSharp @@ -128,17 +128,4 @@ namespace CppSharp
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