Skip to content

Commit b64d905

Browse files
committed
LuaTableTextWriter: Indension support.
1 parent bcc4fed commit b64d905

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

Luaon.NET/LuaTableTextWriter.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ private enum Token
3939
private readonly List<LuaContainerContext> contextStack;
4040
private LuaContainerContext currentContext;
4141
private State currentState = State.Start;
42+
private int _Indentation;
4243

4344
// NextState[State][Token]
4445
private static readonly State[][] NextStateTable =
@@ -60,6 +61,8 @@ public LuaTableTextWriter(TextWriter writer)
6061
currentContext = new LuaContainerContext(LuaContainerType.None);
6162
contextStack = new List<LuaContainerContext>();
6263
CloseWriter = true;
64+
Indentation = 2;
65+
IndentChar = ' ';
6366
}
6467

6568
/// <summary>
@@ -85,6 +88,26 @@ public Formatting Formatting
8588
}
8689
}
8790

91+
/// <summary>
92+
/// Gets or sets how many <see cref="IndentChar"/>s to write for each level in the hierarchy
93+
/// when <see cref="Formatting"/> is set to <see cref="Formatting.Prettified"/>.
94+
/// </summary>
95+
public int Indentation
96+
{
97+
get { return _Indentation; }
98+
set
99+
{
100+
if (value < 0) throw new ArgumentOutOfRangeException(nameof(value));
101+
_Indentation = value;
102+
}
103+
}
104+
105+
/// <summary>
106+
/// Gets/sets which character to use for indenting
107+
/// when <see cref="Formatting"/> is set to <see cref="Formatting.Prettified"/>.
108+
/// </summary>
109+
public char IndentChar { get; set; }
110+
88111
private void GotoNextState(Token token)
89112
{
90113
var next = NextStateTable[(int)currentState][(int)token];
@@ -157,8 +180,14 @@ public virtual void WriteEndTable()
157180
{
158181
AssertContainerType(LuaContainerType.Table);
159182
GotoNextState(Token.TableEnd);
160-
Writer.Write('}');
161183
Pop();
184+
if ((Formatting & Formatting.Prettified) == Formatting.Prettified)
185+
{
186+
Writer.WriteLine();
187+
WriteIndentation();
188+
}
189+
190+
Writer.Write('}');
162191
}
163192

164193
/// <summary>
@@ -246,6 +275,12 @@ public virtual void WriteKey(object key)
246275
currentContext.KeyIsExpression = true;
247276
}
248277

278+
protected virtual void WriteIndentation()
279+
{
280+
var chars = Indentation * contextStack.Count;
281+
for (int i = 0; i < chars; i++) Writer.Write(IndentChar);
282+
}
283+
249284
public virtual void WriteFieldDelimiter()
250285
{
251286
// Can be , or ;
@@ -256,8 +291,10 @@ private void DelimitLastValue(Token nextToken)
256291
{
257292
var s = currentState;
258293
GotoNextState(nextToken);
294+
259295
if (s == State.FieldStart)
260296
{
297+
// In the middle of a table.
261298
if (currentContext.Key != null)
262299
{
263300
currentContext.Key = null;
@@ -266,8 +303,18 @@ private void DelimitLastValue(Token nextToken)
266303
{
267304
currentContext.CurrentIndex++;
268305
}
306+
269307
WriteFieldDelimiter();
270308
}
309+
310+
if (nextToken != Token.TableEnd && (s == State.TableStart || s == State.FieldStart))
311+
{
312+
if ((Formatting & Formatting.Prettified) == Formatting.Prettified)
313+
{
314+
Writer.WriteLine();
315+
WriteIndentation();
316+
}
317+
}
271318
}
272319

273320
#region WriteLiteral overloads

XUnitTestProject1/Tests/LinqTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using Luaon;
45
using Luaon.Linq;
56
using Xunit;
67
using Xunit.Abstractions;
@@ -46,8 +47,8 @@ public void LTableTest()
4647
Assert.Equal(1, table["Child"][1]);
4748
Assert.Equal(2, table["Child"][2]);
4849
Assert.Equal(3, table["Child"][3]);
49-
var s = table.ToString();
50-
Output.WriteLine(s);
50+
Assert.Equal("{[\"Test1\"]=1,2,[\"Test2\"]=3,4.5,[20]=5,[true]=\"6\",[\"Child\"]={1,2,3,4,5}}",
51+
table.ToString(Formatting.None));
5152
}
5253

5354

0 commit comments

Comments
 (0)