In Clojure, there are currently two options:
cljfmt has a light touch. AFAIK it mostly fixes indentation and normalizes newlines. But it doesn’t do more significant changes, such as breaking a line in two or merging two non-empty lines.
- The newer zprint reformats more aggressively. Essentially it disregards all the current formatting and reformats it in a standard way. This is a more powerful concept, as you can (in principle) write an entire file in a single line and then let zprint worry about formatting.
Each approach has its advantages. Personally I think zprint is good enough to enable for the entire codebase, but it’s a harder sell in a team setting than cljfmt.
I think zprint is a wonderful piece of software with plenty of customization options.
To integrate with tooling, we have a few complementary options:
- You can set up CI to reject commits that don’t pass the “fix” mode of cljfmt or zprint. Developers then need to manually rerun cljfmt fix, commit and push.
- You can set up your editor to automatically reformat the file when you save it. (In emacs writing an Emacs modes that does this would be trivial.)
- You can instruct developers to set up a pre-push hook (
.git/hooks/pre-push) that auto-fixes the formatting and creates a formatting commit. (A pre-commit hook would be an option as well.)
Whether options 2 and 3 are going to be pleasant to use depends on the speed of the operation. This again depends on both the startup time and the time to reformat files. For example, if a pre-push commit adds >2-3 seconds on each push, people will be less likely to use it.
The author of
zprint has spent a considerable amount of time exploring options of making the tool start up fast. I haven’t spent enough time to say if a graalvm-compiled version of zprint is fast enough. For instance, I haven’t found a pre-compiled binary for MacOS yet. But this would be a good option to explore.