-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
InvocationExpression.cs
382 lines (341 loc) · 12.4 KB
/
InvocationExpression.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters;
internal record PrintedNode(CSharpSyntaxNode Node, Doc Doc);
// This is based on prettier/src/language-js/print/member-chain.js
// various discussions/prs about how to potentially improve the formatting
// https://github.com/prettier/prettier/issues/5737
// https://github.com/prettier/prettier/issues/7884
// https://github.com/prettier/prettier/issues/8003
// https://github.com/prettier/prettier/issues/8902
// https://github.com/prettier/prettier/pull/7889
internal static class InvocationExpression
{
public static Doc Print(InvocationExpressionSyntax node, FormattingContext context)
{
return PrintMemberChain(node, context);
}
public static Doc PrintMemberChain(ExpressionSyntax node, FormattingContext context)
{
var parent = node.Parent;
var printedNodes = new List<PrintedNode>();
FlattenAndPrintNodes(node, printedNodes, context);
var groups = printedNodes.Any(o => o.Node is InvocationExpressionSyntax)
? GroupPrintedNodesPrettierStyle(printedNodes)
: GroupPrintedNodesOnLines(printedNodes);
var oneLine = groups.SelectMany(o => o).Select(o => o.Doc).ToArray();
var shouldMergeFirstTwoGroups = ShouldMergeFirstTwoGroups(groups, parent);
var cutoff = shouldMergeFirstTwoGroups ? 3 : 2;
var forceOneLine =
groups.Count <= cutoff
&& (
groups
.Skip(shouldMergeFirstTwoGroups ? 1 : 0)
.Any(o =>
o.Last().Node
is not (
InvocationExpressionSyntax
or ElementAccessExpressionSyntax
or PostfixUnaryExpressionSyntax
{
Operand: InvocationExpressionSyntax
}
)
)
// if the last group contains just a !, make sure it doesn't end up on a new line
|| (
groups.Last().Count == 1
&& groups.Last()[0].Node is PostfixUnaryExpressionSyntax
)
);
if (forceOneLine)
{
return Doc.Group(oneLine);
}
var expanded = Doc.Concat(
Doc.Concat(groups[0].Select(o => o.Doc).ToArray()),
shouldMergeFirstTwoGroups
? Doc.Concat(groups[1].Select(o => o.Doc).ToArray())
: Doc.Null,
PrintIndentedGroup(node, groups.Skip(shouldMergeFirstTwoGroups ? 2 : 1).ToList())
);
return
oneLine.Skip(1).Any(DocUtilities.ContainsBreak)
|| groups[0].Any(o =>
o.Node
is ArrayCreationExpressionSyntax
or ObjectCreationExpressionSyntax { Initializer: not null }
)
? expanded
: Doc.ConditionalGroup(Doc.Concat(oneLine), expanded);
}
private static void FlattenAndPrintNodes(
ExpressionSyntax expression,
List<PrintedNode> printedNodes,
FormattingContext context
)
{
/*
We need to flatten things out because the AST has them this way
InvocationExpression
Expression ArgumentList
this.DoSomething().DoSomething ()
MemberAccessExpression
Expression OperatorToken Name
this.DoSomething() . DoSomething
InvocationExpression
Expression ArgumentList
this.DoSomething ()
MemberAccessExpression
Expression OperatorToken Name
this . DoSomething
And we want to work with them from Left to Right
*/
if (expression is InvocationExpressionSyntax invocationExpressionSyntax)
{
FlattenAndPrintNodes(invocationExpressionSyntax.Expression, printedNodes, context);
printedNodes.Add(
new PrintedNode(
invocationExpressionSyntax,
ArgumentList.Print(invocationExpressionSyntax.ArgumentList, context)
)
);
}
else if (expression is ElementAccessExpressionSyntax elementAccessExpression)
{
FlattenAndPrintNodes(elementAccessExpression.Expression, printedNodes, context);
printedNodes.Add(
new PrintedNode(
elementAccessExpression,
Node.Print(elementAccessExpression.ArgumentList, context)
)
);
}
else if (expression is MemberAccessExpressionSyntax memberAccessExpressionSyntax)
{
FlattenAndPrintNodes(memberAccessExpressionSyntax.Expression, printedNodes, context);
printedNodes.Add(
new PrintedNode(
memberAccessExpressionSyntax,
Doc.Concat(
Token.Print(memberAccessExpressionSyntax.OperatorToken, context),
Node.Print(memberAccessExpressionSyntax.Name, context)
)
)
);
}
else if (expression is ConditionalAccessExpressionSyntax conditionalAccessExpressionSyntax)
{
FlattenAndPrintNodes(
conditionalAccessExpressionSyntax.Expression,
printedNodes,
context
);
printedNodes.Add(
new PrintedNode(
conditionalAccessExpressionSyntax,
Token.Print(conditionalAccessExpressionSyntax.OperatorToken, context)
)
);
FlattenAndPrintNodes(
conditionalAccessExpressionSyntax.WhenNotNull,
printedNodes,
context
);
}
else if (
expression is PostfixUnaryExpressionSyntax
{
Operand: InvocationExpressionSyntax or MemberAccessExpressionSyntax
} postfixUnaryExpression
)
{
FlattenAndPrintNodes(postfixUnaryExpression.Operand, printedNodes, context);
printedNodes.Add(
new PrintedNode(
postfixUnaryExpression,
Token.Print(postfixUnaryExpression.OperatorToken, context)
)
);
}
else
{
printedNodes.Add(new PrintedNode(expression, Node.Print(expression, context)));
}
}
private static List<List<PrintedNode>> GroupPrintedNodesOnLines(List<PrintedNode> printedNodes)
{
// We want to group the printed nodes in the following manner
//
// a?.b.c!.d
// so that we can print it like this if it breaks
// a
// ?.b
// .c!
// .d
var groups = new List<List<PrintedNode>>();
var currentGroup = new List<PrintedNode> { printedNodes[0] };
groups.Add(currentGroup);
for (var index = 1; index < printedNodes.Count; index++)
{
if (printedNodes[index].Node is ConditionalAccessExpressionSyntax)
{
currentGroup = [];
groups.Add(currentGroup);
}
else if (
printedNodes[index].Node
is MemberAccessExpressionSyntax
or MemberBindingExpressionSyntax
or IdentifierNameSyntax
&& printedNodes[index + -1].Node is not ConditionalAccessExpressionSyntax
)
{
currentGroup = [];
groups.Add(currentGroup);
}
currentGroup.Add(printedNodes[index]);
}
return groups;
}
private static List<List<PrintedNode>> GroupPrintedNodesPrettierStyle(
List<PrintedNode> printedNodes
)
{
// We want to group the printed nodes in the following manner
//
// a().b.c().d().e
// will be grouped as
// [
// [Identifier, InvocationExpression],
// [MemberAccessExpression], [MemberAccessExpression, InvocationExpression],
// [MemberAccessExpression, InvocationExpression],
// [MemberAccessExpression],
// ]
// so that we can print it as
// a()
// .b.c()
// .d()
// .e
// TODO #451 this whole thing could possibly just turn into a big loop
// based on the current node, and the next/previous node, decide when to create new groups.
// certain nodes need to stay in the current group, other nodes indicate that a new group needs to be created.
var groups = new List<List<PrintedNode>>();
var currentGroup = new List<PrintedNode> { printedNodes[0] };
var index = 1;
for (; index < printedNodes.Count; index++)
{
if (printedNodes[index].Node is InvocationExpressionSyntax)
{
currentGroup.Add(printedNodes[index]);
}
else
{
break;
}
}
if (
printedNodes[0].Node is not (InvocationExpressionSyntax or PostfixUnaryExpressionSyntax)
&& index < printedNodes.Count
&& printedNodes[index].Node
is ElementAccessExpressionSyntax
or PostfixUnaryExpressionSyntax
)
{
currentGroup.Add(printedNodes[index]);
index++;
}
groups.Add(currentGroup);
currentGroup = [];
var hasSeenNodeThatRequiresBreak = false;
for (; index < printedNodes.Count; index++)
{
if (
hasSeenNodeThatRequiresBreak
&& printedNodes[index].Node
is MemberAccessExpressionSyntax
or ConditionalAccessExpressionSyntax
)
{
groups.Add(currentGroup);
currentGroup = [];
hasSeenNodeThatRequiresBreak = false;
}
if (
printedNodes[index].Node
is (InvocationExpressionSyntax or ElementAccessExpressionSyntax)
)
{
hasSeenNodeThatRequiresBreak = true;
}
currentGroup.Add(printedNodes[index]);
}
if (currentGroup.Any())
{
groups.Add(currentGroup);
}
return groups;
}
private static Doc PrintIndentedGroup(ExpressionSyntax node, IList<List<PrintedNode>> groups)
{
if (groups.Count == 0)
{
return Doc.Null;
}
return Doc.Indent(
Doc.Group(
Doc.HardLine,
Doc.Join(
Doc.HardLine,
groups.Select(o => Doc.Group(o.Select(p => p.Doc).ToArray()))
)
)
);
}
// There are cases where merging the first two groups looks better
// For example
/*
// without merging we get this
this
.CallMethod()
.CallMethod();
// merging gives us this
this.CallMethod()
.CallMethod();
*/
private static bool ShouldMergeFirstTwoGroups(
List<List<PrintedNode>> groups,
SyntaxNode? parent
)
{
if (groups.Count < 2 || groups[0].Count != 1)
{
return false;
}
var firstNode = groups[0][0].Node;
if (
firstNode
is not (
IdentifierNameSyntax { Identifier.Text.Length: <= 4 }
or ThisExpressionSyntax
or PredefinedTypeSyntax
or BaseExpressionSyntax
)
)
{
return false;
}
// TODO maybe some things to fix in here
// https://github.com/belav/csharpier-repos/pull/100/files
if (
groups[1].Count == 1
|| parent is SimpleLambdaExpressionSyntax or ArgumentSyntax or BinaryExpressionSyntax
|| groups[1].Skip(1).First().Node
is InvocationExpressionSyntax
or ElementAccessExpressionSyntax
or PostfixUnaryExpressionSyntax
)
{
return true;
}
return false;
}
}