What is prettier-plugin-java?
The prettier-plugin-java npm package is a plugin for Prettier that formats Java code. It ensures that Java code adheres to a consistent style, making it easier to read and maintain.
What are prettier-plugin-java's main functionalities?
Code Formatting
This feature automatically formats Java code to follow a consistent style. For example, it ensures proper indentation, spacing, and line breaks.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Consistent Style
This feature enforces a consistent coding style across the entire codebase, making it easier to read and maintain. It handles things like method spacing, bracket placement, and more.
public class Example {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
Integration with Prettier
This feature allows the plugin to be easily integrated with Prettier, enabling seamless formatting of Java code alongside other languages supported by Prettier.
{
"prettier": {
"plugins": ["prettier-plugin-java"]
}
}
Other packages similar to prettier-plugin-java
google-java-format
google-java-format is a program that reformats Java source code to comply with Google Java Style. It is similar to prettier-plugin-java in that it enforces a consistent style, but it specifically adheres to Google's style guide.
spotless
Spotless is a general-purpose code formatter that supports multiple languages, including Java. It can be configured to use different formatters, such as google-java-format or prettier-plugin-java, providing flexibility in formatting rules.
prettier-plugin-java
Prettier is an opinionated code formatter which forces a certain coding style. It makes the code consistent through an entire project.
This plugin allows the support of Java on Prettier.
The plugin implementation is pretty straightforward as it uses java-parser (thanks to Chevrotain) visitor to traverse the Concrete Syntax Tree and apply the format processing on each node (it uses Prettier API).
Installation
Pre-requirements
Since the plugin is meant to be used with Prettier, you need to install it:
npm install --save-dev --save-exact prettier
or
yarn add prettier --dev --exact
Install plugin
npm install prettier-plugin-java --save-dev
or
yarn add prettier-plugin-java --dev
CLI
If you installed Prettier globally and want to format java code via the CLI, run the following command:
npm install -g prettier-plugin-java
The plugin will be automatically loaded, check here for more.
Usage
CLI
prettier --write MyJavaFile.java
If the plugin is not automatically loaded:
prettier --write MyJavaFile.java --plugin=./node_modules/prettier-plugin-java
API
const prettier = require("prettier");
const javaText = `
public class HelloWorldExample{
public static void main(String args[]){
System.out.println("Hello World !");
}
}
`;
const formattedText = prettier.format(javaText, {
parser: "java",
tabWidth: 2
});
Example of formatted code
Input
public class HelloWorld {
public static void main(String[] args) {System.out.println("Hello World!");;;;;}
@Override
public String toString() {
return "Hello World";
}
public int sum(int argument1,int argument2,int argument3,int argument4,int argument5
) {
return argument1+argument2+ argument3 +argument4 + argument5;
}
}
Output
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
@Override
public String toString() {
return "Hello World";
}
public int sum(
int argument1,
int argument2,
int argument3,
int argument4,
int argument5
) {
return argument1 + argument2 + argument3 + argument4 + argument5;
}
}