Skip to content

Commit 0ccc170

Browse files
committed
Improving translation of method calls with a single, multi-line code block parameter
1 parent 54eb589 commit 0ccc170

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

ReadableExpressions.UnitTests/WhenFormattingCode.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,55 @@ public void ShouldLeaveABlankLineAfterAMultipleLineExpression()
637637

638638
Assert.AreEqual(EXPECTED.TrimStart(), translated);
639639
}
640+
641+
[TestMethod]
642+
public void ShouldTranslateMultilineBlockSingleMethodArguments()
643+
{
644+
var intVariable = Expression.Variable(typeof(int), "i");
645+
var variableInit = Expression.Assign(intVariable, Expression.Constant(3));
646+
var variableMultiplyFive = Expression.Multiply(intVariable, Expression.Constant(5));
647+
var variableAdditionOne = Expression.Assign(intVariable, variableMultiplyFive);
648+
var variableDivideThree = Expression.Divide(intVariable, Expression.Constant(3));
649+
var variableAdditionTwo = Expression.Assign(intVariable, variableDivideThree);
650+
651+
var argumentBlock = Expression.Block(
652+
new[] { intVariable },
653+
variableInit,
654+
variableAdditionOne,
655+
variableAdditionTwo,
656+
intVariable);
657+
658+
var catchBlock = Expression.Catch(
659+
typeof(Exception),
660+
Expression.Block(ReadableExpression.Comment("So what!"), Expression.Constant(0)));
661+
662+
var tryCatch = Expression.TryCatch(argumentBlock, catchBlock);
663+
664+
var collectionVariable = Expression.Variable(typeof(ICollection<int>), "ints");
665+
var addMethod = collectionVariable.Type.GetMethod("Add");
666+
var addMethodCall = Expression.Call(collectionVariable, addMethod, tryCatch);
667+
668+
const string EXPECTED = @"
669+
ints.Add(
670+
{
671+
try
672+
{
673+
var i = 3;
674+
i = i * 5;
675+
i = i / 3;
676+
return i;
677+
}
678+
catch
679+
{
680+
// So what!
681+
return 0;
682+
}
683+
})";
684+
685+
var translated = addMethodCall.ToReadableString();
686+
687+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
688+
}
640689
}
641690

642691
#region Helper Classes

ReadableExpressions/Translators/Formatting/ParameterSet.cs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,29 +211,59 @@ protected override Func<string> MultipleLineTranslationFactory
211211
{
212212
return () =>
213213
{
214-
var hasSingleLambdaArgument = HasSingleLambdaArgument();
215-
var prefix = hasSingleLambdaArgument ? null : Environment.NewLine;
214+
bool hasSingleBlockArgument, hasSingleLambdaArgument;
216215

217-
var parameters = FormatParameters("," + Environment.NewLine, a => a.Indented());
216+
if (_arguments.Length == 1)
217+
{
218+
hasSingleBlockArgument = IsSingleArgumentABlock();
219+
hasSingleLambdaArgument = !hasSingleBlockArgument && IsSingleArgumentALambda();
220+
}
221+
else
222+
{
223+
hasSingleBlockArgument = hasSingleLambdaArgument = false;
224+
}
225+
226+
var parameters = FormatParameters(
227+
"," + Environment.NewLine,
228+
a =>
229+
{
230+
hasSingleBlockArgument = hasSingleBlockArgument && a.IsMultiLine();
231+
232+
return hasSingleBlockArgument ? a : a.Indented();
233+
});
234+
235+
if (hasSingleBlockArgument)
236+
{
237+
return parameters;
238+
}
218239

219240
if (hasSingleLambdaArgument)
220241
{
221-
parameters = parameters.TrimStart();
242+
return parameters.TrimStart();
222243
}
223244

224-
return prefix + parameters;
245+
return Environment.NewLine + parameters;
225246
};
226247
}
227248
}
228249

229-
private bool HasSingleLambdaArgument()
250+
private bool IsSingleArgumentABlock()
230251
{
231-
if (_arguments.Length != 1)
252+
switch (_arguments[0].NodeType)
232253
{
233-
return false;
254+
case ExpressionType.Block:
255+
case ExpressionType.Switch:
256+
case ExpressionType.Conditional:
257+
case ExpressionType.Try:
258+
return true;
234259
}
235260

236-
var argument = _arguments.First();
261+
return false;
262+
}
263+
264+
private bool IsSingleArgumentALambda()
265+
{
266+
var argument = _arguments[0];
237267

238268
if (argument.NodeType != ExpressionType.Lambda)
239269
{

0 commit comments

Comments
 (0)