prettier-plugin-java 2.6.3 → 2.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base-cst-printer.js +55 -0
- package/dist/cst-printer.js +29 -0
- package/dist/index.js +66 -0
- package/dist/options.js +256 -0
- package/dist/parser.js +4 -0
- package/dist/printer.js +6 -0
- package/dist/printers/arrays.js +25 -0
- package/dist/printers/blocks-and-statements.js +432 -0
- package/dist/printers/classes.js +709 -0
- package/dist/printers/comments/comments-utils.js +21 -0
- package/dist/printers/comments/format-comments.js +171 -0
- package/dist/printers/comments/handle-comments.js +102 -0
- package/dist/printers/expressions.js +614 -0
- package/dist/printers/interfaces.js +222 -0
- package/dist/printers/lexical-structure.js +31 -0
- package/dist/printers/names.js +29 -0
- package/dist/printers/packages-and-modules.js +171 -0
- package/dist/printers/prettier-builder.js +45 -0
- package/dist/printers/printer-utils.js +598 -0
- package/dist/printers/types-values-and-variables.js +153 -0
- package/dist/types/utils.js +20 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/isEmptyDoc.js +4 -0
- package/dist/utils/printArgumentListWithBraces.js +37 -0
- package/package.json +2 -2
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import forEach from "lodash/forEach.js";
|
|
2
|
+
import { builders } from "prettier/doc";
|
|
3
|
+
import { concat, group, indent, join } from "./prettier-builder.js";
|
|
4
|
+
import { printTokenWithComments } from "./comments/format-comments.js";
|
|
5
|
+
import { putIntoBraces, rejectAndConcat, rejectAndJoin, rejectAndJoinSeps, sortClassTypeChildren } from "./printer-utils.js";
|
|
6
|
+
import { BaseCstPrettierPrinter } from "../base-cst-printer.js";
|
|
7
|
+
import { isAnnotationCstNode, isCstNode, isTypeArgumentsCstNode } from "../types/utils.js";
|
|
8
|
+
const { line, softline } = builders;
|
|
9
|
+
export class TypesValuesAndVariablesPrettierVisitor extends BaseCstPrettierPrinter {
|
|
10
|
+
primitiveType(ctx) {
|
|
11
|
+
const annotations = this.mapVisit(ctx.annotation);
|
|
12
|
+
const type = ctx.numericType
|
|
13
|
+
? this.visit(ctx.numericType)
|
|
14
|
+
: this.getSingle(ctx);
|
|
15
|
+
return rejectAndJoin(" ", [join(" ", annotations), type]);
|
|
16
|
+
}
|
|
17
|
+
numericType(ctx) {
|
|
18
|
+
return this.visitSingle(ctx);
|
|
19
|
+
}
|
|
20
|
+
integralType(ctx) {
|
|
21
|
+
return printTokenWithComments(this.getSingle(ctx));
|
|
22
|
+
}
|
|
23
|
+
floatingPointType(ctx) {
|
|
24
|
+
return printTokenWithComments(this.getSingle(ctx));
|
|
25
|
+
}
|
|
26
|
+
referenceType(ctx) {
|
|
27
|
+
const annotations = this.mapVisit(ctx.annotation);
|
|
28
|
+
const type = ctx.primitiveType
|
|
29
|
+
? this.visit(ctx.primitiveType)
|
|
30
|
+
: this.visit(ctx.classOrInterfaceType);
|
|
31
|
+
const dims = this.visit(ctx.dims);
|
|
32
|
+
return rejectAndJoin(" ", [join(" ", annotations), concat([type, dims])]);
|
|
33
|
+
}
|
|
34
|
+
classOrInterfaceType(ctx) {
|
|
35
|
+
return this.visitSingle(ctx);
|
|
36
|
+
}
|
|
37
|
+
classType(ctx) {
|
|
38
|
+
const tokens = sortClassTypeChildren(ctx.annotation, ctx.typeArguments, ctx.Identifier);
|
|
39
|
+
const segments = [];
|
|
40
|
+
let currentSegment = [];
|
|
41
|
+
forEach(tokens, (token, i) => {
|
|
42
|
+
if (isTypeArgumentsCstNode(token)) {
|
|
43
|
+
currentSegment.push(this.visit([token]));
|
|
44
|
+
segments.push(rejectAndConcat(currentSegment));
|
|
45
|
+
currentSegment = [];
|
|
46
|
+
}
|
|
47
|
+
else if (isAnnotationCstNode(token)) {
|
|
48
|
+
currentSegment.push(this.visit([token]), " ");
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
currentSegment.push(token);
|
|
52
|
+
if ((i + 1 < tokens.length && !isTypeArgumentsCstNode(tokens[i + 1])) ||
|
|
53
|
+
i + 1 === tokens.length) {
|
|
54
|
+
segments.push(rejectAndConcat(currentSegment));
|
|
55
|
+
currentSegment = [];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return rejectAndJoinSeps(ctx.Dot, segments);
|
|
60
|
+
}
|
|
61
|
+
interfaceType(ctx) {
|
|
62
|
+
return this.visitSingle(ctx);
|
|
63
|
+
}
|
|
64
|
+
typeVariable(ctx) {
|
|
65
|
+
const annotations = this.mapVisit(ctx.annotation);
|
|
66
|
+
const identifier = this.getSingle(ctx);
|
|
67
|
+
return rejectAndJoin(" ", [join(" ", annotations), identifier]);
|
|
68
|
+
}
|
|
69
|
+
dims(ctx) {
|
|
70
|
+
let tokens = [...ctx.LSquare];
|
|
71
|
+
if (ctx.annotation) {
|
|
72
|
+
tokens = [...tokens, ...ctx.annotation];
|
|
73
|
+
}
|
|
74
|
+
tokens = tokens.sort((a, b) => {
|
|
75
|
+
const startOffset1 = isCstNode(a)
|
|
76
|
+
? a.children.At[0].startOffset
|
|
77
|
+
: a.startOffset;
|
|
78
|
+
const startOffset2 = isCstNode(b)
|
|
79
|
+
? b.children.At[0].startOffset
|
|
80
|
+
: b.startOffset;
|
|
81
|
+
return startOffset1 - startOffset2;
|
|
82
|
+
});
|
|
83
|
+
const segments = [];
|
|
84
|
+
let currentSegment = [];
|
|
85
|
+
forEach(tokens, token => {
|
|
86
|
+
if (isCstNode(token)) {
|
|
87
|
+
currentSegment.push(this.visit([token]));
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
segments.push(rejectAndConcat([
|
|
91
|
+
rejectAndJoin(" ", currentSegment),
|
|
92
|
+
concat([ctx.LSquare[0], ctx.RSquare[0]])
|
|
93
|
+
]));
|
|
94
|
+
currentSegment = [];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
return rejectAndConcat(segments);
|
|
98
|
+
}
|
|
99
|
+
typeParameter(ctx) {
|
|
100
|
+
const typeParameterModifiers = this.mapVisit(ctx.typeParameterModifier);
|
|
101
|
+
const typeIdentifier = this.visit(ctx.typeIdentifier);
|
|
102
|
+
const typeBound = this.visit(ctx.typeBound);
|
|
103
|
+
return rejectAndJoin(" ", [
|
|
104
|
+
join(" ", typeParameterModifiers),
|
|
105
|
+
typeIdentifier,
|
|
106
|
+
typeBound
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
typeParameterModifier(ctx) {
|
|
110
|
+
return this.visitSingle(ctx);
|
|
111
|
+
}
|
|
112
|
+
typeBound(ctx) {
|
|
113
|
+
const classOrInterfaceType = this.visit(ctx.classOrInterfaceType);
|
|
114
|
+
const additionalBound = this.mapVisit(ctx.additionalBound);
|
|
115
|
+
return concat([
|
|
116
|
+
rejectAndJoin(" ", [ctx.Extends[0], classOrInterfaceType]),
|
|
117
|
+
indent(group(concat([
|
|
118
|
+
additionalBound.length ? line : "",
|
|
119
|
+
rejectAndJoin(line, additionalBound)
|
|
120
|
+
])))
|
|
121
|
+
]);
|
|
122
|
+
}
|
|
123
|
+
additionalBound(ctx) {
|
|
124
|
+
const interfaceType = this.visit(ctx.interfaceType);
|
|
125
|
+
return join(" ", [ctx.And[0], interfaceType]);
|
|
126
|
+
}
|
|
127
|
+
typeArguments(ctx) {
|
|
128
|
+
const typeArgumentList = this.visit(ctx.typeArgumentList);
|
|
129
|
+
return putIntoBraces(typeArgumentList, softline, ctx.Less[0], ctx.Greater[0]);
|
|
130
|
+
}
|
|
131
|
+
typeArgumentList(ctx) {
|
|
132
|
+
const typeArguments = this.mapVisit(ctx.typeArgument);
|
|
133
|
+
const commas = ctx.Comma ? ctx.Comma.map(elt => concat([elt, line])) : [];
|
|
134
|
+
return rejectAndJoinSeps(commas, typeArguments);
|
|
135
|
+
}
|
|
136
|
+
typeArgument(ctx) {
|
|
137
|
+
return this.visitSingle(ctx);
|
|
138
|
+
}
|
|
139
|
+
wildcard(ctx) {
|
|
140
|
+
const annotations = this.mapVisit(ctx.annotation);
|
|
141
|
+
const wildcardBounds = this.visit(ctx.wildcardBounds);
|
|
142
|
+
return rejectAndJoin(" ", [
|
|
143
|
+
join(" ", annotations),
|
|
144
|
+
ctx.QuestionMark[0],
|
|
145
|
+
wildcardBounds
|
|
146
|
+
]);
|
|
147
|
+
}
|
|
148
|
+
wildcardBounds(ctx) {
|
|
149
|
+
const keyWord = ctx.Extends ? ctx.Extends[0] : ctx.Super[0];
|
|
150
|
+
const referenceType = this.visit(ctx.referenceType);
|
|
151
|
+
return join(" ", [keyWord, referenceType]);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function isCstNode(tokenOrNode) {
|
|
2
|
+
return !isIToken(tokenOrNode);
|
|
3
|
+
}
|
|
4
|
+
export function isIToken(tokenOrNode) {
|
|
5
|
+
return (tokenOrNode.tokenType !== undefined &&
|
|
6
|
+
tokenOrNode.image !== undefined);
|
|
7
|
+
}
|
|
8
|
+
export function isCstElementOrUndefinedIToken(tokenOrNode) {
|
|
9
|
+
return tokenOrNode !== undefined && isIToken(tokenOrNode);
|
|
10
|
+
}
|
|
11
|
+
export const isTypeArgumentsCstNode = (cstElement) => {
|
|
12
|
+
return cstElement.name === "typeArguments";
|
|
13
|
+
};
|
|
14
|
+
export const isAnnotationCstNode = (cstElement) => {
|
|
15
|
+
return cstElement.name === "annotation";
|
|
16
|
+
};
|
|
17
|
+
export const isOrdinaryCompilationUnitCtx = (ctx) => {
|
|
18
|
+
return (ctx.ordinaryCompilationUnit !==
|
|
19
|
+
undefined);
|
|
20
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { builders } from "prettier/doc";
|
|
2
|
+
import { handleCommentsParameters } from "../printers/comments/handle-comments.js";
|
|
3
|
+
import { indent } from "../printers/prettier-builder.js";
|
|
4
|
+
import { rejectAndConcat } from "../printers/printer-utils.js";
|
|
5
|
+
const { lineSuffixBoundary, softline } = builders;
|
|
6
|
+
export default function printArgumentListWithBraces(argumentListNodes, rBrace, lBrace) {
|
|
7
|
+
var _a, _b, _c;
|
|
8
|
+
const argumentListNode = argumentListNodes === null || argumentListNodes === void 0 ? void 0 : argumentListNodes[0];
|
|
9
|
+
const expressions = (_a = argumentListNode === null || argumentListNode === void 0 ? void 0 : argumentListNode.children.expression) !== null && _a !== void 0 ? _a : [];
|
|
10
|
+
if (argumentListNode) {
|
|
11
|
+
const { leadingComments, trailingComments } = argumentListNode;
|
|
12
|
+
delete argumentListNode.leadingComments;
|
|
13
|
+
delete argumentListNode.trailingComments;
|
|
14
|
+
if (leadingComments) {
|
|
15
|
+
const firstExpression = expressions[0];
|
|
16
|
+
firstExpression.leadingComments = [
|
|
17
|
+
...leadingComments,
|
|
18
|
+
...((_b = firstExpression.leadingComments) !== null && _b !== void 0 ? _b : [])
|
|
19
|
+
];
|
|
20
|
+
}
|
|
21
|
+
if (trailingComments) {
|
|
22
|
+
const lastExpression = expressions.at(-1);
|
|
23
|
+
lastExpression.trailingComments = [
|
|
24
|
+
...((_c = lastExpression.trailingComments) !== null && _c !== void 0 ? _c : []),
|
|
25
|
+
...trailingComments
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
handleCommentsParameters(lBrace, expressions, rBrace);
|
|
30
|
+
const argumentList = this.visit(argumentListNodes);
|
|
31
|
+
const contents = argumentList
|
|
32
|
+
? [argumentList]
|
|
33
|
+
: lBrace.trailingComments
|
|
34
|
+
? [softline, lineSuffixBoundary]
|
|
35
|
+
: [];
|
|
36
|
+
return rejectAndConcat([indent(lBrace), ...contents, rBrace]);
|
|
37
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-java",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.5",
|
|
4
4
|
"description": "Prettier Java Plugin",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./dist/index.js",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"ts-node": "10.9.1",
|
|
39
39
|
"typescript": "4.9.3"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "1c4691a0b302c4cceed4b30d8266d3235fca2de8"
|
|
42
42
|
}
|