prettier-plugin-java 0.7.0 → 0.8.2

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/README.md CHANGED
@@ -115,3 +115,45 @@ public class HelloWorld {
115
115
  }
116
116
  }
117
117
  ```
118
+
119
+ ## Options
120
+ We added a custom option ```entrypoint``` in order to run prettier on code snippet.
121
+
122
+ ### Usage
123
+ ```prettier --write MyJava.java --entrypoint compilationUnit``` \
124
+ [Here](https://github.com/jhipster/prettier-java/blob/master/packages/prettier-plugin-java/src/options.js) is the exhaustive list of all entrypoints.
125
+
126
+ ### Example
127
+ MyJavaCode.java content:
128
+ ```java
129
+ public void myfunction() {
130
+ mymethod.is().very().very().very().very().very().very().very().very().very().very().very().very().very().very().big();
131
+ }
132
+ ```
133
+
134
+ Run: \
135
+ ```prettier --write MyJavaCode.java --entrypoint classBodyDeclaration```
136
+
137
+ Result:
138
+ ```java
139
+ public void myfunction() {
140
+ mymethod
141
+ .is()
142
+ .very()
143
+ .very()
144
+ .very()
145
+ .very()
146
+ .very()
147
+ .very()
148
+ .very()
149
+ .very()
150
+ .very()
151
+ .very()
152
+ .very()
153
+ .very()
154
+ .very()
155
+ .very()
156
+ .big();
157
+ }
158
+ ```
159
+
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "prettier-plugin-java",
3
- "version": "0.7.0",
3
+ "version": "0.8.2",
4
4
  "description": "Prettier Java Plugin",
5
5
  "main": "src/index.js",
6
6
  "repository": "https://github.com/jhipster/prettier-java",
7
7
  "license": "Apache-2.0",
8
8
  "dependencies": {
9
- "java-parser": "^0.7.0",
10
- "lodash": "4.17.15",
11
- "prettier": "1.18.2"
9
+ "java-parser": "0.8.2",
10
+ "lodash": "4.17.20",
11
+ "prettier": "2.1.1"
12
12
  },
13
13
  "scripts": {
14
14
  "test": "npm-run-all test:unit test:e2e-core",
@@ -19,6 +19,5 @@
19
19
  "test:e2e-jhipster2": "node scripts/clone-samples e2e-jhipster2 && mocha \"test/repository-test/jhipster-2-test.js\"",
20
20
  "test:all": "npm-run-all test test:e2e-jhipster1 test:e2e-jhipster2",
21
21
  "clone-samples": "node scripts/clone-samples.js"
22
- },
23
- "gitHead": "7cf83bececc20e6c658a8b07df5d8dd056a3711f"
22
+ }
24
23
  }
@@ -40,7 +40,7 @@ class CstPrettierPrinter extends BaseJavaCstVisitor {
40
40
  return elements.map(element => this.visit(element, params), this);
41
41
  };
42
42
 
43
- this.getSingle = function(ctx) {
43
+ this.getSingle = function (ctx) {
44
44
  const ctxKeys = Object.keys(ctx);
45
45
  if (ctxKeys.length !== 1) {
46
46
  throw Error(
@@ -59,7 +59,7 @@ class CstPrettierPrinter extends BaseJavaCstVisitor {
59
59
  return singleElementValues[0];
60
60
  };
61
61
 
62
- this.visitSingle = function(ctx, params) {
62
+ this.visitSingle = function (ctx, params) {
63
63
  const singleElement = this.getSingle(ctx);
64
64
  return this.visit(singleElement, params);
65
65
  };
@@ -68,7 +68,7 @@ class CstPrettierPrinter extends BaseJavaCstVisitor {
68
68
  // the prototype because we cannot user "super.visit" inside the function
69
69
  // below
70
70
  const orgVisit = this.visit;
71
- this.visit = function(ctx, inParam) {
71
+ this.visit = function (ctx, inParam) {
72
72
  if (ctx === undefined) {
73
73
  // empty Doc
74
74
  return "";
package/src/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const parse = require("./parser");
4
4
  const print = require("./printer");
5
+ const options = require("./options");
5
6
 
6
7
  const languages = [
7
8
  {
@@ -73,5 +74,6 @@ const printers = {
73
74
  module.exports = {
74
75
  languages,
75
76
  printers,
76
- parsers
77
+ parsers,
78
+ options
77
79
  };
package/src/options.js ADDED
@@ -0,0 +1,239 @@
1
+ "use strict";
2
+
3
+ module.exports = {
4
+ entrypoint: {
5
+ type: "choice",
6
+ category: "Global",
7
+ default: "compilationUnit",
8
+ // sed -nr 's/.*\.RULE\(([^,]+),.*/\1/p' $(ls path/to/java-parser/rules/folder/*)
9
+ choices: [
10
+ { value: "arrayInitializer" },
11
+ { value: "variableInitializerList" },
12
+ { value: "block" },
13
+ { value: "blockStatements" },
14
+ { value: "blockStatement" },
15
+ { value: "localVariableDeclarationStatement" },
16
+ { value: "localVariableDeclaration" },
17
+ { value: "localVariableType" },
18
+ { value: "statement" },
19
+ { value: "statementWithoutTrailingSubstatement" },
20
+ { value: "emptyStatement" },
21
+ { value: "labeledStatement" },
22
+ { value: "expressionStatement" },
23
+ { value: "statementExpression" },
24
+ { value: "ifStatement" },
25
+ { value: "assertStatement" },
26
+ { value: "switchStatement" },
27
+ { value: "switchBlock" },
28
+ { value: "switchCase" },
29
+ { value: "switchLabel" },
30
+ { value: "enumConstantName" },
31
+ { value: "whileStatement" },
32
+ { value: "doStatement" },
33
+ { value: "forStatement" },
34
+ { value: "basicForStatement" },
35
+ { value: "forInit" },
36
+ { value: "forUpdate" },
37
+ { value: "statementExpressionList" },
38
+ { value: "enhancedForStatement" },
39
+ { value: "breakStatement" },
40
+ { value: "continueStatement" },
41
+ { value: "returnStatement" },
42
+ { value: "throwStatement" },
43
+ { value: "synchronizedStatement" },
44
+ { value: "tryStatement" },
45
+ { value: "catches" },
46
+ { value: "catchClause" },
47
+ { value: "catchFormalParameter" },
48
+ { value: "catchType" },
49
+ { value: "finally" },
50
+ { value: "tryWithResourcesStatement" },
51
+ { value: "resourceSpecification" },
52
+ { value: "resourceList" },
53
+ { value: "resource" },
54
+ { value: "resourceInit" },
55
+ { value: "variableAccess" },
56
+ { value: "isBasicForStatement" },
57
+ { value: "isLocalVariableDeclaration" },
58
+ { value: "classDeclaration" },
59
+ { value: "normalClassDeclaration" },
60
+ { value: "classModifier" },
61
+ { value: "typeParameters" },
62
+ { value: "typeParameterList" },
63
+ { value: "superclass" },
64
+ { value: "superinterfaces" },
65
+ { value: "interfaceTypeList" },
66
+ { value: "classBody" },
67
+ { value: "classBodyDeclaration" },
68
+ { value: "classMemberDeclaration" },
69
+ { value: "fieldDeclaration" },
70
+ { value: "fieldModifier" },
71
+ { value: "variableDeclaratorList" },
72
+ { value: "variableDeclarator" },
73
+ { value: "variableDeclaratorId" },
74
+ { value: "variableInitializer" },
75
+ { value: "unannType" },
76
+ { value: "unannPrimitiveType" },
77
+ { value: "unannReferenceType" },
78
+ { value: "unannClassOrInterfaceType" },
79
+ { value: "unannClassType" },
80
+ { value: "unannInterfaceType" },
81
+ { value: "unannTypeVariable" },
82
+ { value: "methodDeclaration" },
83
+ { value: "methodModifier" },
84
+ { value: "methodHeader" },
85
+ { value: "result" },
86
+ { value: "methodDeclarator" },
87
+ { value: "receiverParameter" },
88
+ { value: "formalParameterList" },
89
+ { value: "formalParameter" },
90
+ { value: "variableParaRegularParameter" },
91
+ { value: "variableArityParameter" },
92
+ { value: "variableModifier" },
93
+ { value: "throws" },
94
+ { value: "exceptionTypeList" },
95
+ { value: "exceptionType" },
96
+ { value: "methodBody" },
97
+ { value: "instanceInitializer" },
98
+ { value: "staticInitializer" },
99
+ { value: "constructorDeclaration" },
100
+ { value: "constructorModifier" },
101
+ { value: "constructorDeclarator" },
102
+ { value: "simpleTypeName" },
103
+ { value: "constructorBody" },
104
+ { value: "explicitConstructorInvocation" },
105
+ { value: "unqualifiedExplicitConstructorInvocation" },
106
+ { value: "qualifiedExplicitConstructorInvocation" },
107
+ { value: "enumDeclaration" },
108
+ { value: "enumBody" },
109
+ { value: "enumConstantList" },
110
+ { value: "enumConstant" },
111
+ { value: "enumConstantModifier" },
112
+ { value: "enumBodyDeclarations" },
113
+ { value: "isClassDeclaration" },
114
+ { value: "identifyClassBodyDeclarationType" },
115
+ { value: "isDims" },
116
+ { value: "constantExpression" },
117
+ { value: "expression" },
118
+ { value: "lambdaExpression" },
119
+ { value: "lambdaParameters" },
120
+ { value: "lambdaParametersWithBraces" },
121
+ { value: "lambdaParameterList" },
122
+ { value: "inferredLambdaParameterList" },
123
+ { value: "explicitLambdaParameterList" },
124
+ { value: "lambdaParameter" },
125
+ { value: "regularLambdaParameter" },
126
+ { value: "lambdaParameterType" },
127
+ { value: "lambdaBody" },
128
+ { value: "ternaryExpression" },
129
+ { value: "binaryExpression" },
130
+ { value: "unaryExpression" },
131
+ { value: "unaryExpressionNotPlusMinus" },
132
+ { value: "primary" },
133
+ { value: "primaryPrefix" },
134
+ { value: "primarySuffix" },
135
+ { value: "fqnOrRefType" },
136
+ { value: "fqnOrRefTypePartRest" },
137
+ { value: "fqnOrRefTypePartCommon" },
138
+ { value: "fqnOrRefTypePartFirst" },
139
+ { value: "parenthesisExpression" },
140
+ { value: "castExpression" },
141
+ { value: "primitiveCastExpression" },
142
+ { value: "referenceTypeCastExpression" },
143
+ { value: "newExpression" },
144
+ { value: "unqualifiedClassInstanceCreationExpression" },
145
+ { value: "classOrInterfaceTypeToInstantiate" },
146
+ { value: "typeArgumentsOrDiamond" },
147
+ { value: "diamond" },
148
+ { value: "methodInvocationSuffix" },
149
+ { value: "argumentList" },
150
+ { value: "arrayCreationExpression" },
151
+ { value: "arrayCreationDefaultInitSuffix" },
152
+ { value: "arrayCreationExplicitInitSuffix" },
153
+ { value: "dimExprs" },
154
+ { value: "dimExpr" },
155
+ { value: "classLiteralSuffix" },
156
+ { value: "arrayAccessSuffix" },
157
+ { value: "methodReferenceSuffix" },
158
+ { value: "identifyNewExpressionType" },
159
+ { value: "isLambdaExpression" },
160
+ { value: "isCastExpression" },
161
+ { value: "isPrimitiveCastExpression" },
162
+ { value: "isReferenceTypeCastExpression" },
163
+ { value: "isRefTypeInMethodRef" },
164
+ { value: "interfaceDeclaration" },
165
+ { value: "normalInterfaceDeclaration" },
166
+ { value: "interfaceModifier" },
167
+ { value: "extendsInterfaces" },
168
+ { value: "interfaceBody" },
169
+ { value: "interfaceMemberDeclaration" },
170
+ { value: "constantDeclaration" },
171
+ { value: "constantModifier" },
172
+ { value: "interfaceMethodDeclaration" },
173
+ { value: "interfaceMethodModifier" },
174
+ { value: "annotationTypeDeclaration" },
175
+ { value: "annotationTypeBody" },
176
+ { value: "annotationTypeMemberDeclaration" },
177
+ { value: "annotationTypeElementDeclaration" },
178
+ { value: "annotationTypeElementModifier" },
179
+ { value: "defaultValue" },
180
+ { value: "annotation" },
181
+ { value: "elementValuePairList" },
182
+ { value: "elementValuePair" },
183
+ { value: "elementValue" },
184
+ { value: "elementValueArrayInitializer" },
185
+ { value: "elementValueList" },
186
+ { value: "identifyInterfaceBodyDeclarationType" },
187
+ { value: "identifyAnnotationBodyDeclarationType" },
188
+ { value: "isSimpleElementValueAnnotation" },
189
+ { value: "literal" },
190
+ { value: "integerLiteral" },
191
+ { value: "floatingPointLiteral" },
192
+ { value: "booleanLiteral" },
193
+ { value: "moduleName" },
194
+ { value: "packageName" },
195
+ { value: "typeName" },
196
+ { value: "expressionName" },
197
+ { value: "methodName" },
198
+ { value: "packageOrTypeName" },
199
+ { value: "ambiguousName" },
200
+ { value: "compilationUnit" },
201
+ { value: "ordinaryCompilationUnit" },
202
+ { value: "modularCompilationUnit" },
203
+ { value: "packageDeclaration" },
204
+ { value: "packageModifier" },
205
+ { value: "importDeclaration" },
206
+ { value: "typeDeclaration" },
207
+ { value: "moduleDeclaration" },
208
+ { value: "moduleDirective" },
209
+ { value: "requiresModuleDirective" },
210
+ { value: "exportsModuleDirective" },
211
+ { value: "opensModuleDirective" },
212
+ { value: "usesModuleDirective" },
213
+ { value: "providesModuleDirective" },
214
+ { value: "requiresModifier" },
215
+ { value: "isModuleCompilationUnit" },
216
+ { value: "primitiveType" },
217
+ { value: "numericType" },
218
+ { value: "integralType" },
219
+ { value: "floatingPointType" },
220
+ { value: "referenceType" },
221
+ { value: "classOrInterfaceType" },
222
+ { value: "classType" },
223
+ { value: "interfaceType" },
224
+ { value: "typeVariable" },
225
+ { value: "dims" },
226
+ { value: "typeParameter" },
227
+ { value: "typeParameterModifier" },
228
+ { value: "typeBound" },
229
+ { value: "additionalBound" },
230
+ { value: "typeArguments" },
231
+ { value: "typeArgumentList" },
232
+ { value: "typeArgument" },
233
+ { value: "wildcard" },
234
+ { value: "wildcardBounds" }
235
+ ],
236
+ description:
237
+ "Prettify from the entrypoint, allowing to use prettier on snippet."
238
+ }
239
+ };
package/src/parser.js CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  const javaParser = require("java-parser");
4
4
 
5
- function parse(text) {
6
- const cst = javaParser.parse(text);
5
+ function parse(text, parsers, opts) {
6
+ const cst = javaParser.parse(text, opts.entrypoint);
7
7
  return cst;
8
8
  }
9
9
 
package/src/printer.js CHANGED
@@ -25,7 +25,6 @@ function genericPrint(path, options, print) {
25
25
  // printed: false
26
26
  // }
27
27
  // ];
28
-
29
28
  return createPrettierDoc(node, options);
30
29
  }
31
30
 
@@ -81,7 +81,9 @@ class BlocksAndStatementPrettierVisitor {
81
81
  if (ctx.labeledStatement !== undefined) {
82
82
  const newLabelStatement = { ...ctx.labeledStatement[0] };
83
83
  const newColon = { ...ctx.labeledStatement[0].children.Colon[0] };
84
- const newStatement = { ...ctx.labeledStatement[0].children.statement[0] };
84
+ const newStatement = {
85
+ ...ctx.labeledStatement[0].children.statement[0]
86
+ };
85
87
 
86
88
  const labeledStatementLeadingComments = [];
87
89
 
@@ -16,7 +16,10 @@ const {
16
16
  } = require("./printer-utils");
17
17
  const { concat, join, group, indent } = require("./prettier-builder");
18
18
  const { printTokenWithComments } = require("./comments/format-comments");
19
- const { hasLeadingLineComments } = require("./comments/comments-utils");
19
+ const {
20
+ hasLeadingLineComments,
21
+ hasLeadingComments
22
+ } = require("./comments/comments-utils");
20
23
 
21
24
  class ClassesPrettierVisitor {
22
25
  classDeclaration(ctx) {
@@ -674,15 +677,36 @@ class ClassesPrettierVisitor {
674
677
  const enumConstantList = this.visit(ctx.enumConstantList);
675
678
  const enumBodyDeclarations = this.visit(ctx.enumBodyDeclarations);
676
679
 
680
+ const hasEnumConstants = ctx.enumConstantList !== undefined;
681
+ const hasNoClassBodyDeclarations =
682
+ ctx.enumBodyDeclarations === undefined ||
683
+ ctx.enumBodyDeclarations[0].children.classBodyDeclaration === undefined;
684
+
685
+ // edge case: https://github.com/jhipster/prettier-java/issues/383
686
+ const handleEnumBodyDeclarationsLeadingComments =
687
+ !hasNoClassBodyDeclarations &&
688
+ hasLeadingComments(ctx.enumBodyDeclarations[0])
689
+ ? hardline
690
+ : "";
691
+
677
692
  let optionalComma;
678
- if (this.prettierOptions.trailingComma !== "none") {
693
+ if (
694
+ hasEnumConstants &&
695
+ hasNoClassBodyDeclarations &&
696
+ this.prettierOptions.trailingComma !== "none"
697
+ ) {
679
698
  optionalComma = ctx.Comma ? ctx.Comma[0] : ",";
680
699
  } else {
681
700
  optionalComma = ctx.Comma ? { ...ctx.Comma[0], image: "" } : "";
682
701
  }
683
702
 
684
703
  return putIntoBraces(
685
- rejectAndConcat([enumConstantList, optionalComma, enumBodyDeclarations]),
704
+ rejectAndConcat([
705
+ enumConstantList,
706
+ optionalComma,
707
+ handleEnumBodyDeclarationsLeadingComments,
708
+ enumBodyDeclarations
709
+ ]),
686
710
  hardline,
687
711
  ctx.LCurly[0],
688
712
  ctx.RCurly[0]
@@ -2,7 +2,7 @@
2
2
 
3
3
  const _ = require("lodash");
4
4
  const { ifBreak, line, softline } = require("prettier").doc.builders;
5
- const { concat, group, indent } = require("./prettier-builder");
5
+ const { concat, group, indent, dedent } = require("./prettier-builder");
6
6
  const { printTokenWithComments } = require("./comments/format-comments");
7
7
  const {
8
8
  handleCommentsBinaryExpression
@@ -288,34 +288,68 @@ class ExpressionsPrettierVisitor {
288
288
  const primaryPrefix = this.visit(ctx.primaryPrefix, {
289
289
  shouldBreakBeforeFirstMethodInvocation: countMethodInvocation > 1
290
290
  });
291
- const primarySuffixes = this.mapVisit(ctx.primarySuffix);
292
291
 
293
292
  const suffixes = [];
294
293
 
295
294
  if (ctx.primarySuffix !== undefined) {
295
+ // edge case: https://github.com/jhipster/prettier-java/issues/381
296
+ let hasFirstInvocationArg = true;
297
+
298
+ if (
299
+ ctx.primarySuffix.length > 1 &&
300
+ ctx.primarySuffix[1].children.methodInvocationSuffix &&
301
+ Object.keys(
302
+ ctx.primarySuffix[1].children.methodInvocationSuffix[0].children
303
+ ).length === 2
304
+ ) {
305
+ hasFirstInvocationArg = false;
306
+ }
307
+
296
308
  if (
297
309
  ctx.primarySuffix[0].children.Dot !== undefined &&
298
310
  ctx.primaryPrefix[0].children.newExpression !== undefined
299
311
  ) {
300
312
  suffixes.push(softline);
301
313
  }
302
- suffixes.push(primarySuffixes[0]);
314
+ suffixes.push(
315
+ this.visit(ctx.primarySuffix[0], {
316
+ shouldDedent:
317
+ // dedent when simple method invocation
318
+ countMethodInvocation !== 1 &&
319
+ // dedent when (chain) method invocation
320
+ ctx.primaryPrefix[0] &&
321
+ ctx.primaryPrefix[0].children.fqnOrRefType &&
322
+ !(
323
+ ctx.primaryPrefix[0].children.fqnOrRefType[0].children.Dot !==
324
+ undefined
325
+ ) &&
326
+ // indent when lambdaExpression
327
+ ctx.primarySuffix[0].children.methodInvocationSuffix &&
328
+ ctx.primarySuffix[0].children.methodInvocationSuffix[0].children
329
+ .argumentList &&
330
+ ctx.primarySuffix[0].children.methodInvocationSuffix[0].children
331
+ .argumentList[0].children.expression &&
332
+ ctx.primarySuffix[0].children.methodInvocationSuffix[0].children
333
+ .argumentList[0].children.expression[0].children
334
+ .lambdaExpression === undefined
335
+ })
336
+ );
303
337
 
304
- for (let i = 1; i < primarySuffixes.length; i++) {
338
+ for (let i = 1; i < ctx.primarySuffix.length; i++) {
305
339
  if (
306
340
  ctx.primarySuffix[i].children.Dot !== undefined &&
307
341
  ctx.primarySuffix[i - 1].children.methodInvocationSuffix !== undefined
308
342
  ) {
309
343
  suffixes.push(softline);
310
344
  }
311
- suffixes.push(primarySuffixes[i]);
345
+ suffixes.push(this.visit(ctx.primarySuffix[i]));
312
346
  }
313
347
 
314
348
  if (countMethodInvocation === 1) {
315
349
  return group(
316
350
  rejectAndConcat([
317
351
  primaryPrefix,
318
- suffixes[0],
352
+ hasFirstInvocationArg ? suffixes[0] : indent(suffixes[0]),
319
353
  indent(rejectAndConcat(suffixes.slice(1)))
320
354
  ])
321
355
  );
@@ -335,7 +369,7 @@ class ExpressionsPrettierVisitor {
335
369
  return this.visitSingle(ctx, params);
336
370
  }
337
371
 
338
- primarySuffix(ctx) {
372
+ primarySuffix(ctx, params) {
339
373
  if (ctx.Dot) {
340
374
  if (ctx.This) {
341
375
  return rejectAndConcat([ctx.Dot[0], ctx.This[0]]);
@@ -352,7 +386,7 @@ class ExpressionsPrettierVisitor {
352
386
  unqualifiedClassInstanceCreationExpression
353
387
  ]);
354
388
  }
355
- return this.visitSingle(ctx);
389
+ return this.visitSingle(ctx, params);
356
390
  }
357
391
 
358
392
  fqnOrRefType(ctx, params) {
@@ -360,20 +394,40 @@ class ExpressionsPrettierVisitor {
360
394
  const fqnOrRefTypePartRest = this.mapVisit(ctx.fqnOrRefTypePartRest);
361
395
  const dims = this.visit(ctx.dims);
362
396
  const dots = ctx.Dot ? ctx.Dot : [];
397
+ const isMethodInvocation = ctx.Dot && ctx.Dot.length === 1;
363
398
 
364
399
  if (
365
400
  params !== undefined &&
366
401
  params.shouldBreakBeforeFirstMethodInvocation === true
367
402
  ) {
368
- return rejectAndConcat([
369
- indent(
370
- rejectAndJoin(concat([softline, dots[0]]), [
371
- fqnOrRefTypePartFirst,
372
- rejectAndJoinSeps(dots.slice(1), fqnOrRefTypePartRest),
403
+ // when fqnOrRefType is a method call from an object
404
+ if (isMethodInvocation) {
405
+ return rejectAndConcat([
406
+ indent(
407
+ rejectAndJoin(concat([softline, dots[0]]), [
408
+ fqnOrRefTypePartFirst,
409
+ rejectAndJoinSeps(dots.slice(1), fqnOrRefTypePartRest),
410
+ dims
411
+ ])
412
+ )
413
+ ]);
414
+ // otherwise it is a fully qualified name but we need to exclude when it is just a method call
415
+ } else if (ctx.Dot) {
416
+ return indent(
417
+ rejectAndConcat([
418
+ rejectAndJoinSeps(dots.slice(0, dots.length - 1), [
419
+ fqnOrRefTypePartFirst,
420
+ ...fqnOrRefTypePartRest.slice(0, fqnOrRefTypePartRest.length - 1)
421
+ ]),
422
+ softline,
423
+ rejectAndConcat([
424
+ dots[dots.length - 1],
425
+ fqnOrRefTypePartRest[fqnOrRefTypePartRest.length - 1]
426
+ ]),
373
427
  dims
374
428
  ])
375
- )
376
- ]);
429
+ );
430
+ }
377
431
  }
378
432
 
379
433
  return rejectAndConcat([
@@ -532,12 +586,17 @@ class ExpressionsPrettierVisitor {
532
586
  return concat([ctx.Less[0], ctx.Greater[0]]);
533
587
  }
534
588
 
535
- methodInvocationSuffix(ctx) {
589
+ methodInvocationSuffix(ctx, params) {
536
590
  if (ctx.argumentList === undefined) {
537
591
  return rejectAndConcat([ctx.LBrace[0], ctx.RBrace[0]]);
538
592
  }
539
593
 
540
594
  const argumentList = this.visit(ctx.argumentList);
595
+ if (params && params.shouldDedent) {
596
+ return dedent(
597
+ putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0])
598
+ );
599
+ }
541
600
  return putIntoBraces(argumentList, softline, ctx.LBrace[0], ctx.RBrace[0]);
542
601
  }
543
602
 
@@ -334,8 +334,7 @@ function needLineClassBodyDeclaration(declaration) {
334
334
  classMemberDeclaration.children.fieldDeclaration[0];
335
335
  if (
336
336
  fieldDeclaration.children.fieldModifier !== undefined &&
337
- fieldDeclaration.children.fieldModifier[0].children.annotation !==
338
- undefined
337
+ hasAnnotation(fieldDeclaration.children.fieldModifier)
339
338
  ) {
340
339
  return true;
341
340
  }
@@ -352,8 +351,7 @@ function needLineInterfaceMemberDeclaration(declaration) {
352
351
  const constantDeclaration = declaration.children.constantDeclaration[0];
353
352
  if (
354
353
  constantDeclaration.children.constantModifier !== undefined &&
355
- constantDeclaration.children.constantModifier[0].children.annotation !==
356
- undefined
354
+ hasAnnotation(constantDeclaration.children.constantModifier)
357
355
  ) {
358
356
  return true;
359
357
  }
@@ -364,8 +362,9 @@ function needLineInterfaceMemberDeclaration(declaration) {
364
362
  if (
365
363
  interfaceMethodDeclaration.children.interfaceMethodModifier !==
366
364
  undefined &&
367
- interfaceMethodDeclaration.children.interfaceMethodModifier[0].children
368
- .annotation !== undefined
365
+ hasNonTrailingAnnotation(
366
+ interfaceMethodDeclaration.children.interfaceMethodModifier
367
+ )
369
368
  ) {
370
369
  return true;
371
370
  }
@@ -391,6 +390,32 @@ function isInterfaceMemberASemicolon(interfaceMemberDeclaration) {
391
390
  return interfaceMemberDeclaration.children.Semicolon !== undefined;
392
391
  }
393
392
 
393
+ function hasAnnotation(modifiers) {
394
+ return modifiers.some(modifier => modifier.children.annotation !== undefined);
395
+ }
396
+
397
+ /**
398
+ * Return true if there is a method modifier that does not come after all other modifiers
399
+ * It is useful to know if sortModifiers will add an annotation before other modifiers
400
+ *
401
+ * @param methodModifiers
402
+ * @returns {boolean}
403
+ */
404
+ function hasNonTrailingAnnotation(methodModifiers) {
405
+ const firstAnnotationIndex = methodModifiers.findIndex(
406
+ modifier => modifier.children.annotation !== undefined
407
+ );
408
+ const lastNonAnnotationIndex = _.findLastIndex(
409
+ methodModifiers,
410
+ modifier => modifier.children.annotation === undefined
411
+ );
412
+
413
+ return (
414
+ firstAnnotationIndex < lastNonAnnotationIndex ||
415
+ lastNonAnnotationIndex === -1
416
+ );
417
+ }
418
+
394
419
  function getClassBodyDeclarationsSeparator(classBodyDeclarationContext) {
395
420
  return getDeclarationsSeparator(
396
421
  classBodyDeclarationContext,
@@ -625,7 +650,7 @@ function isUniqueMethodInvocation(primarySuffixes) {
625
650
 
626
651
  function printArrayList({ list, extraComma, LCurly, RCurly, trailingComma }) {
627
652
  let optionalComma;
628
- if (trailingComma !== "none") {
653
+ if (trailingComma !== "none" && list !== "") {
629
654
  optionalComma = extraComma
630
655
  ? ifBreak(extraComma[0], { ...extraComma[0], image: "" })
631
656
  : ifBreak(",", "");
package/LICENSE DELETED
@@ -1,204 +0,0 @@
1
-
2
- Copyright 2013-2020 Shahar Soel and the respective prettier-java contributors
3
-
4
- Apache License
5
- Version 2.0, January 2004
6
- http://www.apache.org/licenses/
7
-
8
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
9
-
10
- 1. Definitions.
11
-
12
- "License" shall mean the terms and conditions for use, reproduction,
13
- and distribution as defined by Sections 1 through 9 of this document.
14
-
15
- "Licensor" shall mean the copyright owner or entity authorized by
16
- the copyright owner that is granting the License.
17
-
18
- "Legal Entity" shall mean the union of the acting entity and all
19
- other entities that control, are controlled by, or are under common
20
- control with that entity. For the purposes of this definition,
21
- "control" means (i) the power, direct or indirect, to cause the
22
- direction or management of such entity, whether by contract or
23
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
24
- outstanding shares, or (iii) beneficial ownership of such entity.
25
-
26
- "You" (or "Your") shall mean an individual or Legal Entity
27
- exercising permissions granted by this License.
28
-
29
- "Source" form shall mean the preferred form for making modifications,
30
- including but not limited to software source code, documentation
31
- source, and configuration files.
32
-
33
- "Object" form shall mean any form resulting from mechanical
34
- transformation or translation of a Source form, including but
35
- not limited to compiled object code, generated documentation,
36
- and conversions to other media types.
37
-
38
- "Work" shall mean the work of authorship, whether in Source or
39
- Object form, made available under the License, as indicated by a
40
- copyright notice that is included in or attached to the work
41
- (an example is provided in the Appendix below).
42
-
43
- "Derivative Works" shall mean any work, whether in Source or Object
44
- form, that is based on (or derived from) the Work and for which the
45
- editorial revisions, annotations, elaborations, or other modifications
46
- represent, as a whole, an original work of authorship. For the purposes
47
- of this License, Derivative Works shall not include works that remain
48
- separable from, or merely link (or bind by name) to the interfaces of,
49
- the Work and Derivative Works thereof.
50
-
51
- "Contribution" shall mean any work of authorship, including
52
- the original version of the Work and any modifications or additions
53
- to that Work or Derivative Works thereof, that is intentionally
54
- submitted to Licensor for inclusion in the Work by the copyright owner
55
- or by an individual or Legal Entity authorized to submit on behalf of
56
- the copyright owner. For the purposes of this definition, "submitted"
57
- means any form of electronic, verbal, or written communication sent
58
- to the Licensor or its representatives, including but not limited to
59
- communication on electronic mailing lists, source code control systems,
60
- and issue tracking systems that are managed by, or on behalf of, the
61
- Licensor for the purpose of discussing and improving the Work, but
62
- excluding communication that is conspicuously marked or otherwise
63
- designated in writing by the copyright owner as "Not a Contribution."
64
-
65
- "Contributor" shall mean Licensor and any individual or Legal Entity
66
- on behalf of whom a Contribution has been received by Licensor and
67
- subsequently incorporated within the Work.
68
-
69
- 2. Grant of Copyright License. Subject to the terms and conditions of
70
- this License, each Contributor hereby grants to You a perpetual,
71
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
72
- copyright license to reproduce, prepare Derivative Works of,
73
- publicly display, publicly perform, sublicense, and distribute the
74
- Work and such Derivative Works in Source or Object form.
75
-
76
- 3. Grant of Patent License. Subject to the terms and conditions of
77
- this License, each Contributor hereby grants to You a perpetual,
78
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
79
- (except as stated in this section) patent license to make, have made,
80
- use, offer to sell, sell, import, and otherwise transfer the Work,
81
- where such license applies only to those patent claims licensable
82
- by such Contributor that are necessarily infringed by their
83
- Contribution(s) alone or by combination of their Contribution(s)
84
- with the Work to which such Contribution(s) was submitted. If You
85
- institute patent litigation against any entity (including a
86
- cross-claim or counterclaim in a lawsuit) alleging that the Work
87
- or a Contribution incorporated within the Work constitutes direct
88
- or contributory patent infringement, then any patent licenses
89
- granted to You under this License for that Work shall terminate
90
- as of the date such litigation is filed.
91
-
92
- 4. Redistribution. You may reproduce and distribute copies of the
93
- Work or Derivative Works thereof in any medium, with or without
94
- modifications, and in Source or Object form, provided that You
95
- meet the following conditions:
96
-
97
- (a) You must give any other recipients of the Work or
98
- Derivative Works a copy of this License; and
99
-
100
- (b) You must cause any modified files to carry prominent notices
101
- stating that You changed the files; and
102
-
103
- (c) You must retain, in the Source form of any Derivative Works
104
- that You distribute, all copyright, patent, trademark, and
105
- attribution notices from the Source form of the Work,
106
- excluding those notices that do not pertain to any part of
107
- the Derivative Works; and
108
-
109
- (d) If the Work includes a "NOTICE" text file as part of its
110
- distribution, then any Derivative Works that You distribute must
111
- include a readable copy of the attribution notices contained
112
- within such NOTICE file, excluding those notices that do not
113
- pertain to any part of the Derivative Works, in at least one
114
- of the following places: within a NOTICE text file distributed
115
- as part of the Derivative Works; within the Source form or
116
- documentation, if provided along with the Derivative Works; or,
117
- within a display generated by the Derivative Works, if and
118
- wherever such third-party notices normally appear. The contents
119
- of the NOTICE file are for informational purposes only and
120
- do not modify the License. You may add Your own attribution
121
- notices within Derivative Works that You distribute, alongside
122
- or as an addendum to the NOTICE text from the Work, provided
123
- that such additional attribution notices cannot be construed
124
- as modifying the License.
125
-
126
- You may add Your own copyright statement to Your modifications and
127
- may provide additional or different license terms and conditions
128
- for use, reproduction, or distribution of Your modifications, or
129
- for any such Derivative Works as a whole, provided Your use,
130
- reproduction, and distribution of the Work otherwise complies with
131
- the conditions stated in this License.
132
-
133
- 5. Submission of Contributions. Unless You explicitly state otherwise,
134
- any Contribution intentionally submitted for inclusion in the Work
135
- by You to the Licensor shall be under the terms and conditions of
136
- this License, without any additional terms or conditions.
137
- Notwithstanding the above, nothing herein shall supersede or modify
138
- the terms of any separate license agreement you may have executed
139
- with Licensor regarding such Contributions.
140
-
141
- 6. Trademarks. This License does not grant permission to use the trade
142
- names, trademarks, service marks, or product names of the Licensor,
143
- except as required for reasonable and customary use in describing the
144
- origin of the Work and reproducing the content of the NOTICE file.
145
-
146
- 7. Disclaimer of Warranty. Unless required by applicable law or
147
- agreed to in writing, Licensor provides the Work (and each
148
- Contributor provides its Contributions) on an "AS IS" BASIS,
149
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
150
- implied, including, without limitation, any warranties or conditions
151
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
152
- PARTICULAR PURPOSE. You are solely responsible for determining the
153
- appropriateness of using or redistributing the Work and assume any
154
- risks associated with Your exercise of permissions under this License.
155
-
156
- 8. Limitation of Liability. In no event and under no legal theory,
157
- whether in tort (including negligence), contract, or otherwise,
158
- unless required by applicable law (such as deliberate and grossly
159
- negligent acts) or agreed to in writing, shall any Contributor be
160
- liable to You for damages, including any direct, indirect, special,
161
- incidental, or consequential damages of any character arising as a
162
- result of this License or out of the use or inability to use the
163
- Work (including but not limited to damages for loss of goodwill,
164
- work stoppage, computer failure or malfunction, or any and all
165
- other commercial damages or losses), even if such Contributor
166
- has been advised of the possibility of such damages.
167
-
168
- 9. Accepting Warranty or Additional Liability. While redistributing
169
- the Work or Derivative Works thereof, You may choose to offer,
170
- and charge a fee for, acceptance of support, warranty, indemnity,
171
- or other liability obligations and/or rights consistent with this
172
- License. However, in accepting such obligations, You may act only
173
- on Your own behalf and on Your sole responsibility, not on behalf
174
- of any other Contributor, and only if You agree to indemnify,
175
- defend, and hold each Contributor harmless for any liability
176
- incurred by, or claims asserted against, such Contributor by reason
177
- of your accepting any such warranty or additional liability.
178
-
179
- END OF TERMS AND CONDITIONS
180
-
181
- APPENDIX: How to apply the Apache License to your work.
182
-
183
- To apply the Apache License to your work, attach the following
184
- boilerplate notice, with the fields enclosed by brackets "[]"
185
- replaced with your own identifying information. (Don't include
186
- the brackets!) The text should be enclosed in the appropriate
187
- comment syntax for the file format. We also recommend that a
188
- file or class name and description of purpose be included on the
189
- same "printed page" as the copyright notice for easier
190
- identification within third-party archives.
191
-
192
- Copyright [yyyy] [name of copyright owner]
193
-
194
- Licensed under the Apache License, Version 2.0 (the "License");
195
- you may not use this file except in compliance with the License.
196
- You may obtain a copy of the License at
197
-
198
- http://www.apache.org/licenses/LICENSE-2.0
199
-
200
- Unless required by applicable law or agreed to in writing, software
201
- distributed under the License is distributed on an "AS IS" BASIS,
202
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
203
- See the License for the specific language governing permissions and
204
- limitations under the License.