Skip to content

Commit 4adfded

Browse files
committed
Splitting parameter lists with more than 3 parameters onto multiple lines
1 parent 76cfcaf commit 4adfded

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

ReadableExpressions.UnitTests/WhenFormattingCode.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public void ShouldSplitLongConstructorArgumentListsOntoMultipleLines()
1717
var helperConstructor = helperVariable.Type.GetConstructors().First();
1818
var longVariable = Expression.Variable(typeof(int), "thisVariableReallyHasAVeryLongNameIndeed");
1919
var newHelper = Expression.New(helperConstructor, longVariable, longVariable, longVariable);
20-
var assignHelper = Expression.Assign(helperVariable, newHelper);
20+
var helperAssignment = Expression.Assign(helperVariable, newHelper);
2121

22-
var longArgumentListBlock = Expression.Block(new[] { helperVariable }, assignHelper);
22+
var longArgumentListBlock = Expression.Block(new[] { helperVariable }, helperAssignment);
2323

2424
var translated = longArgumentListBlock.ToReadableString();
2525

@@ -32,6 +32,35 @@ public void ShouldSplitLongConstructorArgumentListsOntoMultipleLines()
3232
Assert.AreEqual(EXPECTED.TrimStart(), translated);
3333
}
3434

35+
[TestMethod]
36+
public void ShouldSplitMultipleArgumentListsOntoMultipleLines()
37+
{
38+
var intsMethod = typeof(HelperClass)
39+
.GetMethod("GiveMeFourInts", BindingFlags.Public | BindingFlags.Instance);
40+
41+
var helperVariable = Expression.Variable(typeof(HelperClass), "helper");
42+
var intVariable = Expression.Variable(typeof(int), "intVariable");
43+
44+
var intsMethodCall = Expression.Call(
45+
helperVariable,
46+
intsMethod,
47+
intVariable,
48+
intVariable,
49+
intVariable,
50+
intVariable);
51+
52+
var translated = intsMethodCall.ToReadableString();
53+
54+
const string EXPECTED = @"
55+
helper.GiveMeFourInts(
56+
intVariable,
57+
intVariable,
58+
intVariable,
59+
intVariable)";
60+
61+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
62+
}
63+
3564
[TestMethod]
3665
public void ShouldSplitLongArgumentListsOntoMultipleLines()
3766
{
@@ -542,6 +571,10 @@ public HelperClass(int intOne, int intTwo, int intThree)
542571
public void GiveMeSomeInts(int intOne, int intTwo, int intThree)
543572
{
544573
}
574+
575+
public void GiveMeFourInts(int intOne, int intTwo, int intThree, int intFour)
576+
{
577+
}
545578
}
546579
// ReSharper restore UnusedParameter.Local
547580

ReadableExpressions/Translators/Formatting/FormattableExpressionBase.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ protected string GetFormattedTranslation()
1717
{
1818
var translation = SingleLineTranslationFactory.Invoke();
1919

20-
return (translation.Length > 100) || translation.IsMultiLine()
20+
return SplitToMultipleLines(translation)
2121
? MultipleLineTranslationFactory.Invoke()
2222
: translation;
2323
}
2424

25+
protected virtual bool SplitToMultipleLines(string translation)
26+
{
27+
return (translation.Length > 100) || translation.IsMultiLine();
28+
}
29+
2530
public override string ToString()
2631
{
2732
return GetFormattedTranslation();

ReadableExpressions/Translators/Formatting/ParameterSet.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ private Func<Expression, string> MethodGroupTranslator
186186
}
187187
}
188188

189+
protected override bool SplitToMultipleLines(string translation)
190+
{
191+
return (_arguments.Count() > 3) || base.SplitToMultipleLines(translation);
192+
}
193+
189194
protected override Func<string> SingleLineTranslationFactory => () => FormatParameters(", ");
190195

191196
protected override Func<string> MultipleLineTranslationFactory

0 commit comments

Comments
 (0)