prettier-plugin-java 2.8.1 → 2.9.1

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.
Files changed (39) hide show
  1. package/README.md +42 -102
  2. package/dist/index.cjs +2215 -0
  3. package/dist/index.d.cts +1630 -0
  4. package/dist/index.d.mts +1633 -0
  5. package/dist/index.mjs +2215 -0
  6. package/dist/tree-sitter-java_orchard.wasm +0 -0
  7. package/package.json +62 -24
  8. package/dist/comments.d.ts +0 -17
  9. package/dist/comments.js +0 -229
  10. package/dist/index.d.ts +0 -563
  11. package/dist/index.js +0 -29
  12. package/dist/options.d.ts +0 -43
  13. package/dist/options.js +0 -284
  14. package/dist/parser.d.ts +0 -9
  15. package/dist/parser.js +0 -24
  16. package/dist/printer.d.ts +0 -18
  17. package/dist/printer.js +0 -40
  18. package/dist/printers/arrays.d.ts +0 -9
  19. package/dist/printers/arrays.js +0 -9
  20. package/dist/printers/blocks-and-statements.d.ts +0 -117
  21. package/dist/printers/blocks-and-statements.js +0 -340
  22. package/dist/printers/classes.d.ts +0 -157
  23. package/dist/printers/classes.js +0 -485
  24. package/dist/printers/expressions.d.ts +0 -134
  25. package/dist/printers/expressions.js +0 -627
  26. package/dist/printers/helpers.d.ts +0 -73
  27. package/dist/printers/helpers.js +0 -273
  28. package/dist/printers/index.d.ts +0 -2
  29. package/dist/printers/index.js +0 -13
  30. package/dist/printers/interfaces.d.ts +0 -62
  31. package/dist/printers/interfaces.js +0 -175
  32. package/dist/printers/lexical-structure.d.ts +0 -14
  33. package/dist/printers/lexical-structure.js +0 -29
  34. package/dist/printers/names.d.ts +0 -12
  35. package/dist/printers/names.js +0 -11
  36. package/dist/printers/packages-and-modules.d.ts +0 -46
  37. package/dist/printers/packages-and-modules.js +0 -169
  38. package/dist/printers/types-values-and-variables.d.ts +0 -46
  39. package/dist/printers/types-values-and-variables.js +0 -90
