Is somebody aware of tiny Kotlin compiler plugin t...
# compiler
r
Is somebody aware of tiny Kotlin compiler plugin that generates new code with the
PackageFragmentProviderExtension
? I’m always looking at the Android Synthetic implementation, but it’s quite complicated. My goal is only to generate a new top level property without a setter in a new package. I see good examples how to generate the property here: https://github.com/JetBrains/kotlin/blob/5636227857545fb11ae2790fca55934b0f88a468/plugins/android-extensions/android-extensions-compiler/src/org/jetbrains/kotlin/android/synthetic/res/syntheticDescriptorGeneration.kt Only the PackageFragmentProvider code is extremely confusing.
s
I did not see tiny ones, but I think you only need to provide package fragment with this property descriptor.
IIRC, something like that
Copy code
class T : PackageFragmentProvider {
    override fun getPackageFragments(fqName: FqName): List<PackageFragmentDescriptor> =
        when (fqName == "me.shika") {
            true -> listOf(customDescriptor)
            false -> emptyList()
        }

    class CustomDescriptor : PackageFragmentDescriptorImpl() {
        override fun getMemberScope(): MemberScope =
            object : MemberScopeImpl() {
                override fun getContributedFunctions(
                    name: Name,
                    location: LookupLocation
                ): Collection<SimpleFunctionDescriptor> =
                    if (name == "myCustomProp") {
                        listOf(genProperty(name))
                    } else {
                        emptyList()
                    }
            }
    }
}
oh, that one is for functions, but props are similar I guess 🙂
Also Raul pointed out that some descriptors (like top level functions) don't play nicely sometimes I didn't test much, but that could be a possibility
r
That’s nice, I’ll give this a try. Thank you!
s
sure, let me know if you have any success with making it work 🙂