I'm writing a custom gradle plugin that does some ...
# intellij
c
I'm writing a custom gradle plugin that does some code generation. I would like to automatically generate all non-ambiguous imports. I can scan project sources files with Ktlint, scan all dependencies with something like ASM, and build my own symbol dictionary. But this is a rather tedious perspective, and if I want to optimize the compilation for speed, I'll need a cache, so I'll have to handle cache freshness, etc. Since idea is obviously doing the same thing when it suggests imports, I was wondering if there is some open source code I could reuse for this task. I did not yet dig into IC sources.
c
Kotlin Poet handles imports while generating the code.
c
Thanks, @Chris Lee. That's an interesting lead.
c
np. using this heavily in many areas, including custom Gradle Plugins, to generate Kotlin code.
c
But I think KotlinPoet only solves part of the problem. It does not build such a symbol dictionary. It only imports classes which are properly referenced in the code building code, and that's the easy part. It cannot discover, say, some extension function defined elsewhere in the project.
c
it will generate types for whatever is used in the generated code - whether that be types that are directly available to the generating code, or stringly-typed references.
you’d reference that extension function with it’s fully-qualified name.
c
That's it. So it cannot suggest imports.
c
no, it generates the code you ask it to.