prettier-plugin-java 1.3.0 → 1.6.0
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.
- package/dist/base-cst-printer.js +77 -0
- package/dist/cst-printer.js +37 -0
- package/dist/index.js +67 -0
- package/dist/options.js +256 -0
- package/dist/parser.js +7 -0
- package/dist/printer.js +28 -0
- package/dist/printers/arrays.js +49 -0
- package/dist/printers/blocks-and-statements.js +493 -0
- package/dist/printers/classes.js +724 -0
- package/dist/printers/comments/comments-utils.js +29 -0
- package/dist/printers/comments/format-comments.js +179 -0
- package/dist/printers/comments/handle-comments.js +38 -0
- package/dist/printers/expressions.js +549 -0
- package/dist/printers/interfaces.js +251 -0
- package/dist/printers/lexical-structure.js +43 -0
- package/dist/printers/names.js +53 -0
- package/dist/printers/packages-and-modules.js +185 -0
- package/dist/printers/prettier-builder.js +44 -0
- package/dist/printers/printer-utils.js +565 -0
- package/dist/printers/types-values-and-variables.js +183 -0
- package/dist/types/utils.js +29 -0
- package/dist/utils/expressions-utils.js +29 -0
- package/dist/utils/index.js +10 -0
- package/dist/utils/printArgumentListWithBraces.js +21 -0
- package/dist/utils/printSingleLambdaInvocation.js +20 -0
- package/package.json +32 -10
- package/src/cst-printer.js +0 -145
- package/src/index.js +0 -79
- package/src/options.js +0 -256
- package/src/parser.js +0 -10
- package/src/printer.js +0 -31
- package/src/printers/arrays.js +0 -38
- package/src/printers/blocks-and-statements.js +0 -588
- package/src/printers/classes.js +0 -940
- package/src/printers/comments/comments-utils.js +0 -38
- package/src/printers/comments/format-comments.js +0 -223
- package/src/printers/comments/handle-comments.js +0 -50
- package/src/printers/expressions.js +0 -703
- package/src/printers/interfaces.js +0 -324
- package/src/printers/lexical-structure.js +0 -27
- package/src/printers/names.js +0 -42
- package/src/printers/packages-and-modules.js +0 -231
- package/src/printers/prettier-builder.js +0 -60
- package/src/printers/printer-utils.js +0 -715
- package/src/printers/types-values-and-variables.js +0 -202
- package/yarn-error.log +0 -8052
|
@@ -1,715 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const forEach = require("lodash/forEach");
|
|
3
|
-
const forEachRight = require("lodash/forEachRight");
|
|
4
|
-
const findLastIndex = require("lodash/findLastIndex");
|
|
5
|
-
|
|
6
|
-
const { ifBreak, join, concat, group } = require("./prettier-builder");
|
|
7
|
-
const {
|
|
8
|
-
getTokenLeadingComments,
|
|
9
|
-
printTokenWithComments
|
|
10
|
-
} = require("./comments/format-comments");
|
|
11
|
-
const { hasComments } = require("./comments/comments-utils");
|
|
12
|
-
const { indent, hardline, line } = require("prettier").doc.builders;
|
|
13
|
-
|
|
14
|
-
const orderedModifiers = [
|
|
15
|
-
"Public",
|
|
16
|
-
"Protected",
|
|
17
|
-
"Private",
|
|
18
|
-
"Abstract",
|
|
19
|
-
"Default",
|
|
20
|
-
"Static",
|
|
21
|
-
"Final",
|
|
22
|
-
"Transient",
|
|
23
|
-
"Volatile",
|
|
24
|
-
"Synchronized",
|
|
25
|
-
"Native",
|
|
26
|
-
"Sealed",
|
|
27
|
-
"NonSealed",
|
|
28
|
-
"Strictfp"
|
|
29
|
-
];
|
|
30
|
-
|
|
31
|
-
function buildFqn(tokens, dots) {
|
|
32
|
-
return rejectAndJoinSeps(dots ? dots : [], tokens);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function rejectAndJoinSeps(sepTokens, elems, sep) {
|
|
36
|
-
if (!Array.isArray(sepTokens)) {
|
|
37
|
-
return rejectAndJoin(sepTokens, elems);
|
|
38
|
-
}
|
|
39
|
-
const actualElements = reject(elems);
|
|
40
|
-
const res = [];
|
|
41
|
-
|
|
42
|
-
for (let i = 0; i < sepTokens.length; i++) {
|
|
43
|
-
res.push(actualElements[i], sepTokens[i]);
|
|
44
|
-
if (sep) {
|
|
45
|
-
res.push(sep);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
res.push(...actualElements.slice(sepTokens.length));
|
|
49
|
-
return concat(res);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function reject(elems) {
|
|
53
|
-
return elems.filter(item => {
|
|
54
|
-
if (typeof item === "string") {
|
|
55
|
-
return item !== "";
|
|
56
|
-
}
|
|
57
|
-
// eslint-ignore next - We want the conversion to boolean!
|
|
58
|
-
return item != false && item !== undefined;
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function rejectSeparators(separators, elems) {
|
|
63
|
-
const realElements = reject(elems);
|
|
64
|
-
|
|
65
|
-
const realSeparators = [];
|
|
66
|
-
for (let i = 0; i < realElements.length - 1; i++) {
|
|
67
|
-
if (realElements[i] !== "") {
|
|
68
|
-
realSeparators.push(separators[i]);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return realSeparators;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function rejectAndJoin(sep, elems) {
|
|
76
|
-
const actualElements = reject(elems);
|
|
77
|
-
|
|
78
|
-
return join(sep, actualElements);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function rejectAndConcat(elems) {
|
|
82
|
-
const actualElements = reject(elems);
|
|
83
|
-
|
|
84
|
-
return concat(actualElements);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function sortAnnotationIdentifier(annotations, identifiers) {
|
|
88
|
-
let tokens = [...identifiers];
|
|
89
|
-
|
|
90
|
-
if (annotations && annotations.length > 0) {
|
|
91
|
-
tokens = [...tokens, ...annotations];
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return tokens.sort((a, b) => {
|
|
95
|
-
const startOffset1 =
|
|
96
|
-
a.name === "annotation" ? a.children.At[0].startOffset : a.startOffset;
|
|
97
|
-
const startOffset2 =
|
|
98
|
-
b.name === "annotation" ? b.children.At[0].startOffset : b.startOffset;
|
|
99
|
-
return startOffset1 - startOffset2;
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function sortTokens() {
|
|
104
|
-
let tokens = [];
|
|
105
|
-
|
|
106
|
-
forEach(arguments, argument => {
|
|
107
|
-
if (argument) {
|
|
108
|
-
tokens = tokens.concat(argument);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
return tokens.sort((a, b) => {
|
|
113
|
-
return a.startOffset - b.startOffset;
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function sortNodes() {
|
|
118
|
-
let nodes = [];
|
|
119
|
-
|
|
120
|
-
forEach(arguments, argument => {
|
|
121
|
-
if (argument) {
|
|
122
|
-
nodes = nodes.concat(argument);
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
return nodes.sort((a, b) => {
|
|
127
|
-
const aOffset = a.startOffset ? a.startOffset : a.location.startOffset;
|
|
128
|
-
const bOffset = b.startOffset ? b.startOffset : b.location.startOffset;
|
|
129
|
-
return aOffset - bOffset;
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function matchCategory(token, categoryName) {
|
|
134
|
-
const labels = token.tokenType.CATEGORIES.map(category => {
|
|
135
|
-
return category.LABEL;
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
return labels.indexOf(categoryName) !== -1;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function sortClassTypeChildren(annotations, typeArguments, identifiers, dots) {
|
|
142
|
-
let tokens = [...identifiers];
|
|
143
|
-
|
|
144
|
-
if (annotations && annotations.length > 0) {
|
|
145
|
-
tokens = [...tokens, ...annotations];
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (typeArguments && typeArguments.length > 0) {
|
|
149
|
-
tokens = [...tokens, ...typeArguments];
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (dots && dots.length > 0) {
|
|
153
|
-
tokens = [...tokens, ...dots];
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return tokens.sort((a, b) => {
|
|
157
|
-
const startOffsetA = a.name
|
|
158
|
-
? a.children.At
|
|
159
|
-
? a.children.At[0].startOffset
|
|
160
|
-
: a.children.Less[0].startOffset
|
|
161
|
-
: a.startOffset;
|
|
162
|
-
const startOffsetB = b.name
|
|
163
|
-
? b.children.At
|
|
164
|
-
? b.children.At[0].startOffset
|
|
165
|
-
: b.children.Less[0].startOffset
|
|
166
|
-
: b.startOffset;
|
|
167
|
-
return startOffsetA - startOffsetB;
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function sortModifiers(modifiers) {
|
|
172
|
-
let firstAnnotations = [];
|
|
173
|
-
const otherModifiers = [];
|
|
174
|
-
let lastAnnotations = [];
|
|
175
|
-
let hasOtherModifier = false;
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* iterate in reverse order because we special-case
|
|
179
|
-
* method annotations which come after all other
|
|
180
|
-
* modifiers
|
|
181
|
-
*/
|
|
182
|
-
forEachRight(modifiers, modifier => {
|
|
183
|
-
const isAnnotation = modifier.children.annotation !== undefined;
|
|
184
|
-
const isMethodAnnotation =
|
|
185
|
-
isAnnotation &&
|
|
186
|
-
(modifier.name === "methodModifier" ||
|
|
187
|
-
modifier.name === "interfaceMethodModifier");
|
|
188
|
-
|
|
189
|
-
if (isAnnotation) {
|
|
190
|
-
if (isMethodAnnotation && !hasOtherModifier) {
|
|
191
|
-
lastAnnotations.unshift(modifier);
|
|
192
|
-
} else {
|
|
193
|
-
firstAnnotations.unshift(modifier);
|
|
194
|
-
}
|
|
195
|
-
} else {
|
|
196
|
-
otherModifiers.unshift(modifier);
|
|
197
|
-
hasOtherModifier = true;
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* if there are only annotations, move everything from
|
|
203
|
-
* lastAnnotations to firstAnnotations
|
|
204
|
-
*/
|
|
205
|
-
if (!hasOtherModifier) {
|
|
206
|
-
firstAnnotations = firstAnnotations.concat(lastAnnotations);
|
|
207
|
-
lastAnnotations = [];
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
otherModifiers.sort((a, b) => {
|
|
211
|
-
const modifierIndexA = orderedModifiers.indexOf(Object.keys(a.children)[0]);
|
|
212
|
-
const modifierIndexB = orderedModifiers.indexOf(Object.keys(b.children)[0]);
|
|
213
|
-
|
|
214
|
-
return modifierIndexA - modifierIndexB;
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
return [firstAnnotations, otherModifiers.concat(lastAnnotations)];
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function findDeepElementInPartsArray(item, elt) {
|
|
221
|
-
if (Array.isArray(item)) {
|
|
222
|
-
if (item.includes(elt)) {
|
|
223
|
-
return true;
|
|
224
|
-
}
|
|
225
|
-
for (let i = 0; i < item.length; i++) {
|
|
226
|
-
if (findDeepElementInPartsArray(item[i], elt)) {
|
|
227
|
-
return true;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
} else {
|
|
231
|
-
for (const key in item) {
|
|
232
|
-
if (
|
|
233
|
-
typeof item[key] === "object" &&
|
|
234
|
-
findDeepElementInPartsArray(item[key], elt)
|
|
235
|
-
) {
|
|
236
|
-
return true;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return false;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
function displaySemicolon(token, params) {
|
|
245
|
-
if (params !== undefined && params.allowEmptyStatement) {
|
|
246
|
-
return printTokenWithComments(token);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (!hasComments(token)) {
|
|
250
|
-
return "";
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
token.image = "";
|
|
254
|
-
return printTokenWithComments(token);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function isExplicitLambdaParameter(ctx) {
|
|
258
|
-
return (
|
|
259
|
-
ctx &&
|
|
260
|
-
ctx.lambdaParameterList &&
|
|
261
|
-
ctx.lambdaParameterList[0] &&
|
|
262
|
-
ctx.lambdaParameterList[0].children &&
|
|
263
|
-
ctx.lambdaParameterList[0].children.explicitLambdaParameterList
|
|
264
|
-
);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function getBlankLinesSeparator(ctx) {
|
|
268
|
-
if (ctx === undefined) {
|
|
269
|
-
return undefined;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const separators = [];
|
|
273
|
-
for (let i = 0; i < ctx.length - 1; i++) {
|
|
274
|
-
const previousRuleEndLineWithComment =
|
|
275
|
-
ctx[i].trailingComments !== undefined
|
|
276
|
-
? ctx[i].trailingComments[ctx[i].trailingComments.length - 1].endLine
|
|
277
|
-
: ctx[i].location.endLine;
|
|
278
|
-
|
|
279
|
-
const nextRuleStartLineWithComment =
|
|
280
|
-
ctx[i + 1].leadingComments !== undefined
|
|
281
|
-
? ctx[i + 1].leadingComments[0].startLine
|
|
282
|
-
: ctx[i + 1].location.startLine;
|
|
283
|
-
|
|
284
|
-
if (nextRuleStartLineWithComment - previousRuleEndLineWithComment > 1) {
|
|
285
|
-
separators.push(concat([hardline, hardline]));
|
|
286
|
-
} else {
|
|
287
|
-
separators.push(hardline);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
return separators;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
function getDeclarationsSeparator(
|
|
295
|
-
declarations,
|
|
296
|
-
needLineDeclaration,
|
|
297
|
-
isSemicolon
|
|
298
|
-
) {
|
|
299
|
-
const declarationsWithoutEmptyStatements = declarations.filter(
|
|
300
|
-
declaration => !isSemicolon(declaration)
|
|
301
|
-
);
|
|
302
|
-
|
|
303
|
-
const userBlankLinesSeparators = getBlankLinesSeparator(
|
|
304
|
-
declarationsWithoutEmptyStatements
|
|
305
|
-
);
|
|
306
|
-
const additionalBlankLines =
|
|
307
|
-
declarationsWithoutEmptyStatements.map(needLineDeclaration);
|
|
308
|
-
|
|
309
|
-
const separators = [];
|
|
310
|
-
let indexNextNotEmptyDeclaration = 0;
|
|
311
|
-
for (let i = 0; i < declarations.length - 1; i++) {
|
|
312
|
-
// if the empty statement has comments
|
|
313
|
-
// we want to print them on their own line
|
|
314
|
-
if (isSemicolon(declarations[i])) {
|
|
315
|
-
if (hasComments(declarations[i])) {
|
|
316
|
-
separators.push(hardline);
|
|
317
|
-
}
|
|
318
|
-
} else if (
|
|
319
|
-
indexNextNotEmptyDeclaration <
|
|
320
|
-
declarationsWithoutEmptyStatements.length - 1
|
|
321
|
-
) {
|
|
322
|
-
const isTwoHardLines =
|
|
323
|
-
userBlankLinesSeparators[indexNextNotEmptyDeclaration].parts[0].type ===
|
|
324
|
-
"concat";
|
|
325
|
-
const additionalSep =
|
|
326
|
-
!isTwoHardLines &&
|
|
327
|
-
(additionalBlankLines[indexNextNotEmptyDeclaration + 1] ||
|
|
328
|
-
additionalBlankLines[indexNextNotEmptyDeclaration])
|
|
329
|
-
? hardline
|
|
330
|
-
: "";
|
|
331
|
-
separators.push(
|
|
332
|
-
concat([
|
|
333
|
-
userBlankLinesSeparators[indexNextNotEmptyDeclaration],
|
|
334
|
-
additionalSep
|
|
335
|
-
])
|
|
336
|
-
);
|
|
337
|
-
|
|
338
|
-
indexNextNotEmptyDeclaration += 1;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
return separators;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
function needLineClassBodyDeclaration(declaration) {
|
|
346
|
-
if (declaration.children.classMemberDeclaration === undefined) {
|
|
347
|
-
return true;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
const classMemberDeclaration = declaration.children.classMemberDeclaration[0];
|
|
351
|
-
|
|
352
|
-
if (classMemberDeclaration.children.fieldDeclaration !== undefined) {
|
|
353
|
-
const fieldDeclaration =
|
|
354
|
-
classMemberDeclaration.children.fieldDeclaration[0];
|
|
355
|
-
if (
|
|
356
|
-
fieldDeclaration.children.fieldModifier !== undefined &&
|
|
357
|
-
hasAnnotation(fieldDeclaration.children.fieldModifier)
|
|
358
|
-
) {
|
|
359
|
-
return true;
|
|
360
|
-
}
|
|
361
|
-
return false;
|
|
362
|
-
} else if (classMemberDeclaration.children.Semicolon !== undefined) {
|
|
363
|
-
return false;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
return true;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
function needLineInterfaceMemberDeclaration(declaration) {
|
|
370
|
-
if (declaration.children.constantDeclaration !== undefined) {
|
|
371
|
-
const constantDeclaration = declaration.children.constantDeclaration[0];
|
|
372
|
-
if (
|
|
373
|
-
constantDeclaration.children.constantModifier !== undefined &&
|
|
374
|
-
hasAnnotation(constantDeclaration.children.constantModifier)
|
|
375
|
-
) {
|
|
376
|
-
return true;
|
|
377
|
-
}
|
|
378
|
-
return false;
|
|
379
|
-
} else if (declaration.children.interfaceMethodDeclaration !== undefined) {
|
|
380
|
-
const interfaceMethodDeclaration =
|
|
381
|
-
declaration.children.interfaceMethodDeclaration[0];
|
|
382
|
-
if (
|
|
383
|
-
interfaceMethodDeclaration.children.interfaceMethodModifier !==
|
|
384
|
-
undefined &&
|
|
385
|
-
hasNonTrailingAnnotation(
|
|
386
|
-
interfaceMethodDeclaration.children.interfaceMethodModifier
|
|
387
|
-
)
|
|
388
|
-
) {
|
|
389
|
-
return true;
|
|
390
|
-
}
|
|
391
|
-
return false;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
return true;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
function isClassBodyDeclarationASemicolon(classBodyDeclaration) {
|
|
398
|
-
if (classBodyDeclaration.children.classMemberDeclaration) {
|
|
399
|
-
if (
|
|
400
|
-
classBodyDeclaration.children.classMemberDeclaration[0].children
|
|
401
|
-
.Semicolon !== undefined
|
|
402
|
-
) {
|
|
403
|
-
return true;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
return false;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
function isInterfaceMemberASemicolon(interfaceMemberDeclaration) {
|
|
410
|
-
return interfaceMemberDeclaration.children.Semicolon !== undefined;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
function hasAnnotation(modifiers) {
|
|
414
|
-
return modifiers.some(modifier => modifier.children.annotation !== undefined);
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
/**
|
|
418
|
-
* Return true if there is a method modifier that does not come after all other modifiers
|
|
419
|
-
* It is useful to know if sortModifiers will add an annotation before other modifiers
|
|
420
|
-
*
|
|
421
|
-
* @param methodModifiers
|
|
422
|
-
* @returns {boolean}
|
|
423
|
-
*/
|
|
424
|
-
function hasNonTrailingAnnotation(methodModifiers) {
|
|
425
|
-
const firstAnnotationIndex = methodModifiers.findIndex(
|
|
426
|
-
modifier => modifier.children.annotation !== undefined
|
|
427
|
-
);
|
|
428
|
-
const lastNonAnnotationIndex = findLastIndex(
|
|
429
|
-
methodModifiers,
|
|
430
|
-
modifier => modifier.children.annotation === undefined
|
|
431
|
-
);
|
|
432
|
-
|
|
433
|
-
return (
|
|
434
|
-
firstAnnotationIndex < lastNonAnnotationIndex ||
|
|
435
|
-
lastNonAnnotationIndex === -1
|
|
436
|
-
);
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
function getClassBodyDeclarationsSeparator(classBodyDeclarationContext) {
|
|
440
|
-
return getDeclarationsSeparator(
|
|
441
|
-
classBodyDeclarationContext,
|
|
442
|
-
needLineClassBodyDeclaration,
|
|
443
|
-
isClassBodyDeclarationASemicolon
|
|
444
|
-
);
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
function getInterfaceBodyDeclarationsSeparator(
|
|
448
|
-
interfaceMemberDeclarationContext
|
|
449
|
-
) {
|
|
450
|
-
return getDeclarationsSeparator(
|
|
451
|
-
interfaceMemberDeclarationContext,
|
|
452
|
-
needLineInterfaceMemberDeclaration,
|
|
453
|
-
isInterfaceMemberASemicolon
|
|
454
|
-
);
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
function putIntoBraces(argument, separator, LBrace, RBrace) {
|
|
458
|
-
const rightBraceLeadingComments = getTokenLeadingComments(RBrace);
|
|
459
|
-
const lastBreakLine =
|
|
460
|
-
// check if last element of the array is a line
|
|
461
|
-
rightBraceLeadingComments.length !== 0 &&
|
|
462
|
-
rightBraceLeadingComments[rightBraceLeadingComments.length - 1] === hardline
|
|
463
|
-
? rightBraceLeadingComments.pop()
|
|
464
|
-
: separator;
|
|
465
|
-
delete RBrace.leadingComments;
|
|
466
|
-
|
|
467
|
-
let contentInsideBraces;
|
|
468
|
-
if (argument === undefined || argument === "") {
|
|
469
|
-
if (rightBraceLeadingComments.length === 0) {
|
|
470
|
-
return concat([LBrace, RBrace]);
|
|
471
|
-
}
|
|
472
|
-
contentInsideBraces = [separator, ...rightBraceLeadingComments];
|
|
473
|
-
} else if (rightBraceLeadingComments.length !== 0) {
|
|
474
|
-
contentInsideBraces = [
|
|
475
|
-
separator,
|
|
476
|
-
argument,
|
|
477
|
-
separator,
|
|
478
|
-
...rightBraceLeadingComments
|
|
479
|
-
];
|
|
480
|
-
} else {
|
|
481
|
-
contentInsideBraces = [separator, argument];
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
return group(
|
|
485
|
-
rejectAndConcat([
|
|
486
|
-
LBrace,
|
|
487
|
-
indent(concat(contentInsideBraces)),
|
|
488
|
-
lastBreakLine,
|
|
489
|
-
RBrace
|
|
490
|
-
])
|
|
491
|
-
);
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
const andOrBinaryOperators = new Set(["&&", "||", "&", "|", "^"]);
|
|
495
|
-
function separateTokensIntoGroups(ctx) {
|
|
496
|
-
/**
|
|
497
|
-
* separate tokens into groups by andOrBinaryOperators ("&&", "||", "&", "|", "^")
|
|
498
|
-
* in order to break those operators in priority.
|
|
499
|
-
*/
|
|
500
|
-
const tokens = sortTokens(
|
|
501
|
-
ctx.Instanceof,
|
|
502
|
-
ctx.AssignmentOperator,
|
|
503
|
-
ctx.Less,
|
|
504
|
-
ctx.Greater,
|
|
505
|
-
ctx.BinaryOperator
|
|
506
|
-
);
|
|
507
|
-
|
|
508
|
-
const groupsOfOperator = [];
|
|
509
|
-
const sortedBinaryOperators = [];
|
|
510
|
-
let tmpGroup = [];
|
|
511
|
-
tokens.forEach(token => {
|
|
512
|
-
if (
|
|
513
|
-
matchCategory(token, "'BinaryOperator'") &&
|
|
514
|
-
andOrBinaryOperators.has(token.image)
|
|
515
|
-
) {
|
|
516
|
-
sortedBinaryOperators.push(token);
|
|
517
|
-
groupsOfOperator.push(tmpGroup);
|
|
518
|
-
tmpGroup = [];
|
|
519
|
-
} else {
|
|
520
|
-
tmpGroup.push(token);
|
|
521
|
-
}
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
groupsOfOperator.push(tmpGroup);
|
|
525
|
-
|
|
526
|
-
return {
|
|
527
|
-
groupsOfOperator,
|
|
528
|
-
sortedBinaryOperators
|
|
529
|
-
};
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
function isShiftOperator(tokens, index) {
|
|
533
|
-
if (tokens.length <= index + 1) {
|
|
534
|
-
return "none";
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
if (
|
|
538
|
-
tokens[index].image === "<" &&
|
|
539
|
-
tokens[index + 1].image === "<" &&
|
|
540
|
-
tokens[index].startOffset === tokens[index + 1].startOffset - 1
|
|
541
|
-
) {
|
|
542
|
-
return "leftShift";
|
|
543
|
-
}
|
|
544
|
-
if (
|
|
545
|
-
tokens[index].image === ">" &&
|
|
546
|
-
tokens[index + 1].image === ">" &&
|
|
547
|
-
tokens[index].startOffset === tokens[index + 1].startOffset - 1
|
|
548
|
-
) {
|
|
549
|
-
if (
|
|
550
|
-
tokens.length > index + 2 &&
|
|
551
|
-
tokens[index + 2].image === ">" &&
|
|
552
|
-
tokens[index + 1].startOffset === tokens[index + 2].startOffset - 1
|
|
553
|
-
) {
|
|
554
|
-
return "doubleRightShift";
|
|
555
|
-
}
|
|
556
|
-
return "rightShift";
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
return "none";
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
function retrieveNodesToken(ctx) {
|
|
563
|
-
const tokens = retrieveNodesTokenRec(ctx);
|
|
564
|
-
tokens.sort((token1, token2) => {
|
|
565
|
-
return token1.startOffset - token2.startOffset;
|
|
566
|
-
});
|
|
567
|
-
return tokens;
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
function retrieveNodesTokenRec(ctx) {
|
|
571
|
-
const tokens = [];
|
|
572
|
-
if (
|
|
573
|
-
ctx &&
|
|
574
|
-
Object.prototype.hasOwnProperty.call(ctx, "image") &&
|
|
575
|
-
ctx.tokenType
|
|
576
|
-
) {
|
|
577
|
-
if (ctx.leadingComments) {
|
|
578
|
-
tokens.push(...ctx.leadingComments);
|
|
579
|
-
}
|
|
580
|
-
tokens.push(ctx);
|
|
581
|
-
if (ctx.trailingComments) {
|
|
582
|
-
tokens.push(...ctx.trailingComments);
|
|
583
|
-
}
|
|
584
|
-
return tokens;
|
|
585
|
-
}
|
|
586
|
-
Object.keys(ctx.children).forEach(child => {
|
|
587
|
-
ctx.children[child].forEach(subctx => {
|
|
588
|
-
tokens.push(...retrieveNodesTokenRec(subctx));
|
|
589
|
-
});
|
|
590
|
-
});
|
|
591
|
-
return tokens;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
function isStatementEmptyStatement(statement) {
|
|
595
|
-
return (
|
|
596
|
-
statement === ";" ||
|
|
597
|
-
(statement.type === "concat" && statement.parts[0] === ";")
|
|
598
|
-
);
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
function sortImports(imports) {
|
|
602
|
-
const staticImports = [];
|
|
603
|
-
const nonStaticImports = [];
|
|
604
|
-
|
|
605
|
-
if (imports !== undefined) {
|
|
606
|
-
for (let i = 0; i < imports.length; i++) {
|
|
607
|
-
if (imports[i].children.Static !== undefined) {
|
|
608
|
-
staticImports.push(imports[i]);
|
|
609
|
-
} else if (imports[i].children.emptyStatement === undefined) {
|
|
610
|
-
nonStaticImports.push(imports[i]);
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
// TODO: Could be optimized as we could expect that the array is already almost sorted
|
|
615
|
-
const comparator = (first, second) =>
|
|
616
|
-
compareFqn(
|
|
617
|
-
first.children.packageOrTypeName[0],
|
|
618
|
-
second.children.packageOrTypeName[0]
|
|
619
|
-
);
|
|
620
|
-
staticImports.sort(comparator);
|
|
621
|
-
nonStaticImports.sort(comparator);
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
return {
|
|
625
|
-
staticImports,
|
|
626
|
-
nonStaticImports
|
|
627
|
-
};
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
function compareFqn(packageOrTypeNameFirst, packageOrTypeNameSecond) {
|
|
631
|
-
const identifiersFirst = packageOrTypeNameFirst.children.Identifier;
|
|
632
|
-
const identifiersSecond = packageOrTypeNameSecond.children.Identifier;
|
|
633
|
-
|
|
634
|
-
const minParts = Math.min(identifiersFirst.length, identifiersSecond.length);
|
|
635
|
-
for (let i = 0; i < minParts; i++) {
|
|
636
|
-
if (identifiersFirst[i].image < identifiersSecond[i].image) {
|
|
637
|
-
return -1;
|
|
638
|
-
} else if (identifiersFirst[i].image > identifiersSecond[i].image) {
|
|
639
|
-
return 1;
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
if (identifiersFirst.length < identifiersSecond.length) {
|
|
644
|
-
return -1;
|
|
645
|
-
} else if (identifiersFirst.length > identifiersSecond.length) {
|
|
646
|
-
return 1;
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
return 0;
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
function isUniqueMethodInvocation(primarySuffixes) {
|
|
653
|
-
if (primarySuffixes === undefined) {
|
|
654
|
-
return 0;
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
let count = 0;
|
|
658
|
-
primarySuffixes.forEach(primarySuffix => {
|
|
659
|
-
if (primarySuffix.children.methodInvocationSuffix !== undefined) {
|
|
660
|
-
count++;
|
|
661
|
-
|
|
662
|
-
if (count > 1) {
|
|
663
|
-
return 2;
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
});
|
|
667
|
-
|
|
668
|
-
return count;
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
function printArrayList({ list, extraComma, LCurly, RCurly, trailingComma }) {
|
|
672
|
-
let optionalComma;
|
|
673
|
-
if (trailingComma !== "none" && list !== "") {
|
|
674
|
-
optionalComma = extraComma
|
|
675
|
-
? ifBreak(extraComma[0], { ...extraComma[0], image: "" })
|
|
676
|
-
: ifBreak(",", "");
|
|
677
|
-
} else {
|
|
678
|
-
optionalComma = extraComma ? { ...extraComma[0], image: "" } : "";
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
return putIntoBraces(
|
|
682
|
-
rejectAndConcat([list, optionalComma]),
|
|
683
|
-
line,
|
|
684
|
-
LCurly,
|
|
685
|
-
RCurly
|
|
686
|
-
);
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
module.exports = {
|
|
690
|
-
buildFqn,
|
|
691
|
-
reject,
|
|
692
|
-
rejectAndJoin,
|
|
693
|
-
rejectAndConcat,
|
|
694
|
-
sortAnnotationIdentifier,
|
|
695
|
-
sortClassTypeChildren,
|
|
696
|
-
sortNodes,
|
|
697
|
-
matchCategory,
|
|
698
|
-
sortModifiers,
|
|
699
|
-
rejectAndJoinSeps,
|
|
700
|
-
findDeepElementInPartsArray,
|
|
701
|
-
isExplicitLambdaParameter,
|
|
702
|
-
getBlankLinesSeparator,
|
|
703
|
-
displaySemicolon,
|
|
704
|
-
rejectSeparators,
|
|
705
|
-
putIntoBraces,
|
|
706
|
-
getInterfaceBodyDeclarationsSeparator,
|
|
707
|
-
getClassBodyDeclarationsSeparator,
|
|
708
|
-
separateTokensIntoGroups,
|
|
709
|
-
isShiftOperator,
|
|
710
|
-
retrieveNodesToken,
|
|
711
|
-
isStatementEmptyStatement,
|
|
712
|
-
sortImports,
|
|
713
|
-
isUniqueMethodInvocation,
|
|
714
|
-
printArrayList
|
|
715
|
-
};
|