palantir-java-format
palantir-java-format copied to clipboard
[QUESTION] Best practise of how to use palantir-java-format with Maven and VS Code
I can't seem to find a forum/channel for discussions/questions, so my aplogies if this is not the right place.
Our team is looking into using Palantir as our Code Convention.
We need it to work with Maven from command line (format) and in our pipelines (vaildate). In addition, we would like for it to integrate with VS Code which supports (only?) Eclipse Formatter to my knowledge. Is there some sort of integration using spotless (we don̈́t use gradle)?
We use other programming languages as well and would like to have a similar solution. Wondering if it would be possible to provide an EditorConfig file for Palantir?
There is a non-official EditorConfig for Google Java Code Style that could be used a starting point.
I understand that you want Palantir to be fixed. However, I think that it is important - in terms of adoption, that Palantir is available officially for different tools, e.g.:
- Build tools: Maven, Gradle
- IDEs: IntelliJ, VS Code
- Code Convention/Style formats: Eclipse Formatter, EditorConfig, Checkstyle, Spotless
Finally, there is not to freightening list of issues currently but are any of these issues something that would block us from using Palantir with satisfaction?
Hey!
We need it to work with Maven from command line (format) and in our pipelines (vaildate).
For using the palantir-java-formatter using maven (from command line or in CI), the spotless integration should work. See the docs in the readme: https://github.com/palantir/palantir-java-format/tree/4b15f6ab93ba063ac334c270bb71f382b28de9d4#maven-integration-via-spotless
In addition, we would like for it to integrate with VS Code
Unfortunately, there is no VSCode support at the moment and given how infrequent VSCode is used for Java development, I don't think we have strong motivation to invest in this. However, I could imagine that setting up a "on-save-action" that runs the maven formatter command could be a workaround?
Wondering if it would be possible to provide an EditorConfig file for Palantir?
Unfortunately, I don't think you'll be able to setup an editor config that is fully compliant with the formatter. In many cases, the formatter formats code using non-trivial rules based on the overall context the code block is in. I can't see how you could recreate these rules in a static editor config.
Thank you for the quick reply!
I have to ask what you base this on "given how infrequent VSCode is used for Java development"? There are currently 17 millions download of the Java Extension Pack for VS Code and it's gaining popularity (with the latest release just a few days ago).
My opinion is that VS Code for Java (blog) with its gaining popularity simply just can't be ignored. Well, it can but not for something like Palantir where I presume the goal is general community wide adoptation or is for Palantir only?
A save-action is truly a workaround but I doubt it I'll be satisfactory for our team. Personally, I want to be able to the Format Document
functionality and ideally also get linting.
Regarding the static editor config, you mean that properties available for EditorConfig aren't enough? They seem to work for Google Style with Palantir is based on, so are there any significant differences between the two in how the decide on the current formatting (e.g. context)?
I fully understand that this is an open source project and there are no guarantees. But, from what I've seen with Palantir it would have been nice if gained popularity and for that to happen I strongly believe (which I wrote initially) that Palantir must be available with different tools etc. One can't expect all organizations and team to change to tooling that Palantir currently provides (i.e. Spotless and IntellJ integration). It's sort of the same discussion that was in https://github.com/palantir/palantir-java-format/issues/829.
That said, if the rules where available then we could see if implementing them in EditorConfig format or Eclipse Formatter would work. Are the rules available?
Do you know if it would be possible to use the Google Format Eclipse Plugin with Palantir to get an Eclipse Formatter file? We could then use that natively with VS Code and Formatter Maven Plugin. It a workaround that I would prefer because it's closer tooling-wise to our VS Code developers.
I have to ask what you base this on "given how infrequent VSCode is used for Java development"? There are currently 17 millions download of the Java Extension Pack for VS Code and it's gaining popularity (with the latest release just a few days ago).
Sorry, I was imprecise here. This was mostly aimed at our own usage. I fully agree with you that it would be great to have good formatting support in VSCode! However, Palantir has pretty much standardized on using Intellij for all Java development. Given that, we don't have a plan for investing developer time into building a VSCode integration anytime soon.
With that being said, the Jars for running the core formatter without the intellij plugin or gradle/maven are all published and I would be happy to see someone building a VSCode extension around it! :)
That said, if the rules where available then we could see if implementing them in EditorConfig format or Eclipse Formatter would work. Are the rules available?
Unfortunately, such rules do not exist. You can implement some simple rules in EditorConfig, such as "no line should be longer than 120 characters" or "at maximum one empty line between methods". But for the more complex lay-outing done by the formatter (i.e. how to indent the nested lambdas from the README examples), the formatter runs through quite complex logic to figure out the best layout for the code that I don't think you can realistically represent in EditorConfig or something like an Eclipse Formatter file. I hope that makes sense!