Skip to content

Commit d6ed917

Browse files
committed
Keeping property on the first line
1 parent fd3865b commit d6ed917

7 files changed

Lines changed: 63 additions & 68 deletions

File tree

Src/CSharpier.Core/CSharp/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,21 @@ public static Doc PrintMemberChain(ExpressionSyntax node, PrintingContext contex
2828

2929
var shouldMergeFirstTwoGroups = ShouldMergeFirstTwoGroups(groups, parent);
3030

31-
var cutoff = shouldMergeFirstTwoGroups ? 3 : 2;
32-
3331
var forceOneLine =
3432
(
35-
groups.Count <= cutoff
33+
groups.Count <= 2
3634
&& (
37-
groups
38-
.Skip(shouldMergeFirstTwoGroups ? 1 : 0)
39-
.Any(o =>
40-
o.Last().Node
41-
is not (
42-
InvocationExpressionSyntax
43-
or ElementAccessExpressionSyntax
44-
or PostfixUnaryExpressionSyntax
45-
{
46-
Operand: InvocationExpressionSyntax
47-
}
48-
)
49-
)
35+
groups.Any(o =>
36+
o.Last().Node
37+
is not (
38+
InvocationExpressionSyntax
39+
or ElementAccessExpressionSyntax
40+
or PostfixUnaryExpressionSyntax
41+
{
42+
Operand: InvocationExpressionSyntax
43+
}
44+
)
45+
)
5046
// if the last group contains just a !, make sure it doesn't end up on a new line
5147
|| (
5248
groups.Last().Count == 1
@@ -355,6 +351,14 @@ private static bool ShouldMergeFirstTwoGroups(
355351
return false;
356352
}
357353

354+
if (
355+
groups[1]
356+
.None(o => o.Node is InvocationExpressionSyntax or ElementAccessExpressionSyntax)
357+
)
358+
{
359+
return true;
360+
}
361+
358362
var firstNode = groups[0][0].Node;
359363

360364
if (
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace CSharpier.Core.Utilities;
2+
3+
internal static class IEnumerableExtensions
4+
{
5+
public static bool None<TSource>(
6+
this IEnumerable<TSource> source,
7+
Func<TSource, bool> predicate
8+
)
9+
{
10+
return !source.Any(predicate);
11+
}
12+
}

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/BinaryExpressions.test

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ class TestClass
201201
) ?? someOtherValue;
202202

203203
var x =
204-
someValue
205-
.Property
204+
someValue.Property
206205
.CallLongMethod_____________________________________()
207206
.CallMethod(
208207
someLongValue__________________________________________,
@@ -274,8 +273,7 @@ class TestClass
274273
someLongValue__________________________________________,
275274
someLongValue__________________________________________
276275
)
277-
)
278-
.SomeProperty
276+
).SomeProperty
279277
.CallMethod() ?? someOtherValue;
280278

281279
var x =
@@ -284,8 +282,7 @@ class TestClass
284282
someLongValue__________________________________________,
285283
someLongValue__________________________________________
286284
)
287-
)
288-
?.SomeProperty
285+
)?.SomeProperty
289286
.CallMethod() ?? someOtherValue;
290287

291288
var x =
@@ -294,8 +291,7 @@ class TestClass
294291
someLongValue__________________________________________,
295292
someLongValue__________________________________________
296293
)
297-
)
298-
?.SomeProperty
294+
)?.SomeProperty
299295
?.CallMethod() ?? someOtherValue;
300296

301297
var x =
@@ -304,8 +300,7 @@ class TestClass
304300
someLongValue__________________________________________,
305301
someLongValue__________________________________________
306302
)
307-
)
308-
.SomeProperty
303+
).SomeProperty
309304
.CallMethod()
310305
.CallMethod()
311306
?? someOtherValue;
@@ -316,8 +311,7 @@ class TestClass
316311
someLongValue__________________________________________,
317312
someLongValue__________________________________________
318313
)
319-
)
320-
?.SomeProperty
314+
)?.SomeProperty
321315
.CallMethod()
322316
.CallMethod()
323317
?? someOtherValue;
@@ -355,22 +349,19 @@ class TestClass
355349
) ?? someOtherValue;
356350

357351
var x =
358-
someValue
359-
.Property
352+
someValue.Property
360353
.CallLongMethod_____________________________________()
361354
.CallMethod__________()
362355
?? throw new Exception();
363356

