@@ -34,11 +34,21 @@ public static bool IsSingleStatement(IList<string> blockLines)
3434
3535 var numberOfNonIndentedLines = blockLines [ 0 ]
3636 . SplitToLines ( )
37- . Count ( line => line . IsNotIndented ( ) && ! line . StartsWith ( '{' ) && ! line . StartsWith ( '}' ) ) ;
37+ . Count ( RelevantNonIndentedLine ) ;
3838
3939 return numberOfNonIndentedLines == 1 ;
4040 }
4141
42+ private CodeBlock (
43+ Expression expression ,
44+ string [ ] blockLines ,
45+ bool isASingleStatement )
46+ {
47+ _expression = expression ;
48+ _blockLines = blockLines ;
49+ IsASingleStatement = isASingleStatement ;
50+ }
51+
4252 public bool IsASingleStatement { get ; }
4353
4454 public string AsExpressionBody ( )
@@ -59,19 +69,26 @@ public CodeBlock Indented()
5969
6070 return new CodeBlock (
6171 _expression ,
62- _blockLines . Select ( line => line . Indented ( ) ) . ToArray ( ) ) ;
72+ _blockLines . Select ( line => line . Indented ( ) ) . ToArray ( ) ,
73+ IsASingleStatement ) ;
6374 }
6475
6576 public CodeBlock Insert ( params string [ ] lines )
6677 {
67- return new CodeBlock ( _expression , lines . Concat ( _blockLines ) . ToArray ( ) ) ;
78+ return new CodeBlock (
79+ _expression ,
80+ lines . Concat ( _blockLines ) . ToArray ( ) ,
81+ isASingleStatement : false ) ;
6882 }
6983
7084 public CodeBlock Append ( params string [ ] lines )
7185 {
7286 AddSemiColonIfRequired ( ) ;
7387
74- return new CodeBlock ( _expression , _blockLines . Concat ( lines ) . ToArray ( ) ) ;
88+ return new CodeBlock (
89+ _expression ,
90+ _blockLines . Concat ( lines ) . ToArray ( ) ,
91+ isASingleStatement : false ) ;
7592 }
7693
7794 public string WithCurlyBracesIfMultiStatement ( )
@@ -100,9 +117,7 @@ private static bool ExpressionHasReturn(Expression expression)
100117 {
101118 while ( true )
102119 {
103- var block = expression as BlockExpression ;
104-
105- if ( block == null )
120+ if ( ! ( expression is BlockExpression block ) )
106121 {
107122 return expression . NodeType == ExpressionType . Goto ;
108123 }
@@ -140,7 +155,10 @@ private CodeBlock WithReturn()
140155 return this ;
141156 }
142157
143- return new CodeBlock ( _expression , GetBlockLinesWithReturnKeyword ( _blockLines ) ) ;
158+ return new CodeBlock (
159+ _expression ,
160+ GetBlockLinesWithReturnKeyword ( _blockLines ) ,
161+ IsASingleStatement ) ;
144162 }
145163
146164 public static string InsertReturnKeyword ( string multiLineStatement )
@@ -166,10 +184,24 @@ private string GetCodeBlock()
166184
167185 private void AddSemiColonIfRequired ( )
168186 {
169- if ( IsASingleStatement && ! _blockLines [ 0 ] . IsTerminated ( ) )
187+ if ( ! IsASingleStatement )
170188 {
171- _blockLines [ 0 ] += ";" ;
189+ return ;
172190 }
191+
192+ var block = ( _blockLines . Length == 1 )
193+ ? _blockLines [ 0 ]
194+ : string . Join ( " " , _blockLines ) ;
195+
196+ if ( ! block . IsTerminated ( ) )
197+ {
198+ _blockLines [ _blockLines . Length - 1 ] += ";" ;
199+ }
200+ }
201+
202+ private static bool RelevantNonIndentedLine ( string line )
203+ {
204+ return line . IsNotIndented ( ) && ! line . StartsWith ( '{' ) && ! line . StartsWith ( '}' ) ;
173205 }
174206
175207 private class BlockInfo
@@ -185,7 +217,7 @@ public BlockInfo(string[] blockLines)
185217 _lastNonIndentedStatement = blockLines . Last ( line => line . IsNotIndented ( ) ) ;
186218
187219 _lastStatementLines = _lastNonIndentedStatement . SplitToLines ( ) ;
188- _lastNonIndentedLine = _lastStatementLines . Last ( line => line . IsNotIndented ( ) ) ;
220+ _lastNonIndentedLine = _lastStatementLines . Last ( RelevantNonIndentedLine ) ;
189221 }
190222
191223 public bool LastLineHasReturnKeyword =>
0 commit comments