@@ -22,16 +22,15 @@ public override string Translate(Expression expression, TranslationContext conte
2222 var block = ( BlockExpression ) expression ;
2323
2424 var variables = GetVariableDeclarations ( block , context ) ;
25- var lines = GetBlockLines ( block , context ) ;
26-
27- lines = ProcessBlockContents ( lines . ToArray ( ) , block ) ;
25+ var statements = GetBlockStatements ( block , context ) . ToArray ( ) ;
26+ var separator = GetStatementsSeparator ( variables , statements ) ;
2827
29- var blockContents = variables . Concat ( lines ) ;
28+ var blockContents = variables . Concat ( separator ) . Concat ( statements ) ;
3029
3130 return string . Join ( Environment . NewLine , blockContents ) ;
3231 }
3332
34- private IEnumerable < string > GetVariableDeclarations (
33+ private IList < string > GetVariableDeclarations (
3534 BlockExpression block ,
3635 TranslationContext context )
3736 {
@@ -44,10 +43,66 @@ private IEnumerable<string> GetVariableDeclarations(
4443 TypeName = vGrp . Key . GetFriendlyName ( ) ,
4544 VariableNames = vGrp . Select ( varName => _variableNameTranslator . Translate ( varName ) )
4645 } )
47- . Select ( varData => $ "{ varData . TypeName } { string . Join ( ", " , varData . VariableNames ) } ;") ;
46+ . Select ( varData => $ "{ varData . TypeName } { string . Join ( ", " , varData . VariableNames ) } ;")
47+ . ToArray ( ) ;
4848 }
4949
50- private static IEnumerable < string > GetBlockLines ( BlockExpression block , TranslationContext context )
50+ private static IEnumerable < string > GetStatementsSeparator (
51+ ICollection < string > variables ,
52+ IList < string > statements )
53+ {
54+ if ( ( variables . Count > 0 ) && LeaveBlankLineBefore ( statements [ 0 ] ) )
55+ {
56+ yield return string . Empty ;
57+ }
58+ }
59+
60+ private static IEnumerable < string > GetBlockStatements (
61+ BlockExpression block ,
62+ TranslationContext context )
63+ {
64+ var lines = GetBlockLines ( block , context ) ;
65+
66+ var finalLineIndex = lines . Count - 1 ;
67+
68+ for ( var i = 0 ; i < lines . Count ; i ++ )
69+ {
70+ var line = lines [ i ] ;
71+
72+ if ( ( i > 0 ) && LeaveBlankLineBefore ( line ) )
73+ {
74+ yield return string . Empty ;
75+ }
76+
77+ if ( i != finalLineIndex )
78+ {
79+ yield return line ;
80+
81+ if ( LeaveBlankLineAfter ( line , lines [ i + 1 ] ) )
82+ {
83+ yield return string . Empty ;
84+ }
85+
86+ continue ;
87+ }
88+
89+ if ( DoNotAddReturnStatement ( block , lines ) )
90+ {
91+ yield return line ;
92+ yield break ;
93+ }
94+
95+ if ( CodeBlock . IsSingleStatement ( line . SplitToLines ( ) ) )
96+ {
97+ yield return "return " + line ;
98+ yield break ;
99+ }
100+
101+ yield return CodeBlock . InsertReturnKeyword ( line ) ;
102+ }
103+ }
104+
105+ private static IList < string > GetBlockLines ( BlockExpression block , TranslationContext context )
51106 {
52107 return block
53108 . Expressions
@@ -58,7 +113,8 @@ private static IEnumerable<string> GetBlockLines(BlockExpression block, Translat
58113 Translation = GetTerminatedStatementOrNull ( exp , context )
59114 } )
60115 . Where ( d => d . Translation != null )
61- . Select ( d => d . Translation ) ;
116+ . Select ( d => d . Translation )
117+ . ToArray ( ) ;
62118 }
63119
64120 private static bool Include ( Expression expression )
@@ -135,40 +191,9 @@ private static bool UseFullTypeName(BinaryExpression assignment)
135191 return conditional . IfTrue . Type != conditional . IfFalse . Type ;
136192 }
137193
138- private static IEnumerable < string > ProcessBlockContents ( IList < string > lines , BlockExpression block )
194+ private static bool LeaveBlankLineBefore ( string line )
139195 {
140- var finalLineIndex = lines . Count - 1 ;
141-
142- for ( var i = 0 ; i < lines . Count ; i ++ )
143- {
144- var line = lines [ i ] ;
145-
146- if ( i != finalLineIndex )
147- {
148- yield return line ;
149-
150- if ( LeaveBlankLineAfter ( line , lines [ i + 1 ] ) )
151- {
152- yield return string . Empty ;
153- }
154-
155- continue ;
156- }
157-
158- if ( DoNotAddReturnStatement ( block , lines ) )
159- {
160- yield return line ;
161- yield break ;
162- }
163-
164- if ( CodeBlock . IsSingleStatement ( line . SplitToLines ( ) ) )
165- {
166- yield return "return " + line ;
167- yield break ;
168- }
169-
170- yield return CodeBlock . InsertReturnKeyword ( line ) ;
171- }
196+ return line . StartsWith ( "if (" ) || line . StartsWith ( "switch " ) ;
172197 }
173198
174199 private static bool LeaveBlankLineAfter ( string line , string nextLine )
@@ -179,12 +204,7 @@ private static bool LeaveBlankLineAfter(string line, string nextLine)
179204
180205 private static bool IsMultiLineStatement ( string line )
181206 {
182- if ( ! line . Contains ( Environment . NewLine ) )
183- {
184- return false ;
185- }
186-
187- return line
207+ return line . IsMultiLine ( ) && line
188208 . SplitToLines ( StringSplitOptions . RemoveEmptyEntries )
189209 . Any ( l => ! l . IsTerminated ( ) ) ;
190210 }
0 commit comments