Skip to content

Commit 44f8a6f

Browse files
authored
docs: Document google-java-format and palantir-java-format as external formatters (#265)
1 parent 176f356 commit 44f8a6f

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,122 @@ If your project uses custom or internal Maven repositories, you should point JDT
246246

247247
Without this, JDTLS's embedded Maven will only resolve artifacts from Maven Central, which will cause unresolved dependency errors for projects using internal or private repositories.
248248

249+
## External Formatters
250+
251+
If you prefer to use an external formatting tool like `google-java-format` or `palantir-java-format` instead of the built-in Eclipse formatter, you can configure Zed to run these tools on save or format commands.
252+
253+
### Configuring google-java-format
254+
255+
To use `google-java-format` as your external formatter:
256+
257+
1. **Installation**: Download the pre-built native binary for your platform from the [GitHub releases page](https://github.com/google/google-java-format/releases). Place it in a directory that is in your system's `PATH` (e.g., `/usr/local/bin` or `~/.local/bin`), rename it to `google-java-format` (or `google-java-format.exe` on Windows), and ensure it is marked as executable.
258+
- Alternatively, on macOS, you can install it via Homebrew: `brew install google-java-format`
259+
2. Add the configuration to your Zed `settings.json`:
260+
261+
```json
262+
"languages": {
263+
"Java": {
264+
"formatter": {
265+
"external": {
266+
"command": "google-java-format",
267+
"arguments": ["-"]
268+
}
269+
}
270+
}
271+
}
272+
```
273+
274+
If you prefer to format using the AOSP style (4-space indentation), you can pass the `--aosp` flag:
275+
276+
```json
277+
"languages": {
278+
"Java": {
279+
"formatter": {
280+
"external": {
281+
"command": "google-java-format",
282+
"arguments": ["--aosp", "-"]
283+
}
284+
}
285+
}
286+
}
287+
```
288+
289+
If you installed it to a custom location not on your `PATH`, specify the absolute path for the `command`:
290+
291+
```json
292+
"languages": {
293+
"Java": {
294+
"formatter": {
295+
"external": {
296+
"command": "/path/to/google-java-format",
297+
"arguments": ["-"]
298+
}
299+
}
300+
}
301+
}
302+
```
303+
304+
### Configuring palantir-java-format
305+
306+
To use `palantir-java-format` as your external formatter:
307+
308+
1. **Installation**:
309+
- **macOS / Linux**: Download the native binary for your architecture from the [Maven Central repository page](https://repo1.maven.org/maven2/com/palantir/javaformat/palantir-java-format-native/). Rename it to `palantir-java-format`, mark it as executable, and place it in a directory in your system's `PATH`.
310+
- **Windows**: Palantir does not distribute official pre-built native binaries for Windows. You must compile the native executable yourself from [palantir-java-format repository](https://github.com/palantir/palantir-java-format).
311+
2. Add the configuration to your Zed `settings.json` (note that the `--palantir` option is required):
312+
313+
```json
314+
"languages": {
315+
"Java": {
316+
"formatter": {
317+
"external": {
318+
"command": "palantir-java-format",
319+
"arguments": ["--palantir", "-"]
320+
}
321+
}
322+
}
323+
}
324+
```
325+
326+
### Running Formatters via JAR File (Alternative)
327+
328+
If a native executable is not available for your platform (or you prefer not to compile/install one), you can run the formatters using their `.jar` files (e.g. the all-deps/shadow JARs) by configuring Zed to launch `java` directly:
329+
330+
#### Using google-java-format JAR
331+
332+
```json
333+
"languages": {
334+
"Java": {
335+
"formatter": {
336+
"external": {
337+
"command": "java",
338+
"arguments": ["-jar", "/path/to/google-java-format-all-deps.jar", "-"]
339+
}
340+
}
341+
}
342+
}
343+
```
344+
345+
#### Using palantir-java-format JAR
346+
347+
```json
348+
"languages": {
349+
"Java": {
350+
"formatter": {
351+
"external": {
352+
"command": "java",
353+
"arguments": [
354+
"-jar",
355+
"/path/to/palantir-java-format-all-deps.jar",
356+
"--palantir",
357+
"-"
358+
]
359+
}
360+
}
361+
}
362+
}
363+
```
364+
249365
## Advanced Configuration/JDTLS initialization Options
250366

251367
JDTLS provides many configuration options that can be passed via the `initialize` LSP-request. The extension will pass the JSON-object from `lsp.jdtls.initialization_options` in your settings on to JDTLS. Please refer to the [JDTLS Configuration Wiki Page](https://github.com/eclipse-jdtls/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request) for the available options and values.

0 commit comments

Comments
 (0)