I want to generate Kotlin code from another source...
# getting-started
h
I want to generate Kotlin code from another source. At the moment I have my own representation and use KotlinPoet. This works fine, but I want to improve the code, eg
!(foo == "FOO")
should refactored into this
foo != "FOO"
. Is it possible to use the Kotlin plugin/frontend and its Kotlin fixup automatically? (I know this would require mapping my internal representation to Kotlin frontend code) Or do you know some other library which has basic optimizations? Alternative I have to write these Kotlin optimization by myself
c
generally generated code isn’t checked-in/maintained or otherwise needed to be viewed by people (transient; generated as part of build process, for example), making stylistic optimizations redundant. Is that the case here?
h
Yes, the converted code should be generated once as a base for further development. And I want to make this initial code to look good. I already have some optimizations, but I thought I can reuse other optimizations like the one from IntelliJ. Or other libs, if some exists.
c
post-generation one could use IntelliJ’s Analyze->Inspect Code to flag issues, which can be selectively bulk-fixed from the analysis results.
h
Yeah, thats true, but only manually.
c
sure, but that is a small investment for a one-time cleanup relative to writing all kinds of generation-optimizations. For any given inspection, right-click and fix all violations across the code base.