Hi everyone, I was wondering if there is an easy w...
# ksp
f
Hi everyone, I was wondering if there is an easy way to format the generated code. Any suggestion on this?
j
Do you use KotlinPoet?
f
no
j
then you should ๐Ÿ˜„
f
I have used it in some projects, but I do not particularly like the API. It seems awkward to use to me. Also, the code it generates is not valid for ktlint, so I was hoping there was some alternatives, equivalent to call "Reformat file" programmatically
j
is it that important to have generated code compliant with your linter?
after all, you're not linting the generated bytecode ๐Ÿ˜›
f
Not particularly, no, you are right. It would be just a nice to have as I look at this generated code and the fact it has a different indentation from the rest of the code makes it slightly more difficult to read. However the problem with the awkward API remain, so I would be happy if there was an alternative for formatting then rewriting the code generation logic to use KotlinPoet
j
I'm not sure there is really any equivalent to KotlinPoet. Also, despite its API you don't like, there are a lot of subtilities here to help you. For example, if you use a class somewhere in the generated code, it will automatically add an import at the top of the file. Can't you adapt the API to your liking by simply creating some extension functions?
f
I will have another look at it, thank you for the suggestion Johann
j
the fact it has a different indentation from the rest of the code makes it slightly more difficult to read
are you reading the code that often? Maybe when you're creating the code generator yes, but you still can reformat the code manually with Intellij's formatter each time you need to read this code?
f
Well, I am in the fase of writing the generator, so maybe I am more sensible to this ๐Ÿ˜„
j
probably ^^
But I will agree with you: KotlinPoet, despite its name, doesn't have a very Kotlin-like API ๐Ÿ˜… A little DSL for the builders would be welcome. Fortunately, this is not that complicated to do with some extension functions!
๐Ÿ˜ƒ 1
h
What do you mean with
the code it generates is not valid for ktlint
? In theory you simply can run ktlint on your generated code too, (ignoring Gradle build cache problems).
f
I had problems in other projects running KtLint on KSP generated code, I think because running Ktlint changes code and that trigger KSP again
h
While I don't think! there is an automatic ksp trigger, using two Gradle tasks changing the generated code isn't a good practice and won't work well. But this is a KSP issue, not a KotlinPoet/generator one ๐Ÿ™‚
b
You can always run embedded ktlint on generated code file (or even string) before handing it over to ksp registrar.
think smart 1
๐Ÿš€ 1
r
i'm personally not a fan of the way ktfmt formats code but it's probably much better than nothing if you want to try to pass your source code through it prior to write https://github.com/facebook/ktfmt
f
Thank you @Big Chungus and @Ryan Brink! I am more familiar with KtLint, never hard about ktfmt, but I will take a look at both