macisamuele / language-formatters-pre-commit-hooks

Collection of custom pre-commit hooks.
Apache License 2.0
112 stars 56 forks source link

Add support for ktfmt #195

Closed 0x26res closed 3 months ago

0x26res commented 7 months ago

At the moment the pretty-format-kotlin hook uses ktlint.

ktfmt is another popular code formatter for koltin. It would be good if there was an extra hook in this repo (eg pretty-format-ktfmt) that would use ktfmt.

I had a look at the code for pretty-format-kotlin and it looks like it should be simple to implement a similar version for ktfmt. It's just a matter of calling java with a jar and the correct arguments: https://github.com/facebook/ktfmt#from-the-command-line

If that makes sense I can have a god at putting an PR together.

PS: so far the only way I found to run ktfmt with pre-commit is to use spotless, with either gradle-spotless or maven-spotless-apply. But these hooks require either maven or gradle installed. Whereas in theory we only need java (and to download and cache the correct jar).

macisamuele commented 7 months ago

I would not have objections on supporting formatting via ktfmt.

Eventually it could be integrated via a flag on the already existing kotlin formatter to avoid the presence of 2 kotlin related for matters.

I can suport you with PR review and scoping out some of the possible CLI arguments. And yes your concern is valid, we should aim to not depend on maven or grade but only java (and I believe that it is possible)

mxr commented 3 months ago

It seems this issue was resolved by #196