https://kotlinlang.org logo
#ksp
Title
# ksp
h

Hristijan

10/08/2023, 11:29 AM
Hey guys, when you generate code using Kotlin poet, do you take care of alignment of the generated code and how do you do it if so?
m

mbonnin

10/08/2023, 12:58 PM
You can to some extent make it look ok by adding the. newlines and
indent()
/
unindent()
but kotlinpoet is not a linter, it will not enforce style rules
2
If you want to do this, you can postprocess with
ktlint
or other linters but I found myself that it's often not worth it
j

Johann Pardanaud

10/08/2023, 1:10 PM
Is it that important to have generated code compliant with your linter? After all, you're not linting the generated bytecode. I really recommend you not wasting time with this. Generated code is not what you read/write when maintaining your project, because it is generated; and unless its indentation messes up the implementation (which should not) then you should not care about it.
h

hfhbd

10/08/2023, 2:11 PM
It depends on your use case. If you want to generate “helper” code stored locally in the unchecked build folder formatting the code is useless, waisted time. We use kotlinpoet to generate code only once which is expected to be checked in and is the code base for further development. This use case requires formatting , either by using the kotlinpoet api or an extra tool.
j

Johann Pardanaud

10/08/2023, 2:37 PM
You're right, I was talking about a compiler plugin. For code generated only once and made to be modified by a human later, it is perfectly fine to do a linter pass right after generation.
h

Hristijan

10/08/2023, 3:26 PM
@Johann Pardanaud the generated code is not linted, for better readability i meant, i like to go an extra mile, i know it's not worth it but i wanted to know the approaches
2 Views