A dprint plugin for formatting Java code, inspired by palantir-java-format.
Built as a Rust WASM plugin using tree-sitter-java for parsing.
Add the plugin to your dprint.json:
{
"plugins": [
"https://github.com/speakeasy-api/dprint-plugin-java/releases/latest/download/dprint_plugin_java.wasm"
],
"java": {}
}| Option | Type | Default | Description |
|---|---|---|---|
lineWidth |
number | 120 |
Maximum line width |
indentWidth |
number | 4 |
Spaces per indent level |
useTabs |
boolean | false |
Use tabs instead of spaces |
newLineKind |
"lf" | "crlf" | "system" |
"lf" |
Line ending style |
formatJavadoc |
boolean | false |
Format Javadoc comments |
methodChainThreshold |
number | 80 |
Column threshold for breaking method chains |
inlineLambdas |
boolean | true |
Keep short lambdas on one line |
Example configuration:
{
"java": {
"lineWidth": 100,
"indentWidth": 2
}
}- Declarations: packages, imports, classes, interfaces, enums, records, methods, constructors, fields
- Statements: blocks, if/else, for, enhanced for, while, do-while, switch, try/catch/finally, try-with-resources, return, throw, break, continue, assert, synchronized, labeled statements
- Expressions: binary, unary, method invocation, field access, lambda, ternary, object creation, cast, instanceof, array access, method reference, parenthesized
- Rust (stable toolchain)
- wasi-sdk (for WASM builds, set
WASI_SDK_PATH)
cargo testexport CC_wasm32_unknown_unknown="$WASI_SDK_PATH/bin/clang"
cargo build --release --target wasm32-unknown-unknown --features wasmThe WASM binary will be at target/wasm32-unknown-unknown/release/dprint_plugin_java.wasm.
cd examples
dprint check # verify formatting
dprint fmt # apply formattingApache-2.0