@@ -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
0 commit comments