prettier-plugin-java-for-cjs 2.8.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.
- package/README.md +158 -0
- package/dist/index.cjs +20803 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +594 -0
- package/dist/index.d.mts +596 -0
- package/dist/index.mjs +20803 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/prettier-plugin-java)
|
|
2
|
+
|
|
3
|
+
# prettier-plugin-java
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
Prettier is an opinionated code formatter which forces a certain coding style. It makes the code consistent through an entire project.
|
|
8
|
+
|
|
9
|
+
This plugin allows the support of Java on Prettier.
|
|
10
|
+
|
|
11
|
+
The plugin implementation is pretty straightforward as it uses [java-parser](../java-parser) (thanks to Chevrotain) visitor to traverse the **C**oncrete **S**yntax **T**ree and apply the format processing on each node (it uses Prettier API).
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
### Pre-requirements
|
|
16
|
+
|
|
17
|
+
Since the plugin is meant to be used with Prettier, you need to install it:
|
|
18
|
+
|
|
19
|
+
`npm install --save-dev --save-exact prettier`
|
|
20
|
+
|
|
21
|
+
or
|
|
22
|
+
|
|
23
|
+
`yarn add prettier --dev --exact`
|
|
24
|
+
|
|
25
|
+
### Install plugin
|
|
26
|
+
|
|
27
|
+
`npm install prettier-plugin-java --save-dev`
|
|
28
|
+
|
|
29
|
+
or
|
|
30
|
+
|
|
31
|
+
`yarn add prettier-plugin-java --dev`
|
|
32
|
+
|
|
33
|
+
### CLI
|
|
34
|
+
|
|
35
|
+
If you installed Prettier globally and want to format java code via the CLI, run the following command:
|
|
36
|
+
|
|
37
|
+
`npm install -g prettier-plugin-java`
|
|
38
|
+
|
|
39
|
+
The plugin will be automatically loaded, check [here](https://prettier.io/docs/en/plugins.html#using-plugins) for more.
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
### CLI
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
prettier --write MyJavaFile.java
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
If the plugin is not automatically loaded:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Example where the plugin is locate in node_modules
|
|
53
|
+
prettier --write MyJavaFile.java --plugin=./node_modules/prettier-plugin-java
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### API
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
const prettier = require("prettier");
|
|
60
|
+
const javaText = `
|
|
61
|
+
public class HelloWorldExample{
|
|
62
|
+
public static void main(String args[]){
|
|
63
|
+
System.out.println("Hello World !");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
`;
|
|
67
|
+
|
|
68
|
+
const formattedText = prettier.format(javaText, {
|
|
69
|
+
parser: "java",
|
|
70
|
+
tabWidth: 2
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Example of formatted code
|
|
75
|
+
|
|
76
|
+
### Input
|
|
77
|
+
|
|
78
|
+
```java
|
|
79
|
+
public class HelloWorld {
|
|
80
|
+
public static void main(String[] args) {System.out.println("Hello World!");;;;;}
|
|
81
|
+
|
|
82
|
+
@Override
|
|
83
|
+
public String toString() {
|
|
84
|
+
return "Hello World";
|
|
85
|
+
}
|
|
86
|
+
public int sum(int argument1,int argument2,int argument3,int argument4,int argument5
|
|
87
|
+
) {
|
|
88
|
+
return argument1+argument2+ argument3 +argument4 + argument5;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Output
|
|
94
|
+
|
|
95
|
+
```java
|
|
96
|
+
public class HelloWorld {
|
|
97
|
+
|
|
98
|
+
public static void main(String[] args) {
|
|
99
|
+
System.out.println("Hello World!");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@Override
|
|
103
|
+
public String toString() {
|
|
104
|
+
return "Hello World";
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public int sum(
|
|
108
|
+
int argument1,
|
|
109
|
+
int argument2,
|
|
110
|
+
int argument3,
|
|
111
|
+
int argument4,
|
|
112
|
+
int argument5
|
|
113
|
+
) {
|
|
114
|
+
return argument1 + argument2 + argument3 + argument4 + argument5;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
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/main/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
|
+
```
|