364357
var x =
365-
someValue
366-
.Property
358+
someValue.Property
367359
.CallLongMethod_____________________________________()
368360
.CallMethod__________(someParameter)
369361
?? throw new Exception();
370362

371363
var x =
372-
someValue
373-
.Property
364+
someValue.Property
374365
.CallLongMethod_____________________________________()
375366
.CallLongMethod___________________________________________________()
376367
?? throw new Exception();

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChain_PropertiesConsistent.test

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
var someVariable = someObject____________
2-
.Property_______________
1+
var someVariable = someObject____________.Property_______________
32
.CallMethod______________________();
43

5-
var someVariable = someObject
6-
.Property
4+
var someVariable = someObject.Property
75
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________);
86

97
var someVariable = someObject
10-
.Property()
8+
.CallMethod()
119
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________);
1210

13-
var someVariable = someObject
14-
.Property
11+
var someVariable = someObject.Property
1512
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________)
1613
.CallMethod();
1714

1815
var someVariable = someObject
19-
.Property()
16+
.CallMethod()
2017
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________)
2118
.CallMethod();
2219

23-
// TODO too hard to change this for now, will do it in https://github.com/belav/csharpier/issues/451
24-
var someVariable = this.Property.CallMethod(someValue =>
25-
someValue.SomeProperty == someOtherValue___________________________
26-
);
20+
var someVariable = this.Property
21+
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________);
2722

2823
var someVariable = this.CallMethod()
2924
.CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________);

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChains.test

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,11 @@ var someValue = someOtherValue!
4747
.Where(o => someLongCondition__________________________)
4848
.Where(o => someLongCondition__________________________);
4949

50-
var someValue = someOtherValue!
51-
.Thing!
50+
var someValue = someOtherValue!.Thing!
5251
.Where(o => someLongCondition__________________________)
5352
.Where(o => someLongCondition__________________________);
5453

55-
var someValue = someOtherValue
56-
.Thing
54+
var someValue = someOtherValue.Thing
5755
.Where(o => someLongCondition__________________________)
5856
.Where(o => someLongCondition__________________________);
5957

@@ -78,8 +76,7 @@ roleNames
7876
this.SomeProperty.Setup(o => longThing_______________________________________)
7977
);
8078

81-
roleNames
82-
.Value
79+
roleNames.Value
8380
.Where(o => o.SomeProperty____________________________________)
8481
.Select(o => o.SomethingElse);
8582

@@ -108,8 +105,7 @@ CallSomeMethod(
108105
someParameter____________________________________
109106
)!;
110107

111-
var someVariable = someObject
112-
.Property
108+
var someVariable = someObject.Property
113109
.CallMethod(someValue =>
114110
someValue.SomeProperty == someOtherValue___________________________________
115111
);
@@ -231,18 +227,15 @@ var someValue = CallMethod______________________(
231227
)
232228
.CallMethod__________________();
233229

234-
someThing_______________________
235-
.Property
230+
someThing_______________________.Property
236231
.CallMethod__________________()
237232
.CallMethod__________________();
238233

239-
someThing_______________________
240-
?.Property
234+
someThing_______________________?.Property
241235
.CallMethod__________________()
242236
.CallMethod__________________();
243237

244-
someThing_______________________
245-
.Property!
238+
someThing_______________________.Property!
246239
.CallMethod__________________()
247240
.CallMethod__________________();
248241

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ParenthesizedLambdaExpressions.test

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ public class ClassName
5151
.ThenAnotherMethod_____________________________________()
5252
);
5353

54-
var task = Task.Factory.StartNew(async () =>
55-
{
56-
return await new WebClient().DownloadStringTaskAsync____________________(
57-
"http://example.com"
58-
);
59-
});
54+
var task = Task.Factory
55+
.StartNew(async () =>
56+
{
57+
return await new WebClient().DownloadStringTaskAsync____________________(
58+
"http://example.com"
59+
);
60+
});
6061

6162
Action find = () =>
6263
EntryPointDiscoverer.FindStaticEntryMethod(typeof(IEnumerable<>).Assembly);

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/SpreadElements.test

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ int[] someOtherArray =
1010

1111
List<KeyValuePair<string, string>> list =
1212
[
13-
.. attribute
14-
.Targets
13+
.. attribute.Targets
1514
.Select(target =>
1615
KeyValuePair.Create(
1716
target,

0 commit comments

Comments
 (0)