prettier-plugin-java 2.6.3 → 2.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,614 @@
1
+ import forEach from "lodash/forEach.js";
2
+ import { builders, utils } from "prettier/doc";
3
+ import { BaseCstPrettierPrinter } from "../base-cst-printer.js";
4
+ import { isAnnotationCstNode } from "../types/utils.js";
5
+ import { printArgumentListWithBraces } from "../utils/index.js";
6
+ import { printTokenWithComments } from "./comments/format-comments.js";
7
+ import { handleCommentsBinaryExpression, handleCommentsParameters } from "./comments/handle-comments.js";
8
+ import { concat, dedent, group, indent, indentIfBreak } from "./prettier-builder.js";
9
+ import { binary, findDeepElementInPartsArray, getOperators, isExplicitLambdaParameter, isUniqueMethodInvocation, putIntoBraces, rejectAndConcat, rejectAndJoin, rejectAndJoinSeps, sortAnnotationIdentifier, sortNodes, sortTokens } from "./printer-utils.js";
10
+ const { breakParent, conditionalGroup, ifBreak, label, line, lineSuffixBoundary, softline } = builders;
11
+ const { removeLines, willBreak } = utils;
12
+ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
13
+ expression(ctx, params) {
14
+ const expression = this.visitSingle(ctx, params);
15
+ return (params === null || params === void 0 ? void 0 : params.hug) && expression.label !== undefined
16
+ ? label(expression.label, expression)
17
+ : expression;
18
+ }
19
+ lambdaExpression(ctx, params) {
20
+ const lambdaParameters = group(this.visit(ctx.lambdaParameters));
21
+ const lambdaBody = this.visit(ctx.lambdaBody);
22
+ const isLambdaBodyABlock = ctx.lambdaBody[0].children.block !== undefined;
23
+ const suffix = [
24
+ " ",
25
+ ctx.Arrow[0],
26
+ ...(isLambdaBodyABlock
27
+ ? [" ", lambdaBody]
28
+ : [group(indent([line, lambdaBody]))])
29
+ ];
30
+ if (params === null || params === void 0 ? void 0 : params.hug) {
31
+ return willBreak(lambdaParameters)
32
+ ? label({ huggable: false }, concat([lambdaParameters, ...suffix]))
33
+ : concat([removeLines(lambdaParameters), ...suffix]);
34
+ }
35
+ return concat([lambdaParameters, ...suffix]);
36
+ }
37
+ lambdaParameters(ctx) {
38
+ if (ctx.lambdaParametersWithBraces) {
39
+ return this.visitSingle(ctx);
40
+ }
41
+ return printTokenWithComments(this.getSingle(ctx));
42
+ }
43
+ lambdaParametersWithBraces(ctx) {
44
+ var _a, _b, _c, _d, _e, _f;
45
+ const lambdaParameters = (_f = (_c = (_b = (_a = ctx.lambdaParameterList) === null || _a === void 0 ? void 0 : _a[0].children.normalLambdaParameterList) === null || _b === void 0 ? void 0 : _b[0].children.normalLambdaParameter) !== null && _c !== void 0 ? _c : (_e = (_d = ctx.lambdaParameterList) === null || _d === void 0 ? void 0 : _d[0].children.conciseLambdaParameterList) === null || _e === void 0 ? void 0 : _e[0].children.conciseLambdaParameter) !== null && _f !== void 0 ? _f : [];
46
+ handleCommentsParameters(ctx.LBrace[0], lambdaParameters, ctx.RBrace[0]);
47
+ const lambdaParameterList = this.visit(ctx.lambdaParameterList);
48
+ if (findDeepElementInPartsArray(lambdaParameterList, ",")) {
49
+ return concat([
50
+ ctx.LBrace[0],
51
+ indent([softline, lambdaParameterList]),
52
+ softline,
53
+ ctx.RBrace[0]
54
+ ]);
55
+ }
56
+ // removing braces when only no comments attached
57
+ if ((ctx.LBrace &&
58
+ ctx.RBrace &&
59
+ (!lambdaParameterList || isExplicitLambdaParameter(ctx))) ||
60
+ ctx.LBrace[0].leadingComments ||
61
+ ctx.LBrace[0].trailingComments ||
62
+ ctx.RBrace[0].leadingComments ||
63
+ ctx.RBrace[0].trailingComments) {
64
+ return rejectAndConcat([
65
+ ctx.LBrace[0],
66
+ lambdaParameterList,
67
+ ctx.RBrace[0]
68
+ ]);
69
+ }
70
+ return lambdaParameterList;
71
+ }
72
+ lambdaParameterList(ctx) {
73
+ return this.visitSingle(ctx);
74
+ }
75
+ conciseLambdaParameterList(ctx) {
76
+ var _a;
77
+ const conciseLambdaParameters = this.mapVisit(ctx.conciseLambdaParameter);
78
+ const commas = (_a = ctx.Comma) === null || _a === void 0 ? void 0 : _a.map(comma => concat([comma, line]));
79
+ return rejectAndJoinSeps(commas, conciseLambdaParameters);
80
+ }
81
+ normalLambdaParameterList(ctx) {
82
+ var _a;
83
+ const normalLambdaParameter = this.mapVisit(ctx.normalLambdaParameter);
84
+ const commas = (_a = ctx.Comma) === null || _a === void 0 ? void 0 : _a.map(comma => concat([comma, line]));
85
+ return rejectAndJoinSeps(commas, normalLambdaParameter);
86
+ }
87
+ normalLambdaParameter(ctx) {
88
+ return this.visitSingle(ctx);
89
+ }
90
+ regularLambdaParameter(ctx) {
91
+ const variableModifier = this.mapVisit(ctx.variableModifier);
92
+ const lambdaParameterType = this.visit(ctx.lambdaParameterType);
93
+ const variableDeclaratorId = this.visit(ctx.variableDeclaratorId);
94
+ return rejectAndJoin(" ", [
95
+ rejectAndJoin(" ", variableModifier),
96
+ lambdaParameterType,
97
+ variableDeclaratorId
98
+ ]);
99
+ }
100
+ lambdaParameterType(ctx) {
101
+ if (ctx.unannType) {
102
+ return this.visitSingle(ctx);
103
+ }
104
+ return printTokenWithComments(this.getSingle(ctx));
105
+ }
106
+ conciseLambdaParameter(ctx) {
107
+ return printTokenWithComments(this.getSingle(ctx));
108
+ }
109
+ lambdaBody(ctx) {
110
+ return this.visitSingle(ctx);
111
+ }
112
+ conditionalExpression(ctx, params) {
113
+ const binaryExpression = this.visit(ctx.binaryExpression, params);
114
+ if (ctx.QuestionMark) {
115
+ const expression1 = this.visit(ctx.expression[0]);
116
+ const expression2 = this.visit(ctx.expression[1]);
117
+ return indent(group(rejectAndConcat([
118
+ rejectAndJoin(line, [
119
+ binaryExpression,
120
+ rejectAndJoin(" ", [ctx.QuestionMark[0], expression1]),
121
+ rejectAndJoin(" ", [ctx.Colon[0], expression2])
122
+ ])
123
+ ])));
124
+ }
125
+ return binaryExpression;
126
+ }
127
+ binaryExpression(ctx, params) {
128
+ var _a, _b, _c, _d;
129
+ handleCommentsBinaryExpression(ctx);
130
+ const sortedNodes = sortNodes([
131
+ ctx.pattern,
132
+ ctx.referenceType,
133
+ ctx.expression,
134
+ ctx.unaryExpression
135
+ ]);
136
+ const tokens = sortTokens(getOperators(ctx));
137
+ const hasTokens = tokens.length > 0;
138
+ const nodeParams = sortedNodes.length === 1 ? params : undefined;
139
+ const nodes = [];
140
+ for (let i = 0; i < sortedNodes.length; i++) {
141
+ const node = this.visit(sortedNodes[i], nodeParams);
142
+ const isAssignment = ((_b = (_a = tokens[i]) === null || _a === void 0 ? void 0 : _a.tokenType.CATEGORIES) === null || _b === void 0 ? void 0 : _b.find(({ name }) => name === "AssignmentOperator")) !== undefined;
143
+ if (!isAssignment) {
144
+ nodes.push(node);
145
+ continue;
146
+ }
147
+ const [equals] = tokens.splice(i, 1);
148
+ const expression = sortedNodes[++i];
149
+ const nextNode = this.visit(expression);
150
+ const conditionalExpression = (_c = expression.children.conditionalExpression) === null || _c === void 0 ? void 0 : _c[0].children;
151
+ const binaryExpression = (_d = conditionalExpression === null || conditionalExpression === void 0 ? void 0 : conditionalExpression.binaryExpression) === null || _d === void 0 ? void 0 : _d[0].children;
152
+ const breakAfterOperator = (conditionalExpression === null || conditionalExpression === void 0 ? void 0 : conditionalExpression.QuestionMark) === undefined &&
153
+ binaryExpression !== undefined &&
154
+ getOperators(binaryExpression).length > 0;
155
+ if (breakAfterOperator) {
156
+ nodes.push(concat([node, " ", equals, group(indent([line, nextNode]))]));
157
+ continue;
158
+ }
159
+ const groupId = Symbol("assignment");
160
+ nodes.push(concat([
161
+ node,
162
+ " ",
163
+ equals,
164
+ indent(group(line, { id: groupId })),
165
+ indentIfBreak(nextNode, { groupId })
166
+ ]));
167
+ }
168
+ const content = binary(nodes, tokens, true);
169
+ return hasTokens && (params === null || params === void 0 ? void 0 : params.addParenthesisToWrapStatement)
170
+ ? group(concat([
171
+ ifBreak("("),
172
+ indent(concat([softline, content])),
173
+ softline,
174
+ ifBreak(")")
175
+ ]))
176
+ : content;
177
+ }
178
+ unaryExpression(ctx, params) {
179
+ const unaryPrefixOperator = ctx.UnaryPrefixOperator
180
+ ? ctx.UnaryPrefixOperator
181
+ : [];
182
+ const primary = this.visit(ctx.primary, params);
183
+ const unarySuffixOperator = ctx.UnarySuffixOperator
184
+ ? ctx.UnarySuffixOperator
185
+ : [];
186
+ return rejectAndConcat([
187
+ rejectAndConcat(unaryPrefixOperator),
188
+ primary,
189
+ rejectAndConcat(unarySuffixOperator)
190
+ ]);
191
+ }
192
+ unaryExpressionNotPlusMinus(ctx) {
193
+ const unaryPrefixOperatorNotPlusMinus = ctx.UnaryPrefixOperatorNotPlusMinus // changed when moved to TS
194
+ ? rejectAndJoin(" ", ctx.UnaryPrefixOperatorNotPlusMinus) // changed when moved to TS
195
+ : "";
196
+ const primary = this.visit(ctx.primary);
197
+ const unarySuffixOperator = ctx.UnarySuffixOperator // changed when moved to TS
198
+ ? rejectAndJoin(" ", ctx.UnarySuffixOperator) // changed when moved to TS
199
+ : "";
200
+ return rejectAndJoin(" ", [
201
+ unaryPrefixOperatorNotPlusMinus,
202
+ primary,
203
+ unarySuffixOperator
204
+ ]);
205
+ }
206
+ primary(ctx, params) {
207
+ var _a, _b, _c, _d;
208
+ const countMethodInvocation = isUniqueMethodInvocation(ctx.primarySuffix);
209
+ const newExpression = (_a = ctx.primaryPrefix[0].children.newExpression) === null || _a === void 0 ? void 0 : _a[0].children;
210
+ const isBreakableNewExpression = countMethodInvocation <= 1 &&
211
+ this.isBreakableNewExpression(newExpression);
212
+ const fqnOrRefType = (_b = ctx.primaryPrefix[0].children.fqnOrRefType) === null || _b === void 0 ? void 0 : _b[0].children;
213
+ const firstMethodInvocation = (_c = ctx.primarySuffix) === null || _c === void 0 ? void 0 : _c.map(suffix => { var _a; return (_a = suffix.children.methodInvocationSuffix) === null || _a === void 0 ? void 0 : _a[0].children; }).find(methodInvocationSuffix => methodInvocationSuffix);
214
+ const isCapitalizedIdentifier = this.isCapitalizedIdentifier(fqnOrRefType);
215
+ const shouldBreakBeforeFirstMethodInvocation = countMethodInvocation > 1 &&
216
+ !(isCapitalizedIdentifier !== null && isCapitalizedIdentifier !== void 0 ? isCapitalizedIdentifier : true) &&
217
+ firstMethodInvocation !== undefined;
218
+ const shouldBreakBeforeMethodInvocations = shouldBreakBeforeFirstMethodInvocation ||
219
+ countMethodInvocation > 2 ||
220
+ (countMethodInvocation > 1 && newExpression) ||
221
+ !(firstMethodInvocation === null || firstMethodInvocation === void 0 ? void 0 : firstMethodInvocation.argumentList);
222
+ const primaryPrefix = this.visit(ctx.primaryPrefix, Object.assign(Object.assign({}, params), { shouldBreakBeforeFirstMethodInvocation }));
223
+ const suffixes = [];
224
+ if (ctx.primarySuffix !== undefined) {
225
+ // edge case: https://github.com/jhipster/prettier-java/issues/381
226
+ let hasFirstInvocationArg = true;
227
+ if (ctx.primarySuffix.length > 1 &&
228
+ ctx.primarySuffix[1].children.methodInvocationSuffix &&
229
+ Object.keys(ctx.primarySuffix[1].children.methodInvocationSuffix[0].children).length === 2) {
230
+ hasFirstInvocationArg = false;
231
+ }
232
+ if (newExpression &&
233
+ !isBreakableNewExpression &&
234
+ ctx.primarySuffix[0].children.Dot !== undefined) {
235
+ suffixes.push(softline);
236
+ }
237
+ suffixes.push(this.visit(ctx.primarySuffix[0]));
238
+ for (let i = 1; i < ctx.primarySuffix.length; i++) {
239
+ if (shouldBreakBeforeMethodInvocations &&
240
+ ctx.primarySuffix[i].children.Dot !== undefined &&
241
+ ctx.primarySuffix[i - 1].children.methodInvocationSuffix !== undefined) {
242
+ suffixes.push(softline);
243
+ }
244
+ suffixes.push(this.visit(ctx.primarySuffix[i]));
245
+ }
246
+ if (!newExpression && countMethodInvocation === 1) {
247
+ return group(rejectAndConcat([
248
+ primaryPrefix,
249
+ hasFirstInvocationArg ? suffixes[0] : indent(suffixes[0]),
250
+ indent(rejectAndConcat(suffixes.slice(1)))
251
+ ]));
252
+ }
253
+ }
254
+ const methodInvocation = (_d = ctx.primarySuffix) === null || _d === void 0 ? void 0 : _d[0].children.methodInvocationSuffix;
255
+ const isMethodInvocationWithArguments = (methodInvocation === null || methodInvocation === void 0 ? void 0 : methodInvocation[0].children.argumentList) !== undefined;
256
+ const isUnqualifiedMethodInvocation = methodInvocation !== undefined && !(fqnOrRefType === null || fqnOrRefType === void 0 ? void 0 : fqnOrRefType.Dot);
257
+ return group(rejectAndConcat([
258
+ primaryPrefix,
259
+ isCapitalizedIdentifier || isUnqualifiedMethodInvocation
260
+ ? suffixes.shift()
261
+ : "",
262
+ !isBreakableNewExpression &&
263
+ (shouldBreakBeforeMethodInvocations || !isMethodInvocationWithArguments)
264
+ ? indent(concat(suffixes))
265
+ : concat(suffixes)
266
+ ]));
267
+ }
268
+ primaryPrefix(ctx, params) {
269
+ if (ctx.This || ctx.Void) {
270
+ return printTokenWithComments(this.getSingle(ctx));
271
+ }
272
+ return this.visitSingle(ctx, params);
273
+ }
274
+ primarySuffix(ctx, params) {
275
+ var _a;
276
+ if (ctx.Dot) {
277
+ if (ctx.This) {
278
+ return rejectAndConcat([ctx.Dot[0], ctx.This[0]]);
279
+ }
280
+ else if (ctx.Identifier) {
281
+ const typeArguments = this.visit(ctx.typeArguments);
282
+ return rejectAndConcat([ctx.Dot[0], typeArguments, ctx.Identifier[0]]);
283
+ }
284
+ const suffix = this.visit((_a = ctx.unqualifiedClassInstanceCreationExpression) !== null && _a !== void 0 ? _a : ctx.templateArgument);
285
+ return rejectAndConcat([ctx.Dot[0], suffix]);
286
+ }
287
+ return this.visitSingle(ctx, params);
288
+ }
289
+ fqnOrRefType(ctx, params) {
290
+ const fqnOrRefTypePartFirst = this.visit(ctx.fqnOrRefTypePartFirst);
291
+ const fqnOrRefTypePartRest = this.mapVisit(ctx.fqnOrRefTypePartRest);
292
+ const dims = this.visit(ctx.dims);
293
+ const dots = ctx.Dot ? ctx.Dot : [];
294
+ const isMethodInvocation = ctx.Dot && ctx.Dot.length === 1;
295
+ if (params !== undefined &&
296
+ params.shouldBreakBeforeFirstMethodInvocation === true) {
297
+ // when fqnOrRefType is a method call from an object
298
+ if (isMethodInvocation) {
299
+ return rejectAndConcat([
300
+ indent(rejectAndJoin(concat([softline, dots[0]]), [
301
+ fqnOrRefTypePartFirst,
302
+ rejectAndJoinSeps(dots.slice(1), fqnOrRefTypePartRest),
303
+ dims
304
+ ]))
305
+ ]);
306
+ // otherwise it is a fully qualified name but we need to exclude when it is just a method call
307
+ }
308
+ else if (ctx.Dot) {
309
+ return indent(rejectAndConcat([
310
+ rejectAndJoinSeps(dots.slice(0, dots.length - 1), [
311
+ fqnOrRefTypePartFirst,
312
+ ...fqnOrRefTypePartRest.slice(0, fqnOrRefTypePartRest.length - 1)
313
+ ]),
314
+ softline,
315
+ rejectAndConcat([
316
+ dots[dots.length - 1],
317
+ fqnOrRefTypePartRest[fqnOrRefTypePartRest.length - 1]
318
+ ]),
319
+ dims
320
+ ]));
321
+ }
322
+ }
323
+ return rejectAndConcat([
324
+ rejectAndJoinSeps(dots, [fqnOrRefTypePartFirst, ...fqnOrRefTypePartRest]),
325
+ dims
326
+ ]);
327
+ }
328
+ fqnOrRefTypePartFirst(ctx) {
329
+ const annotation = this.mapVisit(ctx.annotation);
330
+ const fqnOrRefTypeCommon = this.visit(ctx.fqnOrRefTypePartCommon);
331
+ return rejectAndJoin(" ", [
332
+ rejectAndJoin(" ", annotation),
333
+ fqnOrRefTypeCommon
334
+ ]);
335
+ }
336
+ fqnOrRefTypePartRest(ctx) {
337
+ const annotation = this.mapVisit(ctx.annotation);
338
+ const fqnOrRefTypeCommon = this.visit(ctx.fqnOrRefTypePartCommon);
339
+ const typeArguments = this.visit(ctx.typeArguments);
340
+ return rejectAndJoin(" ", [
341
+ rejectAndJoin(" ", annotation),
342
+ rejectAndConcat([typeArguments, fqnOrRefTypeCommon])
343
+ ]);
344
+ }
345
+ fqnOrRefTypePartCommon(ctx) {
346
+ let keyWord = null;
347
+ if (ctx.Identifier) {
348
+ keyWord = ctx.Identifier[0];
349
+ }
350
+ else {
351
+ keyWord = ctx.Super[0];
352
+ }
353
+ const typeArguments = this.visit(ctx.typeArguments);
354
+ return rejectAndConcat([keyWord, typeArguments]);
355
+ }
356
+ parenthesisExpression(ctx, params) {
357
+ const expression = this.visit(ctx.expression);
358
+ const separator = (params === null || params === void 0 ? void 0 : params.addParenthesisToWrapStatement) ? softline : "";
359
+ return putIntoBraces(expression, separator, ctx.LBrace[0], ctx.RBrace[0]);
360
+ }
361
+ castExpression(ctx) {
362
+ return this.visitSingle(ctx);
363
+ }
364
+ primitiveCastExpression(ctx) {
365
+ const primitiveType = this.visit(ctx.primitiveType);
366
+ const unaryExpression = this.visit(ctx.unaryExpression);
367
+ return rejectAndJoin(" ", [
368
+ rejectAndConcat([ctx.LBrace[0], primitiveType, ctx.RBrace[0]]),
369
+ unaryExpression
370
+ ]);
371
+ }
372
+ referenceTypeCastExpression(ctx) {
373
+ const referenceType = this.visit(ctx.referenceType);
374
+ const hasAdditionalBounds = ctx.additionalBound !== undefined;
375
+ const additionalBounds = rejectAndJoin(line, this.mapVisit(ctx.additionalBound));
376
+ const expression = ctx.lambdaExpression
377
+ ? this.visit(ctx.lambdaExpression)
378
+ : this.visit(ctx.unaryExpressionNotPlusMinus);
379
+ return rejectAndJoin(" ", [
380
+ putIntoBraces(rejectAndJoin(line, [referenceType, additionalBounds]), hasAdditionalBounds ? softline : "", ctx.LBrace[0], ctx.RBrace[0]),
381
+ expression
382
+ ]);
383
+ }
384
+ newExpression(ctx) {
385
+ return this.visitSingle(ctx);
386
+ }
387
+ unqualifiedClassInstanceCreationExpression(ctx) {
388
+ const typeArguments = this.visit(ctx.typeArguments);
389
+ const classOrInterfaceTypeToInstantiate = this.visit(ctx.classOrInterfaceTypeToInstantiate);
390
+ let content = printArgumentListWithBraces.call(this, ctx.argumentList, ctx.RBrace[0], ctx.LBrace[0]);
391
+ const classBody = this.visit(ctx.classBody);
392
+ return rejectAndJoin(" ", [
393
+ ctx.New[0],
394
+ rejectAndConcat([
395
+ typeArguments,
396
+ classOrInterfaceTypeToInstantiate,
397
+ content
398
+ ]),
399
+ classBody
400
+ ]);
401
+ }
402
+ classOrInterfaceTypeToInstantiate(ctx) {
403
+ const tokens = sortAnnotationIdentifier(ctx.annotation, ctx.Identifier);
404
+ const segments = [];
405
+ let currentSegment = [];
406
+ forEach(tokens, token => {
407
+ if (isAnnotationCstNode(token)) {
408
+ currentSegment.push(this.visit([token]));
409
+ }
410
+ else {
411
+ currentSegment.push(token);
412
+ segments.push(rejectAndJoin(" ", currentSegment));
413
+ currentSegment = [];
414
+ }
415
+ });
416
+ const typeArgumentsOrDiamond = this.visit(ctx.typeArgumentsOrDiamond);
417
+ const dots = ctx.Dot ? ctx.Dot : [];
418
+ return rejectAndConcat([
419
+ rejectAndJoinSeps(dots, segments),
420
+ typeArgumentsOrDiamond
421
+ ]);
422
+ }
423
+ typeArgumentsOrDiamond(ctx) {
424
+ return this.visitSingle(ctx);
425
+ }
426
+ diamond(ctx) {
427
+ return concat([ctx.Less[0], ctx.Greater[0]]);
428
+ }
429
+ methodInvocationSuffix(ctx) {
430
+ return printArgumentListWithBraces.call(this, ctx.argumentList, ctx.RBrace[0], ctx.LBrace[0]);
431
+ }
432
+ argumentList(ctx) {
433
+ var _a, _b;
434
+ const headArgs = this.mapVisit(ctx.expression.slice(0, -1)).map((expression, index) => concat([expression, ctx.Comma[index], line]));
435
+ const lastExpression = ctx.expression.at(-1);
436
+ const lastArg = this.visit(lastExpression);
437
+ if (this.isArgumentListHuggable(ctx)) {
438
+ const huggedLastArg = this.visit(lastExpression, { hug: true });
439
+ const lastArgNotHuggable = typeof huggedLastArg === "object" &&
440
+ !Array.isArray(huggedLastArg) &&
441
+ huggedLastArg.type === "label" &&
442
+ ((_a = huggedLastArg.label) === null || _a === void 0 ? void 0 : _a.huggable) === false;
443
+ if (lastArgNotHuggable || headArgs.some(willBreak)) {
444
+ return group([indent([line, ...headArgs, lastArg]), line], {
445
+ shouldBreak: true
446
+ });
447
+ }
448
+ const suffix = ((_b = lastExpression === null || lastExpression === void 0 ? void 0 : lastExpression.children.lambdaExpression) === null || _b === void 0 ? void 0 : _b[0].children.lambdaBody[0].children.block)
449
+ ? ""
450
+ : line;
451
+ const hugged = [
452
+ ...headArgs,
453
+ group([huggedLastArg, suffix], { shouldBreak: true })
454
+ ];
455
+ const expanded = group([indent([line, ...headArgs, lastArg]), line], {
456
+ shouldBreak: true
457
+ });
458
+ return willBreak(huggedLastArg)
459
+ ? [breakParent, conditionalGroup([hugged, expanded])]
460
+ : conditionalGroup([[...headArgs, huggedLastArg], hugged, expanded]);
461
+ }
462
+ return group([indent([softline, ...headArgs, lastArg]), softline]);
463
+ }
464
+ arrayCreationExpression(ctx) {
465
+ const type = ctx.primitiveType
466
+ ? this.visit(ctx.primitiveType)
467
+ : this.visit(ctx.classOrInterfaceType);
468
+ const suffix = ctx.arrayCreationExpressionWithoutInitializerSuffix
469
+ ? this.visit(ctx.arrayCreationExpressionWithoutInitializerSuffix)
470
+ : this.visit(ctx.arrayCreationWithInitializerSuffix);
471
+ return rejectAndConcat([concat([ctx.New[0], " "]), type, suffix]);
472
+ }
473
+ arrayCreationExpressionWithoutInitializerSuffix(ctx) {
474
+ const dimExprs = this.visit(ctx.dimExprs);
475
+ const dims = this.visit(ctx.dims);
476
+ return rejectAndConcat([dimExprs, dims]);
477
+ }
478
+ arrayCreationWithInitializerSuffix(ctx) {
479
+ const dims = this.visit(ctx.dims);
480
+ const arrayInitializer = this.visit(ctx.arrayInitializer);
481
+ return rejectAndJoin(" ", [dims, arrayInitializer]);
482
+ }
483
+ dimExprs(ctx) {
484
+ const dimExpr = this.mapVisit(ctx.dimExpr);
485
+ return rejectAndConcat(dimExpr);
486
+ }
487
+ dimExpr(ctx) {
488
+ const annotations = this.mapVisit(ctx.annotation);
489
+ const expression = this.visit(ctx.expression);
490
+ return rejectAndJoin(" ", [
491
+ rejectAndJoin(" ", annotations),
492
+ rejectAndConcat([ctx.LSquare[0], expression, ctx.RSquare[0]])
493
+ ]);
494
+ }
495
+ classLiteralSuffix(ctx) {
496
+ const squares = [];
497
+ if (ctx.LSquare) {
498
+ for (let i = 0; i < ctx.LSquare.length; i++) {
499
+ squares.push(concat([ctx.LSquare[i], ctx.RSquare[i]]));
500
+ }
501
+ }
502
+ return rejectAndConcat([...squares, ctx.Dot[0], ctx.Class[0]]);
503
+ }
504
+ arrayAccessSuffix(ctx) {
505
+ const expression = this.visit(ctx.expression);
506
+ return rejectAndConcat([ctx.LSquare[0], expression, ctx.RSquare[0]]);
507
+ }
508
+ methodReferenceSuffix(ctx) {
509
+ const typeArguments = this.visit(ctx.typeArguments);
510
+ const identifierOrNew = ctx.New ? ctx.New[0] : ctx.Identifier[0];
511
+ return rejectAndConcat([ctx.ColonColon[0], typeArguments, identifierOrNew]);
512
+ }
513
+ templateArgument(ctx) {
514
+ var _a;
515
+ return ctx.template
516
+ ? this.visit(ctx.template)
517
+ : printTokenWithComments(((_a = ctx.StringLiteral) !== null && _a !== void 0 ? _a : ctx.TextBlock)[0]);
518
+ }
519
+ template(ctx) {
520
+ return this.visitSingle(ctx);
521
+ }
522
+ stringTemplate(ctx) {
523
+ const embeddedExpressions = this.mapVisit(ctx.embeddedExpression).flatMap(expression => group([softline, expression, lineSuffixBoundary, dedent(softline)]));
524
+ return concat([
525
+ ctx.StringTemplateBegin[0],
526
+ rejectAndJoinSeps(ctx.StringTemplateMid, embeddedExpressions),
527
+ ctx.StringTemplateEnd[0]
528
+ ]);
529
+ }
530
+ textBlockTemplate(ctx) {
531
+ const embeddedExpressions = this.mapVisit(ctx.embeddedExpression).flatMap(expression => group([softline, expression, lineSuffixBoundary, dedent(softline)]));
532
+ return concat([
533
+ ctx.TextBlockTemplateBegin[0],
534
+ rejectAndJoinSeps(ctx.TextBlockTemplateMid, embeddedExpressions),
535
+ ctx.TextBlockTemplateEnd[0]
536
+ ]);
537
+ }
538
+ embeddedExpression(ctx) {
539
+ return this.visit(ctx.expression);
540
+ }
541
+ pattern(ctx) {
542
+ return this.visitSingle(ctx);
543
+ }
544
+ typePattern(ctx) {
545
+ return this.visitSingle(ctx);
546
+ }
547
+ recordPattern(ctx) {
548
+ var _a, _b;
549
+ const componentPatterns = (_b = (_a = ctx.componentPatternList) === null || _a === void 0 ? void 0 : _a[0].children.componentPattern) !== null && _b !== void 0 ? _b : [];
550
+ handleCommentsParameters(ctx.LBrace[0], componentPatterns, ctx.RBrace[0]);
551
+ const referenceType = this.visit(ctx.referenceType);
552
+ const componentPatternList = this.visit(ctx.componentPatternList);
553
+ return concat([
554
+ referenceType,
555
+ putIntoBraces(componentPatternList, softline, ctx.LBrace[0], ctx.RBrace[0])
556
+ ]);
557
+ }
558
+ componentPatternList(ctx) {
559
+ var _a, _b;
560
+ const componentPatterns = this.mapVisit(ctx.componentPattern);
561
+ const commas = (_b = (_a = ctx.Comma) === null || _a === void 0 ? void 0 : _a.map(elt => concat([elt, line]))) !== null && _b !== void 0 ? _b : [];
562
+ return rejectAndJoinSeps(commas, componentPatterns);
563
+ }
564
+ componentPattern(ctx) {
565
+ return this.visitSingle(ctx);
566
+ }
567
+ matchAllPattern(ctx) {
568
+ return printTokenWithComments(ctx.Underscore[0]);
569
+ }
570
+ guard(ctx) {
571
+ const expression = this.visit(ctx.expression, {
572
+ addParenthesisToWrapStatement: true
573
+ });
574
+ return concat([ctx.When[0], " ", expression]);
575
+ }
576
+ isRefTypeInMethodRef() {
577
+ return "isRefTypeInMethodRef";
578
+ }
579
+ isArgumentListHuggable(argumentList) {
580
+ var _a, _b, _c;
581
+ const expressions = argumentList.expression;
582
+ const lastArgument = expressions.at(-1);
583
+ const lastArgumentLambdaBodyExpression = (_b = (_a = lastArgument === null || lastArgument === void 0 ? void 0 : lastArgument.children.lambdaExpression) === null || _a === void 0 ? void 0 : _a[0].children.lambdaBody[0].children.expression) === null || _b === void 0 ? void 0 : _b[0].children;
584
+ const lastArgumentLambdaBodyTernaryExpression = (_c = lastArgumentLambdaBodyExpression === null || lastArgumentLambdaBodyExpression === void 0 ? void 0 : lastArgumentLambdaBodyExpression.conditionalExpression) === null || _c === void 0 ? void 0 : _c[0].children;
585
+ return (!(lastArgument === null || lastArgument === void 0 ? void 0 : lastArgument.leadingComments) &&
586
+ !(lastArgument === null || lastArgument === void 0 ? void 0 : lastArgument.trailingComments) &&
587
+ (!lastArgumentLambdaBodyExpression ||
588
+ (lastArgumentLambdaBodyTernaryExpression === null || lastArgumentLambdaBodyTernaryExpression === void 0 ? void 0 : lastArgumentLambdaBodyTernaryExpression.QuestionMark) !== undefined ||
589
+ (lastArgumentLambdaBodyTernaryExpression === null || lastArgumentLambdaBodyTernaryExpression === void 0 ? void 0 : lastArgumentLambdaBodyTernaryExpression.binaryExpression[0].children.unaryExpression.length) === 1) &&
590
+ expressions.findIndex(({ children }) => children.lambdaExpression) ===
591
+ expressions.length - 1);
592
+ }
593
+ isBreakableNewExpression(newExpression) {
594
+ var _a, _b, _c, _d, _e;
595
+ const arrayCreationExpression = (_a = newExpression === null || newExpression === void 0 ? void 0 : newExpression.arrayCreationExpression) === null || _a === void 0 ? void 0 : _a[0].children;
596
+ const classInstanceCreationExpression = (_b = newExpression === null || newExpression === void 0 ? void 0 : newExpression.unqualifiedClassInstanceCreationExpression) === null || _b === void 0 ? void 0 : _b[0].children;
597
+ return [
598
+ (_c = arrayCreationExpression === null || arrayCreationExpression === void 0 ? void 0 : arrayCreationExpression.classOrInterfaceType) === null || _c === void 0 ? void 0 : _c[0].children.classType[0].children.typeArguments,
599
+ (_d = arrayCreationExpression === null || arrayCreationExpression === void 0 ? void 0 : arrayCreationExpression.arrayCreationWithInitializerSuffix) === null || _d === void 0 ? void 0 : _d[0].children.arrayInitializer[0].children.variableInitializerList,
600
+ (_e = classInstanceCreationExpression === null || classInstanceCreationExpression === void 0 ? void 0 : classInstanceCreationExpression.classOrInterfaceTypeToInstantiate[0].children.typeArgumentsOrDiamond) === null || _e === void 0 ? void 0 : _e[0].children.typeArguments,
601
+ classInstanceCreationExpression === null || classInstanceCreationExpression === void 0 ? void 0 : classInstanceCreationExpression.argumentList
602
+ ].some(breakablePart => breakablePart !== undefined);
603
+ }
604
+ isCapitalizedIdentifier(fqnOrRefType) {
605
+ var _a, _b, _c;
606
+ const fqnOrRefTypeParts = [
607
+ fqnOrRefType === null || fqnOrRefType === void 0 ? void 0 : fqnOrRefType.fqnOrRefTypePartFirst[0],
608
+ ...((_a = fqnOrRefType === null || fqnOrRefType === void 0 ? void 0 : fqnOrRefType.fqnOrRefTypePartRest) !== null && _a !== void 0 ? _a : [])
609
+ ];
610
+ const nextToLastIdentifier = (_c = (_b = fqnOrRefTypeParts[fqnOrRefTypeParts.length - 2]) === null || _b === void 0 ? void 0 : _b.children.fqnOrRefTypePartCommon[0].children.Identifier) === null || _c === void 0 ? void 0 : _c[0].image;
611
+ return (nextToLastIdentifier &&
612
+ /^\p{Uppercase_Letter}/u.test(nextToLastIdentifier));
613
+ }
614
+ }