Closed joulev closed 1 year ago
Prof Akshay covered it during the lecture when he went through how to set up the auto-formatter for the switch case. It is under Settings --> Code Style --> Java. Following which, your group mate can go to keymap to edit the keys that is required to activate the formatter. The default command that he uses is Command + Option + L (same for me). Do ask him to review it.
As for on save/commit... Maybe you might want to set up a pre-push hook that runs the CI ./gradlew check
locally upon every git push. It will definitely slow down development, but it checks whether the coding style is followed in every file and ensures that all the test passes upon every push.
(Edit: The test will fail when the style is not followed. Ask the person to go and fix the style manually with Command Option L)
Frankly speaking, the Auto Formatter is the only reason why I am using IntelliJ instead of VSCode.
There is actually a autoformat code on save (Ctrl-S) function on Intellij:
File > Settings > Tools > Actions on Save > Reformat code checkbox
Here's how the screen looks like:
So, on Ctrl-S the code will automatically get formatted.
I think this is what you're looking for!
Reference from intellij themselves
@woowenjun99 A commit hook is always possible but it would be an overkill.
@nichyjt Thanks, I think this is it. I'll ask my teammates to set it up.
I use VSCode, and have been able to configure auto-format on save in my editor, so I never have to worry about formatting – Ctrl+S and my file is nicely formatted according to the style guide. In fact it's hard to go against the style guide since the formatter is run on save.
However, all other members of my team are using the recommended IDE – IntelliJ, and from my observation in their PRs, they are apparently formatting the Java files manually, by hand. That's troublesome and, needless to say, prone to human errors (my editor has been able to detect several formatting issues, from spacing to max line width, just by re-saving the files alone).
But IntelliJ is well-known to be a powerful IDE, there's no way it doesn't have an auto-formatter built-in, right? I want to help my teammates to set up their environments so that formatting is never an issue again, but I don't have IntelliJ. So here comes the question:
How to set up an auto-formatter in IntelliJ, that can automatically format the Java file on save/commit according to the style guide?
For reference, this is how I set up the auto-formatter in VSCode.
Install the Java extension pack. Add the following files to the root of your project (you can add `.vscode` to `.gitignore` if you want): **`.vscode/settings.json`** ```json { "editor.formatOnSave": true, "java.format.settings.url": ".vscode/java-formatter.xml", } ``` **`.vscode/java-formatter.xml`** (don't ask me, I pasted this from Google) ```xml