feat: Add google-java-format and palantir-java-format support#264
feat: Add google-java-format and palantir-java-format support#264fredyw wants to merge 1 commit into
Conversation
Integrates [google-java-format](https://github.com/google/google-java-format) and [palantir-java-format](https://github.com/palantir/palantir-java-format) support into the LSP proxy wrapper using a generic `Formatter trait` by intercepting document formatting requests at the LSP proxy layer. Key architecture and features: 1. Introduces generic `Formatter` to allow future formatting backends to be plugged in easily. 2. Automatically downloads `google-java-format` binaries from GitHub Releases and `palantir-java-format` binaries from Maven Central. 3. Intercepts `textDocument/formatting` requests, retrieves the document from the cache, spawns the formatter command, and responds with the replaced document changes. 4. Add supports for runtime toggling and hot-swapping formatters dynamically by listening to configuration notifications.
|
Thanks for the PR but is this really needed? What are the benefits of including these two specific formatters? |
|
The XML option is basically using Eclipse formatter that tries to match Google Java Style or Palantir Java Style (fork of Google Java Format). It's an approximation and never 100% the same. Ideally when we code and when we run it in CI (using spotless for instance), we want the formatting to be 100% compatible to reduce friction for developers. The option to use the Eclipse formatter still exists for those that prefer that. It's just an additional feature that I thought might benefit others. |
|
I see, I assumed that the end result of those formatters was the xml then consumed by JDTLS. Rather than integrating in the proxy the two formatters we could follow a different approach, similar to what Zed is doing with Ruff, for example. "Python": {
"code_actions_on_format": {
"source.organizeImports.ruff": true,
},
"formatter": {
"language_server": {
"name": "ruff",
},
},
"debuggers": ["Debugpy"],
"language_servers": ["basedpyright", "ruff", "!ty", "!pyrefly", "!pyright", "!pylsp", "..."],
}We vend from the extension the two LSP implementations wrapping the formatters and then the user, in order to enable them, uses the Java language settings, rather than the one for the extension, setting a different language server whose job is to format the code. I see some clear benefits following this path:
I'm happy to further discuss about this. |
|
I think for this use-case, having With that though, we would currently lose the ability to auto-download these if they are needed, see zed-industries/zed#31904. I am happy to discuss (intermediate) solutions for this, but at least that would be the "best" way for supporting this I think. |
|
Thanks for the feedback. I agree that using I'm completely open to changing it. What do you think is the best intermediate path forward? |
|
We can update the README with instructions for installing both formatters and adding them to the PATH. Currently only Google’s formatter is available via Homebrew; Palantir’s is not. If both were available from Homebrew or another package manager, installing via package manager would be the simplest solution. Otherwise we can consider As this PR already shows, the extension can download the formatters for users.
Downside: the external formatter must be configured to point to the exact location where the extension downloaded the executable, because it won’t be on the PATH as a normal install would. That’s not ideal for users. Option 2: Use an LSP proxy (most user-friendly) Provide a single place to configure formatting behavior. For now, the simplest path is to document the manual installation (delegate the download to the user). In the future, if/when it’s supported, add an automatic-download option in the extension. |
Integrates google-java-format and palantir-java-format support into the LSP proxy wrapper using a generic
Formatter traitby intercepting document formatting requests at the LSP proxy layer.Key architecture and features:
Formatterto allow future formatting backends to be plugged in easily.google-java-formatbinaries from GitHub Releases andpalantir-java-formatbinaries from Maven Central.textDocument/formattingrequests, retrieves the document from the cache, spawns the formatter command, and responds with the replaced document changes.