package/dist/index.cjs ADDED
@@ -0,0 +1,2215 @@
1
+ let web_tree_sitter = require("web-tree-sitter");
2
+ let prettier = require("prettier");
3
+ let prettier_doc = require("prettier/doc");
4
+ //#region src/options.ts
5
+ var options_default = {
6
+ arrowParens: {
7
+ type: "choice",
8
+ category: "Java",
9
+ default: "always",
10
+ choices: [{
11
+ value: "always",
12
+ description: ""
13
+ }, {
14
+ value: "avoid",
15
+ description: ""
16
+ }],
17
+ description: "Include parentheses around a sole arrow function parameter."
18
+ },
19
+ trailingComma: {
20
+ type: "choice",
21
+ category: "Java",
22
+ default: "all",
23
+ choices: [
24
+ {
25
+ value: "all",
26
+ description: ""
27
+ },
28
+ {
29
+ value: "es5",
30
+ description: ""
31
+ },
32
+ {
33
+ value: "none",
34
+ description: ""
35
+ }
36
+ ],
37
+ description: "Print trailing commas wherever possible when multi-line."
38
+ },
39
+ experimentalOperatorPosition: {
40
+ type: "choice",
41
+ category: "Java",
42
+ default: "end",
43
+ choices: [{
44
+ value: "start",
45
+ description: ""
46
+ }, {
47
+ value: "end",
48
+ description: ""
49
+ }],
50
+ description: "Where to print operators when binary expressions wrap lines."
51
+ }
52
+ };
53
+ //#endregion
54
+ //#region src/node-types.ts
55
+ const multiFieldsByType = {
56
+ array_creation_expression: { dimensions: true },
57
+ cast_expression: { type: true },
58
+ constant_declaration: { declarator: true },
59
+ exports_module_directive: { modules: true },
60
+ field_declaration: { declarator: true },
61
+ for_statement: {
62
+ init: true,
63
+ update: true
64
+ },
65
+ local_variable_declaration: { declarator: true },
66
+ opens_module_directive: { modules: true },
67
+ provides_module_directive: { provider: true },
68
+ requires_module_directive: { modifiers: true },
69
+ spread_parameter: { annotations: true }
70
+ };
71
+ //#endregion
72
+ //#region src/printers/helpers.ts
73
+ const { group: group$7, hardline: hardline$6, ifBreak: ifBreak$2, indent: indent$7, join: join$7, line: line$7, softline: softline$5 } = prettier_doc.builders;
74
+ const { mapDoc } = prettier_doc.utils;
75
+ function hasType(path, type) {
76
+ return path.node.type === type;
77
+ }
78
+ function hasChild(path, fieldName) {
79
+ return path.node[fieldName] != null;
80
+ }
81
+ function definedKeys(obj, options) {
82
+ return (options ?? Object.keys(obj)).filter((key) => obj[key] !== void 0);
83
+ }
84
+ function printModifiers(path, print, annotationMode) {
85
+ const modifiersIndex = path.node.namedChildren.findIndex(({ type }) => type === "modifiers");
86
+ if (modifiersIndex === -1) return [];
87
+ const separator = annotationMode === "avoidBreak" ? line$7 : annotationMode === "noBreak" || path.node.namedChildren[modifiersIndex].children.some(({ type }) => type !== "annotation" && type !== "marker_annotation") ? " " : hardline$6;
88
+ return [path.call((modifiers) => print(modifiers, { annotationMode }), "namedChildren", modifiersIndex), separator];
89
+ }
90
+ function printValue(path) {
91
+ return path.node.value;
92
+ }
93
+ function lineStartWithComments(node) {
94
+ return node.comments?.length ? Math.min(node.start.row, node.comments[0].start.row) : node.start.row;
95
+ }
96
+ function lineEndWithComments(node) {
97
+ return node.comments?.length ? Math.max(node.end.row, node.comments.at(-1).end.row) : node.end.row;
98
+ }
99
+ function printDanglingComments(path) {
100
+ if (!path.node.comments?.length) return [];
101
+ const comments = [];
102
+ path.each((commentPath) => {
103
+ const comment = commentPath.node;
104
+ if (comment.leading || comment.trailing) return;
105
+ comment.printed = true;
106
+ comments.push(printComment(comment));
107
+ }, "comments");
108
+ return join$7(hardline$6, comments);
109
+ }
110
+ function printComment(comment) {
111
+ const lines = comment.value.split("\n").map((line) => line.trim());
112
+ return lines.length > 1 && lines[0].startsWith("/*") && lines.slice(1).every((line) => line.startsWith("*")) && lines.at(-1).endsWith("*/") ? join$7(hardline$6, lines.map((line, index) => index === 0 ? line : ` ${line}`)) : comment.value;
113
+ }
114
+ function hasLeadingComments(node) {
115
+ return node.comments?.some(({ leading }) => leading) ?? false;
116
+ }
117
+ function indentInParentheses(contents) {
118
+ return !Array.isArray(contents) || contents.length ? [
119
+ "(",
120
+ indent$7([softline$5, contents]),
121
+ softline$5,
122
+ ")"
123
+ ] : "()";
124
+ }
125
+ function printArrayInitializer(path, print, options) {
126
+ if (!path.node.namedChildren.length) {
127
+ const danglingComments = printDanglingComments(path);
128
+ return danglingComments.length ? [
129
+ "{",
130
+ indent$7([hardline$6, ...danglingComments]),
131
+ hardline$6,
132
+ "}"
133
+ ] : "{}";
134
+ }
135
+ const list = join$7([",", line$7], path.map(print, "namedChildren"));
136
+ if (list.length && options.trailingComma !== "none") list.push(ifBreak$2(","));
137
+ return group$7([
138
+ "{",
139
+ indent$7([softline$5, ...list]),
140
+ softline$5,
141
+ "}"
142
+ ]);
143
+ }
144
+ function printBlock(path, contents) {
145
+ if (contents.length) return group$7([
146
+ "{",
147
+ indent$7([hardline$6, ...join$7(hardline$6, contents)]),
148
+ hardline$6,
149
+ "}"
150
+ ]);
151
+ const danglingComments = printDanglingComments(path);
152
+ if (danglingComments.length) return [
153
+ "{",
154
+ indent$7([hardline$6, ...danglingComments]),
155
+ hardline$6,
156
+ "}"
157
+ ];
158
+ const parent = path.parent;
159
+ const grandparent = path.grandparent;
160
+ return parent?.type === "catch_clause" && (grandparent?.type === "try_statement" || grandparent?.type === "try_with_resources_statement") && grandparent.namedChildren.filter(({ type }) => type === "catch_clause").length === 1 && !grandparent.namedChildren.some(({ type }) => type === "finally_clause") || parent && [
161
+ "for_statement",
162
+ "do_statement",
163
+ "enhanced_for_statement",
164
+ "while_statement"
165
+ ].includes(parent.type) || [
166
+ "annotation_type_body",
167
+ "class_body",
168
+ "constructor_body",
169
+ "enum_body",
170
+ "interface_body",
171
+ "module_body",
172
+ "record_pattern_body"
173
+ ].includes(path.node.type) || parent && [
174
+ "block",
175
+ "lambda_expression",
176
+ "method_declaration",
177
+ "static_initializer",
178
+ "synchronized_statement"
179
+ ].includes(parent.type) ? "{}" : [
180
+ "{",
181
+ hardline$6,
182
+ "}"
183
+ ];
184
+ }
185
+ function printBlockStatements(path, print) {
186
+ const parts = [];
187
+ path.each((child) => {
188
+ const { node, previous } = child;
189
+ if (node.type === "switch_label") return;
190
+ const blankLine = parts.length && previous && lineStartWithComments(node) > lineEndWithComments(previous) + 1;
191
+ const declaration = print(child);
192
+ parts.push(blankLine ? [hardline$6, declaration] : declaration);
193
+ }, "namedChildren");
194
+ return parts;
195
+ }
196
+ function printBodyDeclarations(path, print, padFirst = false) {
197
+ const isInterfaceBody = path.node.type === "interface_body";
198
+ const isFormalParameters = path.node.type === "formal_parameters";
199
+ const separator = isFormalParameters ? softline$5 : hardline$6;
200
+ let previousRequiresPadding = padFirst;
201
+ return path.map((child) => {
202
+ const { node, previous } = child;
203
+ const modifiers = node.namedChildren.find(({ type }) => type === "modifiers")?.children ?? [];
204
+ const firstAnnotationIndex = modifiers.findIndex(({ type }) => type === "annotation" || type === "marker_annotation");
205
+ const lastNonAnnotationIndex = modifiers.findLastIndex(({ type }) => type !== "annotation" && type !== "marker_annotation");
206
+ const currentRequiresPadding = firstAnnotationIndex !== -1 && (!isFormalParameters && lastNonAnnotationIndex === -1 || firstAnnotationIndex < lastNonAnnotationIndex) || !isFormalParameters && node.type !== "constant_declaration" && node.type !== "enum_constant" && node.type !== "field_declaration" && !(isInterfaceBody && node.type === "method_declaration" && !node.bodyNode);
207
+ const blankLine = previousRequiresPadding || previous && (currentRequiresPadding || lineStartWithComments(node) > lineEndWithComments(previous) + 1);
208
+ previousRequiresPadding = currentRequiresPadding;
209
+ const declaration = print(child);
210
+ return blankLine ? [separator, declaration] : declaration;
211
+ }, "namedChildren");
212
+ }
213
+ function printVariableDeclaration(path, print) {
214
+ const declaration = printModifiers(path, print);
215
+ declaration.push(path.call(print, "typeNode"), " ");
216
+ const declarators = path.map(print, "declaratorNodes");
217
+ if (declarators.length > 1 && path.node.declaratorNodes.some(({ valueNode }) => valueNode)) declaration.push(group$7(indent$7(join$7([",", line$7], declarators)), { shouldBreak: path.parent?.type !== "for_statement" }));
218
+ else declaration.push(join$7(", ", declarators));
219
+ declaration.push(";");
220
+ return declaration;
221
+ }
222
+ function printTextBlock(path, contents) {
223
+ const parts = [
224
+ "\"\"\"",
225
+ hardline$6,
226
+ contents,
227
+ "\"\"\""
228
+ ];
229
+ const parentType = path.parent?.type;
230
+ const grandparentType = path.grandparent?.type;
231
+ return parentType === "assignment_expression" || parentType === "variable_declarator" || path.node.fieldName === "object" && (grandparentType === "assignment_expression" || grandparentType === "variable_declarator") ? indent$7(parts) : parts;
232
+ }
233
+ function embedTextBlock(path) {
234
+ if (path.node.namedChildren.some(({ type }) => type === "string_interpolation") || path.node.children[0].value === "\"") return null;
235
+ const language = findEmbeddedLanguage(path);
236
+ if (!language) return null;
237
+ const text = unescapeTextBlockContents(textBlockContents(path.node));
238
+ return async (textToDoc) => {
239
+ return printTextBlock(path, [escapeDocForTextBlock(await textToDoc(text, { parser: language })), hardline$6]);
240
+ };
241
+ }
242
+ function textBlockContents(node) {
243
+ const lines = node.value.split("\n").slice(1);
244
+ const baseIndent = findBaseIndent(lines);
245
+ return lines.map((line) => line.slice(baseIndent)).join("\n").slice(0, -3);
246
+ }
247
+ function findBaseIndent(lines) {
248
+ return Math.min(...lines.map((line) => line.search(/\S/)).filter((indent) => indent >= 0));
249
+ }
250
+ function findEmbeddedLanguage(path) {
251
+ return path.ancestors.find(({ type, comments }) => type === "block" || comments?.some(({ leading }) => leading))?.comments?.filter(({ leading }) => leading).map(({ value }) => value.match(/^(?:\/\/|\/\*)\s*language\s*=\s*(\S+)/)?.[1]).findLast((language) => language)?.toLowerCase();
252
+ }
253
+ function escapeDocForTextBlock(doc) {
254
+ return mapDoc(doc, (currentDoc) => typeof currentDoc === "string" ? currentDoc.replace(/\\|"""/g, (match) => match === "\\" ? "\\\\" : "\"\"\\\"") : currentDoc);
255
+ }
256
+ function unescapeTextBlockContents(text) {
257
+ return text.replace(/\\(?:([stnr"'\\])|\n|\r\n?)/g, (_, escaped) => {
258
+ switch (escaped) {
259
+ case "s": return " ";
260
+ case "t": return " ";
261
+ case "n": return "\n";
262
+ case "r": return "\r";
263
+ default: return escaped ?? "";
264
+ }
265
+ });
266
+ }
267
+ //#endregion
268
+ //#region src/printers/arrays.ts
269
+ var arrays_default = { array_initializer: printArrayInitializer };
270
+ //#endregion
271
+ //#region src/printers/blocks-and-statements.ts
272
+ const { group: group$6, hardline: hardline$5, ifBreak: ifBreak$1, indent: indent$6, indentIfBreak: indentIfBreak$2, join: join$6, line: line$6, softline: softline$4 } = prettier_doc.builders;
273
+ var blocks_and_statements_default = {
274
+ block(path, print) {
275
+ return printBlock(path, printBlockStatements(path, print));
276
+ },
277
+ local_variable_declaration: printVariableDeclaration,
278
+ labeled_statement(path, print) {
279
+ return join$6(": ", path.map(print, "namedChildren"));
280
+ },
281
+ expression_statement(path, print) {
282
+ const parentType = path.parent?.type;
283
+ const expressionType = path.node.namedChildren[0].type;
284
+ const expression = path.call(print, "namedChildren", 0);
285
+ return expressionType === "switch_expression" && parentType !== "assignment_expression" && parentType !== "switch_rule" ? expression : [expression, ";"];
286
+ },
287
+ if_statement(path, print) {
288
+ const statement = ["if ", path.call(print, "conditionNode")];
289
+ if (path.node.consequenceNode.type === ";") statement.push(";");
290
+ else statement.push(" ", path.call(print, "consequenceNode"));
291
+ if (!hasChild(path, "alternativeNode")) return statement;
292
+ const danglingComments = printDanglingComments(path);
293
+ if (danglingComments.length) statement.push(hardline$5, ...danglingComments, hardline$5);
294
+ else {
295
+ const ifHasBlock = path.node.consequenceNode.type === "block";
296
+ statement.push(ifHasBlock ? " " : hardline$5);
297
+ }
298
+ statement.push("else");
299
+ if (path.node.alternativeNode.type === ";") statement.push(";");
300
+ else statement.push(" ", path.call(print, "alternativeNode"));
301
+ return statement;
302
+ },
303
+ assert_statement(path, print) {
304
+ return [
305
+ "assert ",
306
+ ...join$6(" : ", path.map(print, "namedChildren")),
307
+ ";"
308
+ ];
309
+ },
310
+ switch_expression(path, print) {
311
+ return join$6(" ", [
312
+ "switch",
313
+ path.call(print, "conditionNode"),
314
+ path.call(print, "bodyNode")
315
+ ]);
316
+ },
317
+ switch_block(path, print) {
318
+ return printBlock(path, path.map(print, "namedChildren"));
319
+ },
320
+ switch_block_statement_group(path, print) {
321
+ const parts = [];
322
+ path.each((child) => {
323
+ if (child.node.type === "switch_label") parts.push(print(child), ":");
324
+ }, "namedChildren");
325
+ const firstStatementIndex = path.node.namedChildren.findIndex(({ type }) => type !== "switch_label");
326
+ if (firstStatementIndex === path.node.namedChildren.length - 1 && path.node.namedChildren[firstStatementIndex].type === "block") parts.push(" ", path.call(print, "namedChildren", firstStatementIndex));
327
+ else if (firstStatementIndex !== -1) parts.push(indent$6([hardline$5, ...join$6(hardline$5, printBlockStatements(path, print))]));
328
+ return parts;
329
+ },
330
+ switch_label(path, print) {
331
+ if (!path.node.children.some(({ type }) => type === "case")) return "default";
332
+ const values = [];
333
+ path.each((child) => {
334
+ if (child.node.type !== "guard") values.push(print(child));
335
+ }, "namedChildren");
336
+ const hasMultipleValues = values.length > 1;
337
+ const label = hasMultipleValues ? ["case", indent$6([line$6, ...join$6([",", line$6], values)])] : ["case ", values[0]];
338
+ const guardIndex = path.node.namedChildren.findIndex(({ type }) => type === "guard");
339
+ return guardIndex !== -1 ? [group$6([...label, hasMultipleValues ? line$6 : " "]), path.call(print, "namedChildren", guardIndex)] : group$6(label);
340
+ },
341
+ switch_rule(path, print) {
342
+ const bodyIndex = path.node.namedChildren.findIndex(({ type }) => type === "block" || type === "expression_statement" || type === "throw_statement");
343
+ const body = path.call(print, "namedChildren", bodyIndex);
344
+ const switchLabelIndex = path.node.namedChildren.findIndex(({ type }) => type === "switch_label");
345
+ const parts = [path.call(print, "namedChildren", switchLabelIndex), " ->"];
346
+ const bodyNode = path.node.namedChildren[bodyIndex];
347
+ if (bodyNode.type !== "block" && hasLeadingComments(bodyNode)) parts.push(indent$6([hardline$5, body]));
348
+ else parts.push(" ", body);
349
+ return parts;
350
+ },
351
+ while_statement(path, print) {
352
+ const parts = ["while ", path.call(print, "conditionNode")];
353
+ const body = path.call(print, "bodyNode");
354
+ const bodyType = path.node.bodyNode.type;
355
+ if (bodyType === "block") {
356
+ parts.push(" ", body);
357
+ return parts;
358
+ } else if (bodyType === ";") {
359
+ parts.push(";");
360
+ return parts;
361
+ } else {
362
+ parts.push(line$6, body);
363
+ return group$6(indent$6(parts));
364
+ }
365
+ },
366
+ do_statement(path, print) {
367
+ return [
368
+ "do",
369
+ path.node.bodyNode.type === ";" ? ";" : [" ", path.call(print, "bodyNode")],
370
+ " while ",
371
+ path.call(print, "conditionNode"),
372
+ ";"
373
+ ];
374
+ },
375
+ for_statement(path, print) {
376
+ const danglingComments = printDanglingComments(path);
377
+ if (danglingComments.length) danglingComments.push(hardline$5);
378
+ const hasInit = path.node.initNodes.length > 0;
379
+ const hasCondition = hasChild(path, "conditionNode");
380
+ const hasUpdate = path.node.updateNodes.length > 0;
381
+ const expressions = [
382
+ !hasInit ? ";" : path.node.initNodes[0].type === "local_variable_declaration" ? path.call(print, "initNodes", 0) : [printExpressionList(path.map(print, "initNodes")), ";"],
383
+ hasCondition ? [path.call(print, "conditionNode"), ";"] : ";",
384
+ hasUpdate ? printExpressionList(path.map(print, "updateNodes")) : ""
385
+ ];
386
+ const hasEmptyStatement = path.node.bodyNode.type === ";";
387
+ return [
388
+ ...danglingComments,
389
+ "for ",
390
+ hasInit || hasCondition || hasUpdate ? group$6(indentInParentheses(join$6(line$6, expressions))) : "(;;)",
391
+ hasEmptyStatement ? ";" : [" ", path.call(print, "bodyNode")]
392
+ ];
393
+ },
394
+ update_expression(path, print) {
395
+ return path.map(print, "children");
396
+ },
397
+ enhanced_for_statement(path, print) {
398
+ const forStatement = printDanglingComments(path);
399
+ forStatement.push("for (", ...printModifiers(path, print), path.call(print, "typeNode"), " ", path.call(print, "nameNode"));
400
+ if (hasChild(path, "dimensionsNode")) forStatement.push(path.call(print, "dimensionsNode"));
401
+ forStatement.push(" : ", path.call(print, "valueNode"), ")");
402
+ const bodyType = path.node.bodyNode.type;
403
+ if (bodyType === ";") forStatement.push(";");
404
+ else {
405
+ const body = path.call(print, "bodyNode");
406
+ forStatement.push(bodyType === "block" ? [" ", body] : indent$6([line$6, body]));
407
+ }
408
+ return group$6(forStatement);
409
+ },
410
+ break_statement(path, print) {
411
+ const parts = ["break"];
412
+ const identifierIndex = path.node.namedChildren.findIndex(({ type }) => type === "identifier");
413
+ if (identifierIndex !== -1) parts.push(" ", path.call(print, "namedChildren", identifierIndex));
414
+ parts.push(";");
415
+ return parts;
416
+ },
417
+ continue_statement(path, print) {
418
+ const identifierIndex = path.node.namedChildren.findIndex(({ type }) => type === "identifier");
419
+ return identifierIndex !== -1 ? [
420
+ "continue ",
421
+ path.call(print, "namedChildren", identifierIndex),
422
+ ";"
423
+ ] : "continue;";
424
+ },
425
+ return_statement(path, print) {
426
+ const statement = ["return"];
427
+ if (path.node.namedChildren.length) {
428
+ statement.push(" ");
429
+ const expression = path.call(print, "namedChildren", 0);
430
+ if (path.node.namedChildren[0].type === "binary_expression") statement.push(group$6([
431
+ ifBreak$1("("),
432
+ indent$6([softline$4, expression]),
433
+ softline$4,
434
+ ifBreak$1(")")
435
+ ]));
436
+ else statement.push(expression);
437
+ }
438
+ statement.push(";");
439
+ return statement;
440
+ },
441
+ throw_statement(path, print) {
442
+ return [
443
+ "throw ",
444
+ path.call(print, "namedChildren", 0),
445
+ ";"
446
+ ];
447
+ },
448
+ synchronized_statement(path, print) {
449
+ const parenthesizedExpressionIndex = path.node.namedChildren.findIndex(({ type }) => type === "parenthesized_expression");
450
+ return [
451
+ "synchronized ",
452
+ path.call(print, "namedChildren", parenthesizedExpressionIndex),
453
+ " ",
454
+ path.call(print, "bodyNode")
455
+ ];
456
+ },
457
+ try_statement(path, print) {
458
+ const parts = ["try", path.call(print, "bodyNode")];
459
+ path.each((child) => {
460
+ if (child.node.type === "catch_clause" || child.node.type === "finally_clause") parts.push(print(child));
461
+ }, "namedChildren");
462
+ return join$6(" ", parts);
463
+ },
464
+ catch_clause(path, print) {
465
+ const catchFormalParameterIndex = path.node.namedChildren.findIndex(({ type }) => type === "catch_formal_parameter");
466
+ return [
467
+ "catch ",
468
+ group$6(indentInParentheses(path.call(print, "namedChildren", catchFormalParameterIndex))),
469
+ " ",
470
+ path.call(print, "bodyNode")
471
+ ];
472
+ },
473
+ catch_formal_parameter(path, print) {
474
+ const parts = printModifiers(path, print);
475
+ const catchTypeIndex = path.node.namedChildren.findIndex(({ type }) => type === "catch_type");
476
+ parts.push(path.call(print, "namedChildren", catchTypeIndex), " ", path.call(print, "nameNode"));
477
+ if (hasChild(path, "dimensionsNode")) parts.push(path.call(print, "dimensionsNode"));
478
+ return parts;
479
+ },
480
+ catch_type(path, print) {
481
+ return join$6([line$6, "| "], path.map(print, "namedChildren"));
482
+ },
483
+ finally_clause(path, print) {
484
+ return ["finally ", path.call(print, "namedChildren", 0)];
485
+ },
486
+ try_with_resources_statement(path, print) {
487
+ const parts = [
488
+ "try",
489
+ path.call(print, "resourcesNode"),
490
+ path.call(print, "bodyNode")
491
+ ];
492
+ path.each((child) => {
493
+ if (child.node.type === "catch_clause" || child.node.type === "finally_clause") parts.push(print(child));
494
+ }, "namedChildren");
495
+ return join$6(" ", parts);
496
+ },
497
+ resource_specification(path, print) {
498
+ const resources = [];
499
+ let hasTrailingSemicolon = false;
500
+ path.each((child) => {
501
+ if (child.node.type === "resource") {
502
+ resources.push(print(child));
503
+ hasTrailingSemicolon = false;
504
+ } else if (child.node.type === ";") hasTrailingSemicolon = true;
505
+ }, "children");
506
+ const parts = join$6([";", line$6], resources);
507
+ if (hasTrailingSemicolon) parts.push(ifBreak$1(";"));
508
+ return group$6(indentInParentheses(parts));
509
+ },
510
+ resource(path, print) {
511
+ if (hasChild(path, "typeNode") && hasChild(path, "nameNode") && hasChild(path, "valueNode")) {
512
+ const parts = printModifiers(path, print);
513
+ parts.push(path.call(print, "typeNode"), " ", path.call(print, "nameNode"));
514
+ if (hasChild(path, "dimensionsNode")) parts.push(path.call(print, "dimensionsNode"));
515
+ parts.push(" =");
516
+ const value = path.call(print, "valueNode");
517
+ if (path.node.valueNode.type === "binary_expression" || hasLeadingComments(path.node.valueNode)) parts.push(group$6(indent$6([line$6, value])));
518
+ else {
519
+ const groupId = Symbol("assignment");
520
+ parts.push(group$6(indent$6(line$6), { id: groupId }), indentIfBreak$2(value, { groupId }));
521
+ }
522
+ return parts;
523
+ }
524
+ const resourceIndex = path.node.namedChildren.findIndex(({ type }) => type === "identifier" || type === "field_access");
525
+ return path.call(print, "namedChildren", resourceIndex);
526
+ },
527
+ yield_statement(path, print) {
528
+ return [
529
+ "yield ",
530
+ path.call(print, "namedChildren", 0),
531
+ ";"
532
+ ];
533
+ }
534
+ };
535
+ function printExpressionList(expressions) {
536
+ return group$6(expressions.map((expression, index) => index === 0 ? expression : [",", indent$6([line$6, expression])]));
537
+ }
538
+ //#endregion
539
+ //#region src/printers/classes.ts
540
+ const { group: group$5, hardline: hardline$4, indent: indent$5, indentIfBreak: indentIfBreak$1, join: join$5, line: line$5, softline: softline$3 } = prettier_doc.builders;
541
+ var classes_default = {
542
+ class_declaration(path, print) {
543
+ const parts = ["class ", path.call(print, "nameNode")];
544
+ const definedClauses = definedKeys(path.node, [
545
+ "superclassNode",
546
+ "interfacesNode",
547
+ "permitsNode"
548
+ ]);
549
+ const hasMultipleClauses = definedClauses.length > 1;
550
+ const hasTypeParameters = hasChild(path, "type_parametersNode");
551
+ if (hasTypeParameters) {
552
+ const typeParameters = path.call(print, "type_parametersNode");
553
+ parts.push(hasMultipleClauses ? group$5(indent$5(typeParameters)) : typeParameters);
554
+ }
555
+ if (definedClauses.length) {
556
+ const separator = hasTypeParameters && !hasMultipleClauses ? " " : line$5;
557
+ const clauses = definedClauses.flatMap((clause) => hasChild(path, clause) ? [separator, path.call(print, clause)] : []);
558
+ const hasBody = path.node.bodyNode.namedChildren.length > 0;
559
+ const clauseGroup = [hasTypeParameters && !hasMultipleClauses ? clauses : indent$5(clauses), hasBody ? separator : " "];
560
+ parts.push(hasMultipleClauses ? clauseGroup : group$5(clauseGroup));
561
+ } else parts.push(" ");
562
+ return [
563
+ ...printModifiers(path, print, "declarationOnly"),
564
+ group$5(parts),
565
+ path.call(print, "bodyNode")
566
+ ];
567
+ },
568
+ type_parameters(path, print) {
569
+ return [
570
+ "<",
571
+ indent$5([softline$3, join$5([",", line$5], path.map(print, "namedChildren"))]),
572
+ softline$3,
573
+ ">"
574
+ ];
575
+ },
576
+ superclass(path, print) {
577
+ return ["extends ", path.call(print, "namedChildren", 0)];
578
+ },
579
+ super_interfaces(path, print) {
580
+ return group$5(["implements", indent$5([line$5, path.call(print, "namedChildren", 0)])]);
581
+ },
582
+ permits(path, print) {
583
+ return group$5(["permits", indent$5([line$5, path.call(print, "namedChildren", 0)])]);
584
+ },
585
+ type_list(path, print) {
586
+ return join$5([",", line$5], path.map(print, "namedChildren"));
587
+ },
588
+ class_body(path, print) {
589
+ return printBlock(path, printBodyDeclarations(path, print, path.parent?.type === "class_declaration"));
590
+ },
591
+ field_declaration: printVariableDeclaration,
592
+ variable_declarator(path, print) {
593
+ const declarator = [path.call(print, "nameNode")];
594
+ if (hasChild(path, "dimensionsNode")) declarator.push(path.call(print, "dimensionsNode"));
595
+ if (!hasChild(path, "valueNode")) return declarator;
596
+ declarator.push(" =");
597
+ const value = path.call(print, "valueNode");
598
+ if (path.node.valueNode.type === "binary_expression" || path.node.valueNode.type === "ternary_expression" && path.node.valueNode.conditionNode.type === "binary_expression" || hasLeadingComments(path.node.valueNode)) declarator.push(group$5(indent$5([line$5, value])));
599
+ else {
600
+ const groupId = Symbol("assignment");
601
+ declarator.push(group$5(indent$5(line$5), { id: groupId }), indentIfBreak$1(value, { groupId }));
602
+ }
603
+ return group$5(declarator);
604
+ },
605
+ method_declaration(path, print) {
606
+ const modifiers = printModifiers(path, print);
607
+ const declaration = [];
608
+ if (hasChild(path, "type_parametersNode")) declaration.push(group$5(path.call(print, "type_parametersNode")), " ");
609
+ declaration.push(path.call(print, "typeNode"));
610
+ if (hasChild(path, "dimensionsNode")) declaration.push(path.call(print, "dimensionsNode"));
611
+ declaration.push(" ", path.call(print, "nameNode"), group$5(path.call(print, "parametersNode")));
612
+ const throwsIndex = path.node.namedChildren.findIndex(({ type }) => type === "throws");
613
+ if (throwsIndex !== -1) declaration.push(group$5(indent$5([line$5, path.call(print, "namedChildren", throwsIndex)])));
614
+ return hasChild(path, "bodyNode") ? [
615
+ modifiers,
616
+ group$5(declaration),
617
+ " ",
618
+ path.call(print, "bodyNode")
619
+ ] : [
620
+ modifiers,
621
+ group$5(declaration),
622
+ ";"
623
+ ];
624
+ },
625
+ receiver_parameter(path, print) {
626
+ return path.map((child) => child.isLast ? print(child) : [print(child), child.node.type === "identifier" ? "." : " "], "namedChildren");
627
+ },
628
+ formal_parameters(path, print) {
629
+ return indentInParentheses(join$5([",", line$5], path.parent?.type === "record_declaration" ? printBodyDeclarations(path, print) : path.map(print, "namedChildren")));
630
+ },
631
+ formal_parameter(path, print) {
632
+ const parameter = printModifiers(path, print, path.grandparent?.type === "record_declaration" ? "avoidBreak" : "noBreak");
633
+ parameter.push(path.call(print, "typeNode"));
634
+ if (hasChild(path, "dimensionsNode")) parameter.push(path.call(print, "dimensionsNode"));
635
+ parameter.push(" ", path.call(print, "nameNode"));
636
+ return group$5(parameter);
637
+ },
638
+ spread_parameter(path, print) {
639
+ const parts = printModifiers(path, print, "noBreak");
640
+ parts.push(path.call(print, "typeNode"));
641
+ if (hasChild(path, "annotationsNodes")) parts.push(...path.map((annotation) => [" ", print(annotation)], "annotationsNodes"));
642
+ parts.push("... ", path.call(print, "namedChildren", path.node.namedChildren.length - 1));
643
+ return parts;
644
+ },
645
+ throws(path, print) {
646
+ return ["throws ", ...join$5(", ", path.map(print, "namedChildren"))];
647
+ },
648
+ static_initializer(path, print) {
649
+ return ["static ", path.call(print, "namedChildren", 0)];
650
+ },
651
+ constructor_declaration(path, print) {
652
+ const modifiers = printModifiers(path, print, "declarationOnly");
653
+ const declaration = [];
654
+ if (hasChild(path, "type_parametersNode")) declaration.push(group$5(path.call(print, "type_parametersNode")), " ");
655
+ declaration.push(path.call(print, "nameNode"), group$5(path.call(print, "parametersNode")));
656
+ const throwsIndex = path.node.namedChildren.findIndex(({ type }) => type === "throws");
657
+ if (throwsIndex !== -1) declaration.push(group$5(indent$5([line$5, path.call(print, "namedChildren", throwsIndex)])));
658
+ return [
659
+ modifiers,
660
+ group$5(declaration),
661
+ " ",
662
+ path.call(print, "bodyNode")
663
+ ];
664
+ },
665
+ constructor_body(path, print) {
666
+ return printBlock(path, printBlockStatements(path, print));
667
+ },
668
+ explicit_constructor_invocation(path, print) {
669
+ const invocation = [];
670
+ if (hasChild(path, "objectNode")) invocation.push(path.call(print, "objectNode"), ".");
671
+ if (hasChild(path, "type_argumentsNode")) invocation.push(path.call(print, "type_argumentsNode"));
672
+ invocation.push(path.call(print, "constructorNode"), path.call(print, "argumentsNode"), ";");
673
+ return invocation;
674
+ },
675
+ visibility: printValue,
676
+ modifier: printValue,
677
+ modifiers(path, print, _, args) {
678
+ const parts = [];
679
+ const modifiers = [];
680
+ const typeAnnotations = [];
681
+ path.each((child) => {
682
+ if (child.node.type === "annotation" || child.node.type === "marker_annotation") (modifiers.length ? typeAnnotations : parts).push(print(child));
683
+ else {
684
+ modifiers.push(child.node.value);
685
+ parts.push(...typeAnnotations);
686
+ typeAnnotations.length = 0;
687
+ }
688
+ }, "namedChildren");
689
+ const annotationMode = args && typeof args === "object" && "annotationMode" in args ? args.annotationMode : null;
690
+ if (annotationMode === "declarationOnly") {
691
+ parts.push(...typeAnnotations);
692
+ typeAnnotations.length = 0;
693
+ }
694
+ modifiers.sort((a, b) => (indexByModifier.get(a) ?? -1) - (indexByModifier.get(b) ?? -1));
695
+ if (modifiers.length || typeAnnotations.length) parts.push(join$5(" ", [...modifiers, ...typeAnnotations]));
696
+ return join$5(annotationMode === "avoidBreak" ? line$5 : annotationMode === "noBreak" ? " " : hardline$4, parts);
697
+ },
698
+ enum_declaration(path, print) {
699
+ const parts = printModifiers(path, print);
700
+ parts.push("enum ", path.call(print, "nameNode"));
701
+ if (hasChild(path, "interfacesNode")) {
702
+ const hasBody = path.node.bodyNode.namedChildren.length > 0;
703
+ parts.push(indent$5([line$5, path.call(print, "interfacesNode")]), hasBody ? line$5 : " ");
704
+ } else parts.push(" ");
705
+ return [group$5(parts), path.call(print, "bodyNode")];
706
+ },
707
+ enum_body(path, print, options) {
708
+ const parts = printBodyDeclarations(path, print);
709
+ const enumBodyDeclarationsIndex = path.node.namedChildren.findIndex(({ type }) => type === "enum_body_declarations");
710
+ const declarations = [];
711
+ if (enumBodyDeclarationsIndex !== -1) {
712
+ const hasDeclarations = path.node.namedChildren[enumBodyDeclarationsIndex].namedChildren.length > 0;
713
+ const enumBodyDeclarations = parts.pop();
714
+ if (hasDeclarations) {
715
+ if (!parts.length) declarations.push(hardline$4);
716
+ declarations.push(enumBodyDeclarations);
717
+ }
718
+ }
719
+ const contents = [];
720
+ if (parts.length) {
721
+ contents.push(join$5([",", hardline$4], parts));
722
+ if (!declarations.length && options.trailingComma !== "none") contents.push(",");
723
+ }
724
+ if (declarations.length) contents.push(";", hardline$4, ...declarations);
725
+ return printBlock(path, contents.length ? [contents] : []);
726
+ },
727
+ enum_constant(path, print) {
728
+ const initializer = printModifiers(path, print);
729
+ initializer.push(path.call(print, "nameNode"));
730
+ if (hasChild(path, "argumentsNode")) initializer.push(path.call(print, "argumentsNode"));
731
+ if (hasChild(path, "bodyNode")) initializer.push(" ", path.call(print, "bodyNode"));
732
+ return initializer;
733
+ },
734
+ enum_body_declarations(path, print) {
735
+ return join$5(hardline$4, printBodyDeclarations(path, print));
736
+ },
737
+ record_declaration(path, print) {
738
+ const parts = printModifiers(path, print, "declarationOnly");
739
+ parts.push("record ", path.call(print, "nameNode"));
740
+ if (hasChild(path, "type_parametersNode")) parts.push(group$5(path.call(print, "type_parametersNode")));
741
+ parts.push(path.call(print, "parametersNode"));
742
+ if (hasChild(path, "interfacesNode")) {
743
+ const hasParameters = path.node.parametersNode.namedChildren.length > 0;
744
+ const hasBody = path.node.bodyNode.namedChildren.length > 0;
745
+ const interfaces = [hasParameters ? " " : line$5, path.call(print, "interfacesNode")];
746
+ parts.push(group$5([hasParameters ? interfaces : indent$5(interfaces), hasBody ? line$5 : " "]));
747
+ } else parts.push(" ");
748
+ return [group$5(parts), path.call(print, "bodyNode")];
749
+ },
750
+ compact_constructor_declaration(path, print) {
751
+ const parts = printModifiers(path, print, "declarationOnly");
752
+ parts.push(path.call(print, "nameNode"), " ", path.call(print, "bodyNode"));
753
+ return parts;
754
+ }
755
+ };
756
+ const indexByModifier = [
757
+ "public",
758
+ "protected",
759
+ "private",
760
+ "abstract",
761
+ "default",
762
+ "static",
763
+ "final",
764
+ "transient",
765
+ "volatile",
766
+ "synchronized",
767
+ "native",
768
+ "sealed",
769
+ "non-sealed",
770
+ "strictfp"
771
+ ].reduce((map, name, index) => map.set(name, index), /* @__PURE__ */ new Map());
772
+ //#endregion
773
+ //#region src/printers/expressions.ts
774
+ const { align, breakParent: breakParent$1, conditionalGroup, group: group$4, hardline: hardline$3, ifBreak, indent: indent$4, indentIfBreak, join: join$4, line: line$4, softline: softline$2 } = prettier_doc.builders;
775
+ const { getNextNonSpaceNonCommentCharacterIndex, hasNewline: hasNewline$1, isNextLineEmpty } = prettier.util;
776
+ const { removeLines, willBreak } = prettier_doc.utils;
777
+ var expressions_default = {
778
+ lambda_expression(path, print, options, args) {
779
+ const signatureDocs = [];
780
+ let bodyDoc;
781
+ const bodyComments = [];
782
+ const shouldPrintAsChain = !(args && typeof args === "object" && "expandLastArg" in args && args.expandLastArg) && path.node.bodyNode.type === "lambda_expression";
783
+ let functionBody;
784
+ (function rec() {
785
+ const { node } = path;
786
+ const signatureDoc = printLambdaExpressionSignature(path, options, print, args);
787
+ if (signatureDocs.length === 0) signatureDocs.push(signatureDoc);
788
+ else {
789
+ const { leading, trailing } = printCommentsSeparately(path);
790
+ signatureDocs.push([leading, signatureDoc]);
791
+ bodyComments.unshift(trailing);
792
+ }
793
+ if (!shouldPrintAsChain || node.bodyNode.type !== "lambda_expression") {
794
+ bodyDoc = path.call((child) => print(child, args), "bodyNode");
795
+ functionBody = node.bodyNode;
796
+ } else path.call(rec, "bodyNode");
797
+ })();
798
+ const shouldPutBodyOnSameLine = !functionBody.comments?.some(({ leading }) => leading && hasNewline$1(options.originalText, functionBody.end.index)) && mayBreakAfterShortPrefix(functionBody);
799
+ const isCallee = path.node.fieldName === "object" && path.parent?.type === "method_invocation";
800
+ const chainGroupId = Symbol("arrow-chain");
801
+ const signaturesDoc = printArrowFunctionSignatures(path, { signatureDocs });
802
+ let shouldBreakSignatures = false;
803
+ let shouldIndentSignatures = false;
804
+ let shouldPrintSoftlineInIndent = false;
805
+ if (shouldPrintAsChain && isCallee) {
806
+ shouldIndentSignatures = true;
807
+ shouldPrintSoftlineInIndent = !path.node.comments?.some(({ type, leading }) => leading && type === "line_comment");
808
+ shouldBreakSignatures = isCallee && !shouldPutBodyOnSameLine;
809
+ }
810
+ const trailingSpace = args && typeof args === "object" && "expandLastArg" in args && args.expandLastArg && !path.node.comments ? softline$2 : "";
811
+ bodyDoc = shouldPutBodyOnSameLine ? [
812
+ " ",
813
+ bodyDoc,
814
+ bodyComments
815
+ ] : [indent$4([
816
+ line$4,
817
+ bodyDoc,
818
+ bodyComments
819
+ ]), trailingSpace];
820
+ return group$4([
821
+ group$4(shouldIndentSignatures ? indent$4([shouldPrintSoftlineInIndent ? softline$2 : "", signaturesDoc]) : signaturesDoc, {
822
+ shouldBreak: shouldBreakSignatures,
823
+ id: chainGroupId
824
+ }),
825
+ " ->",
826
+ shouldPrintAsChain ? indentIfBreak(bodyDoc, { groupId: chainGroupId }) : group$4(bodyDoc),
827
+ shouldPrintAsChain && isCallee ? ifBreak(softline$2, "", { groupId: chainGroupId }) : ""
828
+ ]);
829
+ },
830
+ inferred_parameters(path, print, options) {
831
+ const identifiers = [];
832
+ path.each((child) => {
833
+ if (child.node.type === "identifier") identifiers.push(print(child));
834
+ }, "namedChildren");
835
+ if (!identifiers.length) return "()";
836
+ const parameters = join$4([",", line$4], identifiers);
837
+ if (identifiers.length > 1) return group$4(indentInParentheses(parameters));
838
+ return options.arrowParens === "avoid" ? parameters : [
839
+ "(",
840
+ ...parameters,
841
+ ")"
842
+ ];
843
+ },
844
+ ternary_expression(path, print, options) {
845
+ const condition = path.call(print, "conditionNode");
846
+ const consequence = path.call(print, "consequenceNode");
847
+ const alternative = path.call(print, "alternativeNode");
848
+ const parentType = path.parent?.type;
849
+ const isNestedTernary = parentType === "ternary_expression";
850
+ const suffix = [
851
+ line$4,
852
+ ["? ", options.useTabs ? indent$4(consequence) : align(2, consequence)],
853
+ line$4,
854
+ [": ", options.useTabs ? indent$4(alternative) : align(2, alternative)]
855
+ ];
856
+ const prefix = group$4(condition);
857
+ const alignedSuffix = !isNestedTernary || options.useTabs ? suffix : align(Math.max(0, options.tabWidth - 2), suffix);
858
+ if (isNestedTernary) return [prefix, alignedSuffix];
859
+ const parts = [prefix, indent$4(alignedSuffix)];
860
+ return parentType === "parenthesized_expression" ? parts : group$4(parts);
861
+ },
862
+ assignment_expression(path, print) {
863
+ const { operatorNode, rightNode } = path.node;
864
+ const parts = [
865
+ path.call(print, "leftNode"),
866
+ " ",
867
+ operatorNode.type
868
+ ];
869
+ const right = path.call(print, "rightNode");
870
+ if (rightNode.type === "binary_expression" || rightNode.type === "ternary_expression" && rightNode.conditionNode.type === "binary_expression" || hasLeadingComments(rightNode)) parts.push(group$4(indent$4([line$4, right])));
871
+ else {
872
+ const groupId = Symbol("assignment");
873
+ parts.push(group$4(indent$4(line$4), { id: groupId }), indentIfBreak(right, { groupId }));
874
+ }
875
+ return parts;
876
+ },
877
+ binary_expression(path, print, options) {
878
+ const { node } = path;
879
+ const parent = path.parent;
880
+ const grandparent = path.grandparent;
881
+ const isInsideParentheses = parent?.fieldName === "condition" && (grandparent?.type === "if_statement" || grandparent?.type === "while_statement" || grandparent?.type === "switch_expression" || grandparent?.type === "do_statement") || parent?.fieldName !== "body" && grandparent?.type === "synchronized_statement";
882
+ const parts = printBinaryExpressions(path, print, options, isInsideParentheses);
883
+ if (isInsideParentheses) return parts;
884
+ if (parent?.fieldName === "object" && (grandparent?.type === "method_invocation" || grandparent?.type === "explicit_constructor_invocation" || grandparent?.type === "field_access") || grandparent?.type === "method_reference") return parts;
885
+ if (parent?.type === "return_statement" || parent?.type === "throw_statement" || parent?.type === "parenthesized_expression" || parent?.type === "assignment_expression" || parent?.type === "variable_declarator" || parent?.type === "guard" || node.fieldName === "body" && parent?.type === "lambda_expression" || node.fieldName !== "body" && parent?.type === "for_statement" || parent?.type === "ternary_expression" && grandparent?.type !== "return_statement" && grandparent?.type !== "throw_statement" && grandparent?.type !== "parenthesized_expression" && grandparent?.type !== "argument_list" || node.fieldName === "operand" && parent?.type === "unary_expression") return group$4(parts);
886
+ if (parts.length === 0) return "";
887
+ const firstGroupIndex = parts.findIndex((part) => typeof part !== "string" && !Array.isArray(part) && part.type === "group");
888
+ const headParts = parts.slice(0, firstGroupIndex === -1 ? 1 : firstGroupIndex + 1);
889
+ const rest = parts.slice(headParts.length);
890
+ return group$4([...headParts, indent$4(rest)]);
891
+ },
892
+ instanceof_expression(path, print, options) {
893
+ return group$4(indent$4(path.map((child) => {
894
+ const doc = print(child, { annotationMode: "noBreak" });
895
+ if (!child.previous) return doc;
896
+ return [(options.experimentalOperatorPosition === "start" ? child.node.type === "instanceof" : child.previous.type === "instanceof") ? line$4 : " ", doc];
897
+ }, "children")));
898
+ },
899
+ unary_expression(path, print) {
900
+ return path.map(print, "children");
901
+ },
902
+ field_access: printMemberChain,
903
+ generic_type(path, print) {
904
+ const typeIdentifierIndex = path.node.namedChildren.findIndex(({ type }) => type === "scoped_type_identifier" || type === "type_identifier");
905
+ const typeArgumentsIndex = path.node.namedChildren.findIndex(({ type }) => type === "type_arguments");
906
+ return [path.call(print, "namedChildren", typeIdentifierIndex), path.call(print, "namedChildren", typeArgumentsIndex)];
907
+ },
908
+ parenthesized_expression(path, print) {
909
+ const expression = path.call(print, "namedChildren", 0);
910
+ const parentType = path.parent?.type;
911
+ const grandparentType = path.grandparent?.type;
912
+ const expressionType = path.node.namedChildren[0].type;
913
+ const hasLambda = expressionType === "lambda_expression";
914
+ const hasTernary = expressionType === "ternary_expression";
915
+ const hasSuffix = parentType && (parentType === "array_access" || parentType === "explicit_constructor_invocation" || parentType === "field_access" || parentType === "method_invocation" || parentType === "method_reference" || parentType === "object_creation_expression");
916
+ const isAssignment = parentType && (parentType === "assignment_expression" || parentType === "variable_declarator") || hasSuffix && (grandparentType === "assignment_expression" || grandparentType === "variable_declarator");
917
+ if (!hasLambda && hasSuffix && (!hasTernary || isAssignment)) return group$4(indentInParentheses(hasTernary ? group$4(expression) : expression));
918
+ else if (parentType && (parentType === "guard" || parentType === "return_statement" || parentType === "unary_expression" && grandparentType === "return_statement" || path.node.fieldName === "condition" && (parentType === "do_statement" || parentType === "if_statement" || parentType === "switch_expression" || parentType === "while_statement") || path.node.fieldName !== "body" && parentType === "synchronized_statement")) return group$4(indentInParentheses(group$4(expression)));
919
+ else if (hasTernary && hasSuffix && !isAssignment) return group$4([
920
+ "(",
921
+ expression,
922
+ softline$2,
923
+ ")"
924
+ ]);
925
+ else return group$4([
926
+ "(",
927
+ hasLambda || hasTernary ? expression : indent$4(expression),
928
+ ")"
929
+ ]);
930
+ },
931
+ cast_expression(path, print) {
932
+ const types = path.map(print, "typeNodes");
933
+ const value = path.call(print, "valueNode");
934
+ return types.length > 1 ? [
935
+ group$4(indentInParentheses(join$4([line$4, "& "], types))),
936
+ " ",
937
+ value
938
+ ] : [
939
+ "(",
940
+ ...types,
941
+ ") ",
942
+ value
943
+ ];
944
+ },
945
+ object_creation_expression(path, print) {
946
+ const expression = [];
947
+ path.each((child) => {
948
+ if (child.node.type === "class_body") expression.push(" ");
949
+ expression.push(print(child));
950
+ if (child.node.type === "annotation" || child.node.type === "marker_annotation" || child.node.type === "new") expression.push(" ");
951
+ }, "children");
952
+ return expression;
953
+ },
954
+ method_invocation: printMemberChain,
955
+ argument_list(path, print, options) {
956
+ const args = path.node.namedChildren;
957
+ if (args.length === 0) {
958
+ const shouldBreak = path.node.comments?.some(({ type, leading, trailing }) => !leading && !trailing && type === "line_comment");
959
+ return group$4(indentInParentheses(printDanglingComments(path)), { shouldBreak });
960
+ }
961
+ const lastArgIndex = args.length - 1;
962
+ let anyArgEmptyLine = false;
963
+ const printedArguments = [];
964
+ path.each((arg, index) => {
965
+ let argDoc = print(arg);
966
+ if (index === lastArgIndex) {} else if (isNextLineEmpty(options.originalText, arg.node.end.index)) {
967
+ anyArgEmptyLine = true;
968
+ argDoc = [
969
+ argDoc,
970
+ ",",
971
+ hardline$3,
972
+ hardline$3
973
+ ];
974
+ } else argDoc = [
975
+ argDoc,
976
+ ",",
977
+ line$4
978
+ ];
979
+ printedArguments.push(argDoc);
980
+ }, "namedChildren");
981
+ function allArgsBrokenOut() {
982
+ return group$4([
983
+ "(",
984
+ indent$4([line$4, ...printedArguments]),
985
+ line$4,
986
+ ")"
987
+ ], { shouldBreak: true });
988
+ }
989
+ if (anyArgEmptyLine) return allArgsBrokenOut();
990
+ if (shouldExpandFirstArg(args)) {
991
+ const tailArgs = printedArguments.slice(1);
992
+ if (tailArgs.some(willBreak)) return allArgsBrokenOut();
993
+ let firstArg;
994
+ try {
995
+ firstArg = path.call((arg) => print(arg, { expandFirstArg: true }), "namedChildren", 0);
996
+ } catch (caught) {
997
+ if (caught instanceof ArgExpansionBailout) return allArgsBrokenOut();
998
+ throw caught;
999
+ }
1000
+ if (willBreak(firstArg)) return [breakParent$1, conditionalGroup([[
1001
+ "(",
1002
+ group$4(firstArg, { shouldBreak: true }),
1003
+ ", ",
1004
+ ...tailArgs,
1005
+ ")"
1006
+ ], allArgsBrokenOut()])];
1007
+ return conditionalGroup([
1008
+ [
1009
+ "(",
1010
+ firstArg,
1011
+ ", ",
1012
+ ...tailArgs,
1013
+ ")"
1014
+ ],
1015
+ [
1016
+ "(",
1017
+ group$4(firstArg, { shouldBreak: true }),
1018
+ ", ",
1019
+ ...tailArgs,
1020
+ ")"
1021
+ ],
1022
+ allArgsBrokenOut()
1023
+ ]);
1024
+ }
1025
+ if (shouldExpandLastArg(args)) {
1026
+ const headArgs = printedArguments.slice(0, -1);
1027
+ if (headArgs.some(willBreak)) return allArgsBrokenOut();
1028
+ let lastArg;
1029
+ try {
1030
+ lastArg = path.call((arg) => print(arg, { expandLastArg: true }), "namedChildren", lastArgIndex);
1031
+ } catch (caught) {
1032
+ if (caught instanceof ArgExpansionBailout) return allArgsBrokenOut();
1033
+ throw caught;
1034
+ }
1035
+ if (willBreak(lastArg)) return [breakParent$1, conditionalGroup([[
1036
+ "(",
1037
+ ...headArgs,
1038
+ group$4(lastArg, { shouldBreak: true }),
1039
+ ")"
1040
+ ], allArgsBrokenOut()])];
1041
+ return conditionalGroup([
1042
+ [
1043
+ "(",
1044
+ ...headArgs,
1045
+ lastArg,
1046
+ ")"
1047
+ ],
1048
+ [
1049
+ "(",
1050
+ ...headArgs,
1051
+ group$4(lastArg, { shouldBreak: true }),
1052
+ ")"
1053
+ ],
1054
+ allArgsBrokenOut()
1055
+ ]);
1056
+ }
1057
+ return group$4(indentInParentheses(printedArguments), { shouldBreak: printedArguments.some(willBreak) || anyArgEmptyLine });
1058
+ },
1059
+ array_creation_expression(path, print) {
1060
+ const parts = ["new "];
1061
+ path.each((child) => {
1062
+ if (child.node.type === "annotation" || child.node.type === "marker_annotation") parts.push(print(child), " ");
1063
+ }, "namedChildren");
1064
+ parts.push(path.call(print, "typeNode"), ...path.map(print, "dimensionsNodes"));
1065
+ if (hasChild(path, "valueNode")) parts.push(" ", path.call(print, "valueNode"));
1066
+ return parts;
1067
+ },
1068
+ dimensions_expr(path, print) {
1069
+ return path.map((child) => child.node.type === "annotation" || child.node.type === "marker_annotation" ? [print(child), " "] : [
1070
+ "[",
1071
+ print(child),
1072
+ "]"
1073
+ ], "namedChildren");
1074
+ },
1075
+ class_literal(path, print) {
1076
+ return [path.call(print, "namedChildren", 0), ".class"];
1077
+ },
1078
+ array_access: printMemberChain,
1079
+ method_reference(path, print) {
1080
+ return path.map(print, "children");
1081
+ },
1082
+ template_expression(path, print) {
1083
+ return [
1084
+ path.call(print, "template_processorNode"),
1085
+ ".",
1086
+ path.call(print, "template_argumentNode")
1087
+ ];
1088
+ },
1089
+ pattern(path, print) {
1090
+ return path.call(print, "namedChildren", 0);
1091
+ },
1092
+ type_pattern(path, print) {
1093
+ return join$4(" ", path.map(print, "children"));
1094
+ },
1095
+ record_pattern(path, print) {
1096
+ return path.map(print, "children");
1097
+ },
1098
+ record_pattern_body(path, print) {
1099
+ return group$4(indentInParentheses(join$4([",", line$4], path.map(print, "namedChildren"))));
1100
+ },
1101
+ record_pattern_component(path, print) {
1102
+ return join$4(" ", path.map(print, "children"));
1103
+ },
1104
+ guard(path, print) {
1105
+ const hasParentheses = path.node.namedChildren[0].type === "parenthesized_expression";
1106
+ const expression = path.call(print, "namedChildren", 0);
1107
+ return ["when ", hasParentheses ? expression : group$4([
1108
+ ifBreak("("),
1109
+ indent$4([softline$2, expression]),
1110
+ softline$2,
1111
+ ifBreak(")")
1112
+ ])];
1113
+ }
1114
+ };
1115
+ function printLambdaExpressionSignature(path, options, print, args) {
1116
+ const parts = [];
1117
+ const parameters = path.call(print, "parametersNode");
1118
+ if (shouldPrintParamsWithoutParens(path, options)) parts.push(parameters);
1119
+ else {
1120
+ if (args != null && typeof args === "object" && ("expandLastArg" in args && args.expandLastArg === true || "expandFirstArg" in args && args.expandFirstArg === true)) {
1121
+ if (willBreak(parameters)) throw new ArgExpansionBailout();
1122
+ parts.push(group$4(removeLines(parameters)));
1123
+ } else parts.push(parameters);
1124
+ if (path.node.parametersNode.type === "identifier") {
1125
+ parts.unshift("(");
1126
+ parts.push(")");
1127
+ }
1128
+ }
1129
+ const dangling = printDanglingComments(path);
1130
+ if (dangling.length) parts.push(" ", dangling);
1131
+ return parts;
1132
+ }
1133
+ function mayBreakAfterShortPrefix(functionBody) {
1134
+ return functionBody.type === "array_creation_expression" || functionBody.type === "lambda_expression" || functionBody.type === "block";
1135
+ }
1136
+ function printArrowFunctionSignatures(path, { signatureDocs }) {
1137
+ if (signatureDocs.length === 1) return signatureDocs[0];
1138
+ const { node, parent } = path;
1139
+ if (node.fieldName !== "object" && parent?.type === "method_invocation" || parent?.type === "binary_expression") return group$4([
1140
+ signatureDocs[0],
1141
+ " ->",
1142
+ indent$4([line$4, join$4([" ->", line$4], signatureDocs.slice(1))])
1143
+ ]);
1144
+ if (node.fieldName === "object" && parent?.type === "method_invocation") return group$4(join$4([" ->", line$4], signatureDocs));
1145
+ return group$4(indent$4(join$4([" ->", line$4], signatureDocs)));
1146
+ }
1147
+ function shouldPrintParamsWithoutParens(path, options) {
1148
+ if (options.arrowParens === "always") return false;
1149
+ if (options.arrowParens === "avoid") {
1150
+ const { node } = path;
1151
+ return canPrintParamsWithoutParens(node);
1152
+ }
1153
+ return false;
1154
+ }
1155
+ function canPrintParamsWithoutParens(node) {
1156
+ return node.parametersNode.type === "identifier" && !node.comments?.some(({ leading, trailing }) => !leading && !trailing) && !node.parametersNode.comments;
1157
+ }
1158
+ function printMemberChain(path, print, options) {
1159
+ const isExpressionStatement = path.parent?.type === "expression_statement";
1160
+ const printedNodes = [];
1161
+ function shouldInsertEmptyLineAfter(node) {
1162
+ const { originalText } = options;
1163
+ const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex(originalText, node.end.index);
1164
+ if ((nextCharIndex ? originalText.charAt(nextCharIndex) : "") === ")") return nextCharIndex !== false && isNextLineEmpty(originalText, nextCharIndex + 1);
1165
+ return isNextLineEmpty(originalText, node.end.index);
1166
+ }
1167
+ function rec(path) {
1168
+ const { node } = path;
1169
+ if (hasType(path, "method_invocation") && hasChild(path, "objectNode")) {
1170
+ const hasTrailingEmptyLine = shouldInsertEmptyLineAfter(node);
1171
+ printedNodes.unshift({
1172
+ node,
1173
+ hasTrailingEmptyLine,
1174
+ printed: [printComments(path, printMethodInvocation(path, print)), hasTrailingEmptyLine ? hardline$3 : ""]
1175
+ });
1176
+ path.call(rec, "objectNode");
1177
+ } else if (hasType(path, "array_access")) {
1178
+ printedNodes.unshift({
1179
+ node,
1180
+ printed: printComments(path, printArrayAccess(path, print))
1181
+ });
1182
+ path.call(rec, "arrayNode");
1183
+ } else if (hasType(path, "field_access")) {
1184
+ printedNodes.unshift({
1185
+ node,
1186
+ printed: printComments(path, printFieldAccess(path, print))
1187
+ });
1188
+ path.call(rec, "objectNode");
1189
+ } else printedNodes.unshift({
1190
+ node,
1191
+ printed: print(path)
1192
+ });
1193
+ }
1194
+ const { node } = path;
1195
+ if (hasType(path, "method_invocation")) {
1196
+ printedNodes.unshift({
1197
+ node,
1198
+ printed: printComments(path, printMethodInvocation(path, print))
1199
+ });
1200
+ if (hasChild(path, "objectNode")) path.call(rec, "objectNode");
1201
+ } else if (hasType(path, "array_access")) {
1202
+ printedNodes.unshift({
1203
+ node,
1204
+ printed: printComments(path, printArrayAccess(path, print))
1205
+ });
1206
+ if (hasChild(path, "arrayNode")) path.call(rec, "arrayNode");
1207
+ } else if (hasType(path, "field_access")) {
1208
+ printedNodes.unshift({
1209
+ node,
1210
+ printed: printComments(path, printFieldAccess(path, print))
1211
+ });
1212
+ if (hasChild(path, "objectNode")) path.call(rec, "objectNode");
1213
+ }
1214
+ const danglingComments = printDanglingComments(path);
1215
+ if (danglingComments.length) printedNodes[0].printed = [
1216
+ ...danglingComments,
1217
+ hardline$3,
1218
+ printedNodes[0].printed
1219
+ ];
1220
+ const groups = [];
1221
+ let currentGroup = [printedNodes[0]];
1222
+ let i = 1;
1223
+ for (; i < printedNodes.length; ++i) if (printedNodes[i].node.type === "array_access") currentGroup.push(printedNodes[i]);
1224
+ else break;
1225
+ if (printedNodes[0].node.type !== "method_invocation") for (; i + 1 < printedNodes.length; ++i) if (printedNodes[i].node.type !== "method_invocation") currentGroup.push(printedNodes[i]);
1226
+ else break;
1227
+ groups.push(currentGroup);
1228
+ currentGroup = [];
1229
+ let hasSeenMethodInvocation = false;
1230
+ for (; i < printedNodes.length; ++i) {
1231
+ if (hasSeenMethodInvocation) {
1232
+ if (printedNodes[i].node.type === "array_access") {
1233
+ currentGroup.push(printedNodes[i]);
1234
+ continue;
1235
+ }
1236
+ groups.push(currentGroup);
1237
+ currentGroup = [];
1238
+ hasSeenMethodInvocation = false;
1239
+ }
1240
+ if (printedNodes[i].node.type === "method_invocation") hasSeenMethodInvocation = true;
1241
+ currentGroup.push(printedNodes[i]);
1242
+ if (printedNodes[i].node.comments?.some(({ trailing }) => trailing)) {
1243
+ groups.push(currentGroup);
1244
+ currentGroup = [];
1245
+ hasSeenMethodInvocation = false;
1246
+ }
1247
+ }
1248
+ if (currentGroup.length > 0) groups.push(currentGroup);
1249
+ function isFactory(name) {
1250
+ return /^[A-Z]|^[$_]+$/.test(name);
1251
+ }
1252
+ function isShort(name) {
1253
+ return name.length <= options.tabWidth;
1254
+ }
1255
+ function shouldNotWrap(groups) {
1256
+ const hasArrayAccess = groups[1][0]?.node.type === "array_access";
1257
+ if (groups[0].length === 1) {
1258
+ const firstNode = groups[0][0].node;
1259
+ return firstNode.type === "this" || firstNode.type === "identifier" && (isFactory(firstNode.value) || isExpressionStatement && isShort(firstNode.value) || hasArrayAccess);
1260
+ }
1261
+ const lastNode = groups[0].at(-1).node;
1262
+ return lastNode.type === "field_access" && lastNode.fieldNode.type === "identifier" && (isFactory(lastNode.fieldNode.value) || hasArrayAccess);
1263
+ }
1264
+ const shouldMerge = groups.length >= 2 && !groups[1][0].node.comments?.length && shouldNotWrap(groups);
1265
+ function printGroup(printedGroup) {
1266
+ return printedGroup.map((tuple) => tuple.printed);
1267
+ }
1268
+ function printIndentedGroup(groups) {
1269
+ if (groups.length === 0) return "";
1270
+ return indent$4([hardline$3, join$4(hardline$3, groups.map(printGroup))]);
1271
+ }
1272
+ const printedGroups = groups.map(printGroup);
1273
+ const oneLine = printedGroups;
1274
+ const cutoff = shouldMerge ? 3 : 2;
1275
+ const nodeHasComment = groups.flat().some((node) => node.node.comments?.some(({ leading, trailing }) => leading || trailing));
1276
+ if (groups.length <= cutoff && !nodeHasComment && !groups.some((g) => g.at(-1).hasTrailingEmptyLine)) return group$4(oneLine);
1277
+ const lastNodeBeforeIndent = groups[shouldMerge ? 1 : 0].at(-1).node;
1278
+ const shouldHaveEmptyLineBeforeIndent = lastNodeBeforeIndent.type !== "method_invocation" && shouldInsertEmptyLineAfter(lastNodeBeforeIndent);
1279
+ const expanded = [
1280
+ printGroup(groups[0]),
1281
+ shouldMerge ? groups.slice(1, 2).map(printGroup) : "",
1282
+ shouldHaveEmptyLineBeforeIndent ? hardline$3 : "",
1283
+ printIndentedGroup(groups.slice(shouldMerge ? 2 : 1))
1284
+ ];
1285
+ const methodInvocations = printedNodes.map(({ node }) => node).filter((node) => node.type === "method_invocation");
1286
+ function lastGroupWillBreakAndOtherCallsHaveFunctionArguments() {
1287
+ const lastGroupNode = groups.at(-1).at(-1).node;
1288
+ const lastGroupDoc = printedGroups.at(-1);
1289
+ return lastGroupNode.type === "method_invocation" && willBreak(lastGroupDoc) && methodInvocations.slice(0, -1).some((node) => node.argumentsNode.namedChildren.some(({ type }) => type === "lambda_expression"));
1290
+ }
1291
+ let result;
1292
+ if (nodeHasComment || methodInvocations.length > 2 && methodInvocations.some((inv) => !inv.argumentsNode.namedChildren.every((arg) => isSimpleCallArgument(arg))) || printedGroups.slice(0, -1).some(willBreak) || lastGroupWillBreakAndOtherCallsHaveFunctionArguments()) result = group$4(expanded);
1293
+ else result = [willBreak(oneLine) || shouldHaveEmptyLineBeforeIndent ? breakParent$1 : "", conditionalGroup([oneLine, expanded])];
1294
+ return result;
1295
+ }
1296
+ function printMethodInvocation(path, print) {
1297
+ const parts = [];
1298
+ if (hasChild(path, "objectNode")) parts.push(".");
1299
+ if (path.node.children.filter(({ type }) => type === ".").length === 2) parts.push("super", ".");
1300
+ if (hasChild(path, "type_argumentsNode")) parts.push(path.call(print, "type_argumentsNode"));
1301
+ parts.push(path.call(print, "nameNode"), path.call(print, "argumentsNode"));
1302
+ return parts;
1303
+ }
1304
+ function printArrayAccess(path, print) {
1305
+ const index = path.call(print, "indexNode");
1306
+ return path.node.indexNode.type === "decimal_integer_literal" ? [
1307
+ "[",
1308
+ index,
1309
+ "]"
1310
+ ] : group$4([
1311
+ "[",
1312
+ indent$4([softline$2, index]),
1313
+ softline$2,
1314
+ "]"
1315
+ ]);
1316
+ }
1317
+ function printFieldAccess(path, print) {
1318
+ const parts = ["."];
1319
+ if (path.node.children.filter(({ type }) => type === ".").length === 2) parts.push("super.");
1320
+ parts.push(path.call(print, "fieldNode"));
1321
+ return parts;
1322
+ }
1323
+ /**
1324
+ * For binary expressions to be consistent, we need to group
1325
+ * subsequent operators with the same precedence level under a single
1326
+ * group. Otherwise they will be nested such that some of them break
1327
+ * onto new lines but not all. Operators with the same precedence
1328
+ * level should either all break or not. Because we group them by
1329
+ * precedence level and the AST is structured based on precedence
1330
+ * level, things are naturally broken up correctly, i.e. `&&` is
1331
+ * broken before `+`.
1332
+ */
1333
+ function printBinaryExpressions(path, print, options, isInsideParentheses) {
1334
+ if (!hasType(path, "binary_expression")) return [group$4(print(path))];
1335
+ const { node } = path;
1336
+ let parts = [];
1337
+ if (node.leftNode.type === "binary_expression" && shouldFlatten(node.operatorNode.type, node.leftNode.operatorNode.type)) parts = path.call(() => printBinaryExpressions(path, print, options, isInsideParentheses), "leftNode");
1338
+ else parts.push(group$4(path.call(print, "leftNode")));
1339
+ const operator = node.operatorNode.type;
1340
+ const operatorDoc = path.call(print, "operatorNode");
1341
+ const rightContent = path.call(print, "rightNode");
1342
+ let right = options.experimentalOperatorPosition === "start" ? [
1343
+ line$4,
1344
+ operatorDoc,
1345
+ " ",
1346
+ rightContent
1347
+ ] : [
1348
+ " ",
1349
+ operatorDoc,
1350
+ line$4,
1351
+ rightContent
1352
+ ];
1353
+ const { parent } = path;
1354
+ const shouldBreak = node.leftNode.comments?.some(({ trailing, type }) => trailing && type === "line_comment") ?? false;
1355
+ if (shouldBreak || !(isInsideParentheses && logicalOperators.has(operator)) && (parent?.type !== node.type || logicalOperators.has(parent.operatorNode.value) !== logicalOperators.has(operator)) && node.leftNode.type !== node.type && node.rightNode.type !== node.type) right = group$4(right, { shouldBreak });
1356
+ parts.push(right);
1357
+ return parent?.type === "binary_expression" && needsParentheses(parent.operatorNode.type, operator) ? [
1358
+ "(",
1359
+ ...parts,
1360
+ ")"
1361
+ ] : parts;
1362
+ }
1363
+ const logicalOperators = new Set(["||", "&&"]);
1364
+ const equalityOperators = new Set(["==", "!="]);
1365
+ const multiplicativeOperators = new Set([
1366
+ "*",
1367
+ "/",
1368
+ "%"
1369
+ ]);
1370
+ const bitshiftOperators = new Set([
1371
+ ">>",
1372
+ ">>>",
1373
+ "<<"
1374
+ ]);
1375
+ function shouldFlatten(parentOp, nodeOp) {
1376
+ if (getPrecedence(nodeOp) !== getPrecedence(parentOp)) return false;
1377
+ if (equalityOperators.has(parentOp) && equalityOperators.has(nodeOp)) return false;
1378
+ if (nodeOp === "%" && multiplicativeOperators.has(parentOp) || parentOp === "%" && multiplicativeOperators.has(nodeOp)) return false;
1379
+ if (nodeOp !== parentOp && multiplicativeOperators.has(nodeOp) && multiplicativeOperators.has(parentOp)) return false;
1380
+ if (bitshiftOperators.has(parentOp) && bitshiftOperators.has(nodeOp)) return false;
1381
+ return true;
1382
+ }
1383
+ const PRECEDENCE = new Map([
1384
+ ["||"],
1385
+ ["&&"],
1386
+ ["|"],
1387
+ ["^"],
1388
+ ["&"],
1389
+ ["==", "!="],
1390
+ [
1391
+ "<",
1392
+ ">",
1393
+ "<=",
1394
+ ">=",
1395
+ "instanceof"
1396
+ ],
1397
+ [
1398
+ "<<",
1399
+ ">>",
1400
+ ">>>"
1401
+ ],
1402
+ ["+", "-"],
1403
+ [
1404
+ "*",
1405
+ "/",
1406
+ "%"
1407
+ ]
1408
+ ].flatMap((operators, index) => operators.map((operator) => [operator, index])));
1409
+ function getPrecedence(operator) {
1410
+ return PRECEDENCE.get(operator) ?? -1;
1411
+ }
1412
+ function needsParentheses(parentOperator, operator) {
1413
+ return operator === "&&" && parentOperator === "||" || [
1414
+ "|",
1415
+ "^",
1416
+ "&",
1417
+ "<<",
1418
+ ">>",
1419
+ ">>>"
1420
+ ].includes(parentOperator) && getPrecedence(operator) > getPrecedence(parentOperator) || [operator, parentOperator].every((o) => ["==", "!="].includes(o)) || [operator, parentOperator].every((o) => [
1421
+ "<<",
1422
+ ">>",
1423
+ ">>>"
1424
+ ].includes(o)) || operator === "*" && parentOperator === "/" || operator === "/" && parentOperator === "*" || operator === "%" && [
1425
+ "+",
1426
+ "-",
1427
+ "*",
1428
+ "/"
1429
+ ].includes(parentOperator) || ["*", "/"].includes(operator) && parentOperator === "%";
1430
+ }
1431
+ function isSimpleCallArgument(node, depth = 2) {
1432
+ if (depth <= 0) return false;
1433
+ const isChildSimple = (child) => isSimpleCallArgument(child, depth - 1);
1434
+ if (node.type.endsWith("_literal") || node.type === "true" || node.type === "false" || node.type === "identifier" || node.type === "this") return true;
1435
+ if (node.type === "object_creation_expression" || node.type === "method_invocation") {
1436
+ if (node.type === "object_creation_expression" || !node.objectNode || isSimpleCallArgument(node.objectNode, depth)) {
1437
+ const args = node.argumentsNode.namedChildren;
1438
+ return args.length <= depth && args.every(isChildSimple);
1439
+ }
1440
+ return false;
1441
+ }
1442
+ if (node.type === "array_access") return isSimpleCallArgument(node.arrayNode, depth) && isSimpleCallArgument(node.indexNode, depth);
1443
+ if (node.type === "field_access") return isSimpleCallArgument(node.objectNode, depth) && isSimpleCallArgument(node.fieldNode, depth);
1444
+ if (node.type === "method_reference" || node.type === "unary_expression" || node.type === "update_expression") return isSimpleCallArgument(node.namedChildren[0], depth);
1445
+ return false;
1446
+ }
1447
+ function couldExpandArg(arg, lambdaChainRecursion = false) {
1448
+ if (arg.type === "array_creation_expression" && arg.valueNode && (arg.valueNode.namedChildren.length > 0 || arg.valueNode.comments)) return true;
1449
+ if (arg.type === "lambda_expression") {
1450
+ const { bodyNode: body } = arg;
1451
+ if (body.type === "block" || body.type === "array_creation_expression") return true;
1452
+ if (body.type === "lambda_expression" && couldExpandArg(body, true)) return true;
1453
+ if (!lambdaChainRecursion) {
1454
+ if (body.type === "ternary_expression") return true;
1455
+ if (body.type === "method_invocation" || body.type === "object_creation_expression") return true;
1456
+ }
1457
+ }
1458
+ return false;
1459
+ }
1460
+ function shouldExpandLastArg(args) {
1461
+ const lastArg = args.at(-1);
1462
+ const penultimateArg = args.at(-2);
1463
+ return !lastArg.comments?.some(({ leading }) => leading) && !lastArg.comments?.some(({ trailing }) => trailing) && couldExpandArg(lastArg) && (!penultimateArg || penultimateArg.type !== lastArg.type) && (args.length !== 2 || penultimateArg.type !== "lambda_expression");
1464
+ }
1465
+ function shouldExpandFirstArg(args) {
1466
+ if (args.length !== 2) return false;
1467
+ const [firstArg, secondArg] = args;
1468
+ return !firstArg.comments && firstArg.type === "lambda_expression" && firstArg.bodyNode.type === "block" && secondArg.type !== "lambda_expression" && secondArg.type !== "ternary_expression" && isHopefullyShortCallArgument(secondArg) && !couldExpandArg(secondArg);
1469
+ }
1470
+ function isHopefullyShortCallArgument(node) {
1471
+ if (node.type === "parenthesized_expression") return isHopefullyShortCallArgument(node.namedChildren[0]);
1472
+ if ((node.type === "method_invocation" || node.type === "object_creation_expression") && node.argumentsNode.namedChildren.length > 1) return false;
1473
+ if (node.type === "binary_expression") return isSimpleCallArgument(node.leftNode, 1) && isSimpleCallArgument(node.rightNode, 1);
1474
+ return isSimpleCallArgument(node);
1475
+ }
1476
+ var ArgExpansionBailout = class extends Error {
1477
+ constructor(..._args) {
1478
+ super(..._args);
1479
+ this.name = "ArgExpansionBailout";
1480
+ }
1481
+ };
1482
+ //#endregion
1483
+ //#region src/printers/interfaces.ts
1484
+ const { group: group$3, indent: indent$3, join: join$3, line: line$3 } = prettier_doc.builders;
1485
+ var interfaces_default = {
1486
+ interface_declaration(path, print) {
1487
+ const parts = ["interface ", path.call(print, "nameNode")];
1488
+ const extendsInterfacesIndex = path.node.namedChildren.findIndex(({ type }) => type === "extends_interfaces");
1489
+ const hasExtendsInterfaces = extendsInterfacesIndex !== -1;
1490
+ const hasPermits = hasChild(path, "permitsNode");
1491
+ const hasMultipleClauses = hasExtendsInterfaces && hasPermits;
1492
+ const hasTypeParameters = hasChild(path, "type_parametersNode");
1493
+ if (hasTypeParameters) {
1494
+ const typeParameters = path.call(print, "type_parametersNode");
1495
+ parts.push(hasMultipleClauses ? group$3(indent$3(typeParameters)) : typeParameters);
1496
+ }
1497
+ if (hasExtendsInterfaces || hasPermits) {
1498
+ const separator = hasTypeParameters && !hasMultipleClauses ? " " : line$3;
1499
+ const clauses = [];
1500
+ if (hasExtendsInterfaces) clauses.push(separator, path.call(print, "namedChildren", extendsInterfacesIndex));
1501
+ if (hasPermits) clauses.push(separator, path.call(print, "permitsNode"));
1502
+ const hasBody = path.node.bodyNode.namedChildren.length > 0;
1503
+ const clauseGroup = [hasTypeParameters && !hasMultipleClauses ? clauses : indent$3(clauses), hasBody ? separator : " "];
1504
+ parts.push(hasMultipleClauses ? clauseGroup : group$3(clauseGroup));
1505
+ } else parts.push(" ");
1506
+ return [
1507
+ ...printModifiers(path, print, "declarationOnly"),
1508
+ group$3(parts),
1509
+ path.call(print, "bodyNode")
1510
+ ];
1511
+ },
1512
+ extends_interfaces(path, print) {
1513
+ const typeListIndex = path.node.namedChildren.findIndex(({ type }) => type === "type_list");
1514
+ return group$3(["extends", indent$3([line$3, path.call(print, "namedChildren", typeListIndex)])]);
1515
+ },
1516
+ interface_body(path, print) {
1517
+ return printBlock(path, printBodyDeclarations(path, print));
1518
+ },
1519
+ constant_declaration: printVariableDeclaration,
1520
+ annotation_type_declaration(path, print) {
1521
+ const parts = printModifiers(path, print);
1522
+ parts.push("@interface ", path.call(print, "nameNode"), " ", path.call(print, "bodyNode"));
1523
+ return parts;
1524
+ },
1525
+ annotation_type_body(path, print) {
1526
+ return printBlock(path, printBodyDeclarations(path, print));
1527
+ },
1528
+ annotation_type_element_declaration(path, print) {
1529
+ const parts = printModifiers(path, print);
1530
+ parts.push(path.call(print, "typeNode"), " ", path.call(print, "nameNode"), "()");
1531
+ if (hasChild(path, "dimensionsNode")) parts.push(path.call(print, "dimensionsNode"));
1532
+ if (hasChild(path, "valueNode")) parts.push(" default ", path.call(print, "valueNode"));
1533
+ parts.push(";");
1534
+ return parts;
1535
+ },
1536
+ annotation(path, print) {
1537
+ return [
1538
+ "@",
1539
+ path.call(print, "nameNode"),
1540
+ path.call(print, "argumentsNode")
1541
+ ];
1542
+ },
1543
+ marker_annotation(path, print) {
1544
+ return ["@", path.call(print, "nameNode")];
1545
+ },
1546
+ annotation_argument_list(path, print) {
1547
+ const args = path.map(print, "namedChildren");
1548
+ return args.length === 1 && path.node.namedChildren[0].type === "element_value_array_initializer" ? [
1549
+ "(",
1550
+ args[0],
1551
+ ")"
1552
+ ] : group$3(indentInParentheses(join$3([",", line$3], args)));
1553
+ },
1554
+ element_value_pair(path, print) {
1555
+ return group$3([
1556
+ path.call(print, "keyNode"),
1557
+ " = ",
1558
+ path.call(print, "valueNode")
1559
+ ]);
1560
+ },
1561
+ element_value_array_initializer: printArrayInitializer
1562
+ };
1563
+ //#endregion
1564
+ //#region src/printers/lexical-structure.ts
1565
+ const { group: group$2, hardline: hardline$2, indent: indent$2, join: join$2, softline: softline$1 } = prettier_doc.builders;
1566
+ var lexical_structure_default = {
1567
+ string_literal(path, print) {
1568
+ if (path.node.namedChildren.some(({ type }) => type === "string_interpolation") || path.node.children[0].value === "\"") return path.map(print, "children");
1569
+ return printTextBlock(path, join$2(hardline$2, textBlockContents(path.node).split("\n")));
1570
+ },
1571
+ string_fragment: printValue,
1572
+ multiline_string_fragment: printValue,
1573
+ string_interpolation(path, print) {
1574
+ const expressionType = path.node.namedChildren[0].type;
1575
+ const expression = path.call(print, "namedChildren", 0);
1576
+ return expressionType === "binary_expression" || expressionType === "ternary_expression" ? group$2([
1577
+ "\\{",
1578
+ indent$2([softline$1, expression]),
1579
+ softline$1,
1580
+ "}"
1581
+ ]) : [
1582
+ "\\{",
1583
+ expression,
1584
+ "}"
1585
+ ];
1586
+ },
1587
+ escape_sequence: printValue,
1588
+ character_literal: printValue,
1589
+ binary_integer_literal: printValue,
1590
+ decimal_integer_literal: printValue,
1591
+ hex_integer_literal: printValue,
1592
+ octal_integer_literal: printValue,
1593
+ decimal_floating_point_literal: printValue,
1594
+ hex_floating_point_literal: printValue,
1595
+ null_literal: printValue,
1596
+ true: printValue,
1597
+ false: printValue,
1598
+ this: printValue,
1599
+ super: printValue,
1600
+ underscore_pattern: printValue,
1601
+ asterisk: printValue
1602
+ };
1603
+ //#endregion
1604
+ //#region src/printers/names.ts
1605
+ var names_default = {
1606
+ identifier: printValue,
1607
+ type_identifier: printValue,
1608
+ scoped_identifier(path, print) {
1609
+ return [
1610
+ path.call(print, "scopeNode"),
1611
+ ".",
1612
+ path.call(print, "nameNode")
1613
+ ];
1614
+ },
1615
+ scoped_type_identifier(path, print) {
1616
+ return path.map((child) => child.node.type === "annotation" || child.node.type === "marker_annotation" ? [print(child), " "] : print(child), "children");
1617
+ }
1618
+ };
1619
+ //#endregion
1620
+ //#region src/printers/packages-and-modules.ts
1621
+ const { group: group$1, hardline: hardline$1, indent: indent$1, join: join$1, line: line$2 } = prettier_doc.builders;
1622
+ var packages_and_modules_default = {
1623
+ program(path, print) {
1624
+ if (!path.node.namedChildren.length) return [...printDanglingComments(path), hardline$1];
1625
+ const parts = [];
1626
+ if (path.node.namedChildren[0].type === "package_declaration") parts.push(path.call(print, "namedChildren", 0));
1627
+ const staticImports = [];
1628
+ const imports = [];
1629
+ const otherDeclarations = [];
1630
+ path.each((child) => {
1631
+ const doc = print(child);
1632
+ if (child.node.type === "import_declaration") {
1633
+ const names = extractNames(child.node.namedChildren[0]);
1634
+ if (child.node.namedChildren.some(({ type }) => type === "asterisk")) names.push("*");
1635
+ (child.node.children[1].type === "static" ? staticImports : imports).push({
1636
+ doc,
1637
+ names
1638
+ });
1639
+ } else if (child.node.type !== "package_declaration") otherDeclarations.push(doc);
1640
+ }, "namedChildren");
1641
+ parts.push(...[staticImports, imports].filter((imports) => imports.length).map((imports) => join$1(hardline$1, imports.sort(compareFqn).map(({ doc }) => doc))), ...otherDeclarations);
1642
+ return [...join$1([hardline$1, hardline$1], parts), hardline$1];
1643
+ },
1644
+ package_declaration(path, print) {
1645
+ const annotations = [];
1646
+ const identifier = [];
1647
+ path.each((child) => {
1648
+ switch (child.node.type) {
1649
+ case "annotation":
1650
+ case "marker_annotation":
1651
+ annotations.push(print(child));
1652
+ break;
1653
+ case "identifier":
1654
+ case "scoped_identifier":
1655
+ identifier.push(print(child));
1656
+ break;
1657
+ }
1658
+ }, "namedChildren");
1659
+ return join$1(hardline$1, [...annotations, [
1660
+ "package ",
1661
+ ...identifier,
1662
+ ";"
1663
+ ]]);
1664
+ },
1665
+ import_declaration(path, print) {
1666
+ const declaration = ["import "];
1667
+ if (path.node.children.some(({ type }) => type === "static")) declaration.push("static ");
1668
+ const identifierIndex = path.node.namedChildren.findIndex(({ type }) => type === "identifier" || type === "scoped_identifier");
1669
+ declaration.push(path.call(print, "namedChildren", identifierIndex));
1670
+ if (path.node.namedChildren.some(({ type }) => type === "asterisk")) declaration.push(".*");
1671
+ declaration.push(";");
1672
+ return declaration;
1673
+ },
1674
+ module_declaration(path, print) {
1675
+ const parts = [];
1676
+ path.each((child) => {
1677
+ if (child.node.type === "annotation" || child.node.type === "marker_annotation") parts.push(print(child));
1678
+ }, "namedChildren");
1679
+ if (path.node.children.some(({ type }) => type === "open")) parts.push("open");
1680
+ parts.push("module", path.call(print, "nameNode"), path.call(print, "bodyNode"));
1681
+ return join$1(" ", parts);
1682
+ },
1683
+ module_body(path, print) {
1684
+ return printBlock(path, path.map((child) => child.previous && child.node.start.row > child.previous.end.row + 1 ? [hardline$1, print(child)] : print(child), "namedChildren"));
1685
+ },
1686
+ requires_module_directive(path, print) {
1687
+ const parts = ["requires"];
1688
+ path.each((child) => {
1689
+ if (child.node.type === "requires_modifier") parts.push(print(child));
1690
+ }, "namedChildren");
1691
+ parts.push(path.call(print, "moduleNode"));
1692
+ return [...join$1(" ", parts), ";"];
1693
+ },
1694
+ exports_module_directive: printToModuleNamesDirective,
1695
+ opens_module_directive: printToModuleNamesDirective,
1696
+ uses_module_directive(path, print) {
1697
+ return [
1698
+ "uses ",
1699
+ path.call(print, "typeNode"),
1700
+ ";"
1701
+ ];
1702
+ },
1703
+ provides_module_directive(path, print) {
1704
+ const [provided, ...providers] = path.map(print, "namedChildren");
1705
+ return [
1706
+ "provides ",
1707
+ provided,
1708
+ group$1(indent$1([line$2, group$1(indent$1([
1709
+ "with",
1710
+ line$2,
1711
+ ...join$1([",", line$2], providers)
1712
+ ]))])),
1713
+ ";"
1714
+ ];
1715
+ },
1716
+ requires_modifier: printValue
1717
+ };
1718
+ function extractNames(node) {
1719
+ return node.type === "identifier" ? [node.value] : [...extractNames(node.scopeNode), node.nameNode.value];
1720
+ }
1721
+ function compareFqn({ names: a }, { names: b }) {
1722
+ const minParts = Math.min(a.length, b.length);
1723
+ for (let i = 0; i < minParts; i++) {
1724
+ const imageA = a[i];
1725
+ const imageB = b[i];
1726
+ if (imageA < imageB) return -1;
1727
+ else if (imageA > imageB) return 1;
1728
+ }
1729
+ return a.length - b.length;
1730
+ }
1731
+ function printToModuleNamesDirective(path, print) {
1732
+ const directive = [
1733
+ path.node.type === "exports_module_directive" ? "exports" : "opens",
1734
+ " ",
1735
+ path.call(print, "packageNode")
1736
+ ];
1737
+ if (path.node.modulesNodes.length) {
1738
+ const moduleNames = join$1([",", line$2], path.map(print, "modulesNodes"));
1739
+ directive.push(group$1(indent$1([line$2, group$1(indent$1([
1740
+ "to",
1741
+ line$2,
1742
+ ...moduleNames
1743
+ ]))])));
1744
+ }
1745
+ directive.push(";");
1746
+ return directive;
1747
+ }
1748
+ //#endregion
1749
+ //#region src/printers/types-values-and-variables.ts
1750
+ const { group, indent, join, line: line$1, softline } = prettier_doc.builders;
1751
+ var types_values_and_variables_default = {
1752
+ boolean_type: printValue,
1753
+ integral_type: printValue,
1754
+ floating_point_type: printValue,
1755
+ void_type: printValue,
1756
+ array_type(path, print) {
1757
+ return [path.call(print, "elementNode"), path.call(print, "dimensionsNode")];
1758
+ },
1759
+ annotated_type(path, print) {
1760
+ return join(" ", path.map(print, "children"));
1761
+ },
1762
+ dimensions(path, print) {
1763
+ return path.map((child) => child.node.isNamed ? [print(child), " "] : child.node.value, "children");
1764
+ },
1765
+ type_parameter(path, print) {
1766
+ return join(" ", path.map(print, "children"));
1767
+ },
1768
+ type_bound(path, print) {
1769
+ const [firstType, ...restTypes] = path.map(print, "namedChildren");
1770
+ const bound = ["extends ", firstType];
1771
+ if (restTypes.length) bound.push(group(indent([
1772
+ line$1,
1773
+ "& ",
1774
+ ...join([line$1, "& "], restTypes)
1775
+ ])));
1776
+ return bound;
1777
+ },
1778
+ type_arguments(path, print) {
1779
+ const types = path.map(print, "namedChildren");
1780
+ return types.length ? group([
1781
+ "<",
1782
+ indent([softline, join([",", line$1], types)]),
1783
+ softline,
1784
+ ">"
1785
+ ]) : "<>";
1786
+ },
1787
+ wildcard(path, print) {
1788
+ return join(" ", path.map(print, "children"));
1789
+ }
1790
+ };
1791
+ //#endregion
1792
+ //#region src/printers/index.ts
1793
+ const printersByNodeType = {
1794
+ ERROR(path) {
1795
+ throw new Error(`Failed to parse: "${printValue(path)}"`);
1796
+ },
1797
+ ...arrays_default,
1798
+ ...blocks_and_statements_default,
1799
+ ...classes_default,
1800
+ ...expressions_default,
1801
+ ...interfaces_default,
1802
+ ...lexical_structure_default,
1803
+ ...names_default,
1804
+ ...packages_and_modules_default,
1805
+ ...types_values_and_variables_default
1806
+ };
1807
+ function printerForNodeType(type) {
1808
+ return printersByNodeType[type];
1809
+ }
1810
+ //#endregion
1811
+ //#region src/printer.ts
1812
+ var printer_default = {
1813
+ print(path, options, print, args) {
1814
+ return hasJavaNode(path) ? printerForNodeType(path.node.type)(path, print, options, args) : printValue(path);
1815
+ },
1816
+ embed(path) {
1817
+ return hasType(path, "string_literal") ? embedTextBlock(path) : null;
1818
+ },
1819
+ hasPrettierIgnore(path) {
1820
+ return path.node.comments?.some(isPrettierIgnore) === true || canAttachComment(path.node) && isFullyBetweenPrettierIgnore(path);
1821
+ },
1822
+ canAttachComment,
1823
+ isBlockComment(node) {
1824
+ return node.type === "block_comment";
1825
+ },
1826
+ willPrintOwnComments,
1827
+ printComment(commentPath) {
1828
+ return printComment(commentPath.node);
1829
+ },
1830
+ getCommentChildNodes(node) {
1831
+ return node.isNamed ? node.children : [];
1832
+ },
1833
+ handleComments: {
1834
+ ownLine: handleLineComment,
1835
+ endOfLine: handleLineComment,
1836
+ remaining: handleRemainingComment
1837
+ },
1838
+ getVisitorKeys() {
1839
+ return ["namedChildren"];
1840
+ }
1841
+ };
1842
+ function hasJavaNode(path) {
1843
+ return path.node.isNamed;
1844
+ }
1845
+ //#endregion
1846
+ //#region src/comments.ts
1847
+ const { hasNewline, isPreviousLineEmpty, skipNewline, skipSpaces } = prettier.util;
1848
+ const { breakParent, hardline, line, lineSuffix } = prettier_doc.builders;
1849
+ const prettierIgnoreRangesByTree = /* @__PURE__ */ new WeakMap();
1850
+ function determinePrettierIgnoreRanges(tree) {
1851
+ const { comments } = tree;
1852
+ if (!comments) return;
1853
+ const ranges = comments.filter(({ value }) => /^\/(?:\/\s*(?:prettier-ignore-(?:start|end)|@formatter:(?:off|on))\s*|\*\s*(?:prettier-ignore-(?:start|end)|@formatter:(?:off|on))\s*\*\/)$/.test(value)).reduce((ranges, { value, start }) => {
1854
+ const previous = ranges.at(-1);
1855
+ if (value.includes("start") || value.includes("off")) {
1856
+ if (previous?.end !== Infinity) ranges.push({
1857
+ start: start.index,
1858
+ end: Infinity
1859
+ });
1860
+ } else if (previous?.end === Infinity) previous.end = start.index;
1861
+ return ranges;
1862
+ }, new Array());
1863
+ prettierIgnoreRangesByTree.set(tree, ranges);
1864
+ }
1865
+ function isFullyBetweenPrettierIgnore(path) {
1866
+ const { node, root } = path;
1867
+ const start = parser_default.locStart(node);
1868
+ const end = parser_default.locEnd(node);
1869
+ return prettierIgnoreRangesByTree.get(root)?.some((range) => range.start < start && end < range.end) === true;
1870
+ }
1871
+ function isPrettierIgnore(comment) {
1872
+ return /^(\/\/\s*prettier-ignore|\/\*\s*prettier-ignore\s*\*\/)$/.test(comment.value);
1873
+ }
1874
+ function willPrintOwnComments(path) {
1875
+ return isMember(path.node) && !printer_default.hasPrettierIgnore(path);
1876
+ }
1877
+ function canAttachComment(node) {
1878
+ if (!node.isNamed) return isBinaryOperator(node);
1879
+ switch (node.type) {
1880
+ case "enum_body_declarations":
1881
+ case "escape_sequence":
1882
+ case "formal_parameters":
1883
+ case "modifier":
1884
+ case "multiline_string_fragment":
1885
+ case "parenthesized_expression":
1886
+ case "program":
1887
+ case "string_fragment":
1888
+ case "visibility": return false;
1889
+ default: return true;
1890
+ }
1891
+ }
1892
+ function handleLineComment(commentNode, _, options) {
1893
+ return [
1894
+ handleBinaryExpressionComments,
1895
+ handleFqnOrRefTypeComments,
1896
+ handleIfStatementComments,
1897
+ handleJumpStatementComments,
1898
+ handleLabeledStatementComments,
1899
+ handleMemberChainComments,
1900
+ handleModifiersComments,
1901
+ handleNameComments,
1902
+ handleTernaryExpressionComments,
1903
+ handleTryStatementComments
1904
+ ].some((fn) => fn(commentNode, options));
1905
+ }
1906
+ function handleRemainingComment(commentNode) {
1907
+ return [
1908
+ handleFqnOrRefTypeComments,
1909
+ handleNameComments,
1910
+ handleJumpStatementComments
1911
+ ].some((fn) => fn(commentNode));
1912
+ }
1913
+ function handleBinaryExpressionComments(commentNode, options) {
1914
+ const { enclosingNode, precedingNode, followingNode } = commentNode;
1915
+ if (enclosingNode?.type === "binary_expression") {
1916
+ if (isBinaryOperator(followingNode)) {
1917
+ if (options.experimentalOperatorPosition === "start") prettier.util.addLeadingComment(followingNode, commentNode);
1918
+ else prettier.util.addTrailingComment(followingNode, commentNode);
1919
+ return true;
1920
+ } else if (options.experimentalOperatorPosition === "start" && isBinaryOperator(precedingNode)) {
1921
+ prettier.util.addLeadingComment(precedingNode, commentNode);
1922
+ return true;
1923
+ }
1924
+ }
1925
+ return false;
1926
+ }
1927
+ function handleFqnOrRefTypeComments(commentNode) {
1928
+ const { enclosingNode, followingNode } = commentNode;
1929
+ if (enclosingNode?.type === "scoped_type_identifier" && followingNode) {
1930
+ prettier.util.addLeadingComment(followingNode, commentNode);
1931
+ return true;
1932
+ }
1933
+ return false;
1934
+ }
1935
+ function handleIfStatementComments(commentNode) {
1936
+ const { enclosingNode, precedingNode } = commentNode;
1937
+ if (enclosingNode?.type === "if_statement" && precedingNode?.fieldName === "consequence") {
1938
+ prettier.util.addDanglingComment(enclosingNode, commentNode, void 0);
1939
+ return true;
1940
+ }
1941
+ return false;
1942
+ }
1943
+ function handleJumpStatementComments(commentNode) {
1944
+ const { enclosingNode, precedingNode, followingNode } = commentNode;
1945
+ if (enclosingNode && !precedingNode && !followingNode && (enclosingNode.type === "break_statement" || enclosingNode.type === "continue_statement" || enclosingNode.type === "return_statement")) {
1946
+ prettier.util.addTrailingComment(enclosingNode, commentNode);
1947
+ return true;
1948
+ }
1949
+ return false;
1950
+ }
1951
+ function handleLabeledStatementComments(commentNode) {
1952
+ const { enclosingNode, precedingNode } = commentNode;
1953
+ if (enclosingNode?.type === "labeled_statement" && precedingNode?.type === "identifier") {
1954
+ prettier.util.addLeadingComment(precedingNode, commentNode);
1955
+ return true;
1956
+ }
1957
+ return false;
1958
+ }
1959
+ function handleMemberChainComments(commentNode) {
1960
+ const { enclosingNode, precedingNode, followingNode } = commentNode;
1961
+ if ((enclosingNode?.type === "field_access" || enclosingNode?.type === "method_invocation") && followingNode?.type === "identifier") {
1962
+ prettier.util.addLeadingComment(enclosingNode, commentNode);
1963
+ return true;
1964
+ } else if (followingNode && isMember(followingNode) && precedingNode !== enclosingNode && !isPrettierIgnore(commentNode)) {
1965
+ prettier.util.addDanglingComment(followingNode, commentNode, void 0);
1966
+ return true;
1967
+ }
1968
+ return false;
1969
+ }
1970
+ function handleModifiersComments(commentNode) {
1971
+ const { precedingNode } = commentNode;
1972
+ if (precedingNode?.type === "annotation" || precedingNode?.type === "marker_annotation" || precedingNode?.type === "modifiers") {
1973
+ prettier.util.addTrailingComment(precedingNode, commentNode);
1974
+ return true;
1975
+ }
1976
+ return false;
1977
+ }
1978
+ function handleNameComments(commentNode) {
1979
+ const { enclosingNode, precedingNode } = commentNode;
1980
+ if (enclosingNode && precedingNode?.type === "identifier" && (enclosingNode.type === "scoped_identifier" || enclosingNode.type === "module_declaration" || enclosingNode.type === "package_declaration" || enclosingNode.type === "scoped_type_identifier")) {
1981
+ prettier.util.addTrailingComment(precedingNode, commentNode);
1982
+ return true;
1983
+ }
1984
+ return false;
1985
+ }
1986
+ function handleTernaryExpressionComments(commentNode) {
1987
+ const { enclosingNode, precedingNode, followingNode } = commentNode;
1988
+ if (enclosingNode?.type === "ternary_expression" && precedingNode?.isNamed && followingNode?.isNamed && precedingNode.end.row < commentNode.start.row && commentNode.end.row < followingNode.start.row) {
1989
+ prettier.util.addLeadingComment(followingNode, commentNode);
1990
+ return true;
1991
+ }
1992
+ return false;
1993
+ }
1994
+ function handleTryStatementComments(commentNode) {
1995
+ const { enclosingNode, followingNode } = commentNode;
1996
+ if (enclosingNode && (enclosingNode.type === "catch_clause" || enclosingNode.type === "try_statement" || enclosingNode.type === "try_with_resources_statement") && followingNode?.isNamed) {
1997
+ const block = followingNode.type === "catch_clause" ? followingNode.bodyNode : followingNode.type === "finally_clause" ? followingNode.namedChildren[0] : null;
1998
+ if (!block) return false;
1999
+ const blockStatement = block.namedChildren.at(0);
2000
+ if (blockStatement) prettier.util.addLeadingComment(blockStatement, commentNode);
2001
+ else prettier.util.addDanglingComment(block, commentNode, void 0);
2002
+ return true;
2003
+ }
2004
+ return false;
2005
+ }
2006
+ function isMember(node) {
2007
+ return node.type === "array_access" || node.type === "field_access" || node.type === "method_invocation";
2008
+ }
2009
+ const binaryOperators = new Set([
2010
+ "<<",
2011
+ ">>",
2012
+ ">>>",
2013
+ "instanceof",
2014
+ "<=",
2015
+ ">=",
2016
+ "==",
2017
+ "-",
2018
+ "+",
2019
+ "&&",
2020
+ "&",
2021
+ "^",
2022
+ "!=",
2023
+ "||",
2024
+ "|",
2025
+ "*",
2026
+ "/",
2027
+ "%"
2028
+ ]);
2029
+ function isBinaryOperator(node) {
2030
+ return node !== void 0 && binaryOperators.has(node.type);
2031
+ }
2032
+ function printLeadingComment(path) {
2033
+ const comment = path.node;
2034
+ comment.printed = true;
2035
+ const parts = [printComment(comment)];
2036
+ const originalText = path.root.value;
2037
+ if (comment.type === "block_comment") {
2038
+ let lineBreak = " ";
2039
+ if (hasNewline(originalText, parser_default.locEnd(comment))) if (hasNewline(originalText, parser_default.locStart(comment), { backwards: true })) lineBreak = hardline;
2040
+ else lineBreak = line;
2041
+ parts.push(lineBreak);
2042
+ } else parts.push(hardline);
2043
+ const index = skipNewline(originalText, skipSpaces(originalText, parser_default.locEnd(comment)));
2044
+ if (index !== false && hasNewline(originalText, index)) parts.push(hardline);
2045
+ return parts;
2046
+ }
2047
+ function printTrailingComment(path, previousComment) {
2048
+ const comment = path.node;
2049
+ comment.printed = true;
2050
+ const printed = printComment(comment);
2051
+ const originalText = path.root.value;
2052
+ const isBlock = comment.type === "block_comment";
2053
+ if (previousComment?.hasLineSuffix && !previousComment?.isBlock || hasNewline(originalText, parser_default.locStart(comment), { backwards: true })) return {
2054
+ doc: lineSuffix([
2055
+ hardline,
2056
+ isPreviousLineEmpty(originalText, parser_default.locStart(comment)) ? hardline : "",
2057
+ printed
2058
+ ]),
2059
+ isBlock,
2060
+ hasLineSuffix: true
2061
+ };
2062
+ if (!isBlock || previousComment?.hasLineSuffix) return {
2063
+ doc: [lineSuffix([" ", printed]), breakParent],
2064
+ isBlock,
2065
+ hasLineSuffix: true
2066
+ };
2067
+ return {
2068
+ doc: [" ", printed],
2069
+ isBlock,
2070
+ hasLineSuffix: false
2071
+ };
2072
+ }
2073
+ function printLeadingComments(path) {
2074
+ if (!hasChild(path, "comments")) return [];
2075
+ const docs = [];
2076
+ path.each((path) => {
2077
+ const { node: comment } = path;
2078
+ if (!comment.leading) return;
2079
+ docs.push(printLeadingComment(path));
2080
+ }, "comments");
2081
+ return docs;
2082
+ }
2083
+ function printTrailingComments(path) {
2084
+ if (!hasChild(path, "comments")) return [];
2085
+ const docs = [];
2086
+ let printedTrailingComment;
2087
+ path.each((path) => {
2088
+ const { node: comment } = path;
2089
+ if (!comment.trailing) return;
2090
+ printedTrailingComment = printTrailingComment(path, printedTrailingComment);
2091
+ docs.push(printedTrailingComment.doc);
2092
+ }, "comments");
2093
+ return docs;
2094
+ }
2095
+ function printCommentsSeparately(path) {
2096
+ return {
2097
+ leading: printLeadingComments(path),
2098
+ trailing: printTrailingComments(path)
2099
+ };
2100
+ }
2101
+ function printComments(path, doc) {
2102
+ const leading = printLeadingComments(path);
2103
+ const trailing = printTrailingComments(path);
2104
+ return leading.length || trailing.length ? [
2105
+ leading,
2106
+ doc,
2107
+ trailing
2108
+ ] : doc;
2109
+ }
2110
+ //#endregion
2111
+ //#region src/parser.ts
2112
+ var parser_default = {
2113
+ async parse(text) {
2114
+ const tree = (await parser).parse(text);
2115
+ const { rootNode } = tree;
2116
+ if (rootNode.hasError) throw new Error("Failed to parse: " + rootNode);
2117
+ const ast = processTree(rootNode);
2118
+ determinePrettierIgnoreRanges(ast);
2119
+ tree.delete();
2120
+ return ast;
2121
+ },
2122
+ astFormat: "java",
2123
+ hasPragma(text) {
2124
+ return /^\/\*\*\n\s+\*\s@(format|prettier)\n\s+\*\//.test(text);
2125
+ },
2126
+ locStart(node) {
2127
+ return node.start.index;
2128
+ },
2129
+ locEnd(node) {
2130
+ return node.end.index;
2131
+ }
2132
+ };
2133
+ const parser = (async () => {
2134
+ await web_tree_sitter.Parser.init();
2135
+ const parser = new web_tree_sitter.Parser();
2136
+ const Java = await web_tree_sitter.Language.load(new URL("./tree-sitter-java_orchard.wasm", require("url").pathToFileURL(__filename).href).pathname);
2137
+ parser.setLanguage(Java);
2138
+ return parser;
2139
+ })();
2140
+ function processTree(node, fieldName = null, comments) {
2141
+ const { type, isNamed, text: value, startPosition, endPosition } = node;
2142
+ const javaNode = {
2143
+ type,
2144
+ isNamed,
2145
+ value,
2146
+ start: {
2147
+ index: node.startIndex,
2148
+ row: startPosition.row,
2149
+ column: startPosition.column
2150
+ },
2151
+ end: {
2152
+ index: node.endIndex,
2153
+ row: endPosition.row,
2154
+ column: endPosition.column
2155
+ },
2156
+ children: [],
2157
+ namedChildren: [],
2158
+ fieldName
2159
+ };
2160
+ if (!comments) comments = javaNode.comments = [];
2161
+ if (!javaNode.isNamed) return javaNode;
2162
+ const multiFields = multiFieldsByType[node.type];
2163
+ if (multiFields) Object.keys(multiFields).forEach((name) => javaNode[`${name}Nodes`] = []);
2164
+ node.children.forEach((child, index) => {
2165
+ const { type, text: value, startPosition, endPosition } = child;
2166
+ if (type === "block_comment" || type === "line_comment") comments.push({
2167
+ type,
2168
+ value,
2169
+ start: {
2170
+ index: child.startIndex,
2171
+ row: startPosition.row,
2172
+ column: startPosition.column
2173
+ },
2174
+ end: {
2175
+ index: child.endIndex,
2176
+ row: endPosition.row,
2177
+ column: endPosition.column
2178
+ },
2179
+ leading: false,
2180
+ trailing: false,
2181
+ printed: false
2182
+ });
2183
+ else {
2184
+ const fieldName = node.fieldNameForChild(index);
2185
+ const javaChild = processTree(child, fieldName, comments);
2186
+ javaNode.children.push(javaChild);
2187
+ if (javaChild.isNamed) javaNode.namedChildren.push(javaChild);
2188
+ if (fieldName) if (multiFields?.[fieldName]) javaNode[`${fieldName}Nodes`].push(javaChild);
2189
+ else javaNode[`${fieldName}Node`] = javaChild;
2190
+ }
2191
+ });
2192
+ return javaNode;
2193
+ }
2194
+ //#endregion
2195
+ //#region src/index.ts
2196
+ var src_default = {
2197
+ languages: [{
2198
+ name: "Java",
2199
+ parsers: ["java"],
2200
+ group: "Java",
2201
+ tmScope: "source.java",
2202
+ aceMode: "java",
2203
+ codemirrorMode: "clike",
2204
+ codemirrorMimeType: "text/x-java",
2205
+ extensions: [".java"],
2206
+ linguistLanguageId: 181,
2207
+ vscodeLanguageIds: ["java"]
2208
+ }],
2209
+ parsers: { java: parser_default },
2210
+ printers: { java: printer_default },
2211
+ options: options_default,
2212
+ defaultOptions: { arrowParens: "avoid" }
2213
+ };
2214
+ //#endregion
2215
+ module.exports = src_default;