How to choose a `newKind` for a `fakeElement()` w...
# compiler
e
How to choose a
newKind
for a
fakeElement()
when generating properties/functions in
FirDeclarationGenerationExtension
? Whats developer writes:
Copy code
package com.example
import compiled.library.SomeClass
@ASomeClass()
class SomeClassMixin {
    @B fun iwannabeext() {}
}
-> generated virtual:
Copy code
package compiled.library
fun SomeClass.iwannabeext() {}
-> to call it later:
Copy code
fun foo(bar: SomeClass) { bar.iwannabeext() }
So, I need to bind: Virtual top-level function with receiver - Original member function in a class (without receiver). Note that real dispatch receiver is not same as virtual extension receiver. I notice that the choice of kind leads to various unpredictable behaviors. • For functions: I use
FromUseSiteTarget
. I have all the IDE integration working except I can't rename the original at call site. • For properties: I use
PropertyTypeFromGetterReturnType
. After a lot of trial and error, this kind finally stopped triggering false positive unused imports, but I still haven't gotten a working "go to declaration" (from call to original). Does anyone have any suggestions for a different kind? I should also mention that in the future I'm going to make an IDEA plugin because I want a custom color for calls.
Here is my `FirDeclarationGenerationExtension`: gist.github.com/diskria/…
I wanted to write a helper to sort through the kinds in search of the most suitable one, but perhaps this is too much 😄
y
Generally, you should use
PluginGenerated
, unless you really really know what you're doing, and even then, probably should use
PluginGenerated
anyway
e
@Youssef Shoaib [MOD] oh, I tried, but the calls are not resolved
I checked again, the calls became unresolved, and sometimes they become the color of a member call and not the extension
I wonder if it's possible to create your own custom fake to figure everything out without guessing...
y
github.com/JetBrains/kotlin/blob/…/KtSourceElement.kt#… Do you mean in IDE? Yeah that's the one case where I've personally used other such kinds The easiest thing you can do is find some Kotlin code that behaves similarly w.r.t. IDE resolution, and see what fake source kinds are set on it Alternatively, you can figure it out from compiler code I've used it before to make Go to Definition act like a
this@label
or something like that, using
GeneratedLambdaLabel
I believe
I don't think creating a custom fake can work. IDE (more accurately Analysis API) knows about these fake source elements and treats them specially. You could maybe do something with an IDE plugin and the Analysis API though
e
Btw, have you also encountered the situation where the IDE does not add imports automatically? When selecting virtual decl from autocomplete popup
y
I don't think so, but I haven't played around with top-level generation that much. If you can reproduce it, please file an issue
e
Okay, I'll see how this is implemented in the compiler
Okay, I'll make issue when I figure it out better!
y
The fact your packages are unrelated makes me think it's unlikely to work, but that's just a hunch If you were generating in the same package, then I could maybe see it working.
e
Yes, choosing a package strategy adds complexity, but unfortunately, this is a requirement of "business logic" for imports stability.
y
Useful shortlist not sure what it actually does, but it makes sense why
FromUseSiteTarget
would work pretty well then
e
That's interesting, I'll look into it
KA is stable api between FIR and KSP, afaik
but I haven't really figured out KA yet
y
KA (as in
KaModule
etc) just means Kotlin Analysis I believe, so it's a different name for Analysis API
e
Ah okay
I think I'll move on to the IDEA plugin to complete this integration, but I'll leave the current fakes as a fallback in case the developer doesn't have the plugin installed.
ItLambdaParameter
would be an ideal candidate since it is the only fake that makes Go to definition work, but for properties this results in a false positive unused import (interestingly, there is no such problem for imported functions)
y
Probably because a value parameter and a property are both "variables" in the compiler sense, while functions aren't. Do keep in mind that this behavior is probably subject to change (as are most things in compiler plugins). It's not a tested-for use case I don't think
e
It's sad that there is no way to fine-tune this thing
it's like a lottery
y
I can't find a feature request for this oddly enough. Your use case seems pretty valid though If you reach your wits end with this exploration, it'd be really helpful if you file an issue requesting such an API
e
Do you think that if virtual top levels were generated in the original package, I would have fewer problems with this?
I wouldn't want to give up on the package name strategy, but...
y
Maybe? It is absolutely a lottery here, but I have a feeling that same package could help smooth things over a bit. I'm AFK so can't look this up, but I'd recommend looking at how generated data class members are handled
e
Switching to the same package, just as an experiment
nothing has changed, eh
@Youssef Shoaib [MOD] Thanks for the idea, I'll see how
copy()
generated, tomorrow