What's the methed call to Resolve a Kotlin reference
The following code works for Java files:
val node: ASTNode
....
val referencedFieldElement = node.psi.reference!!.resolve()
It doesn't work for Kotlin files because there is no resolve() method call and node.psi.reference returns null.
How do you resolve a Kotlin reference ?
请先登录再写评论。
I have exactly the same question
I have a plugin written for Java which works great but it doesn't support the Kotlin files because I can't find a way to resolve the reference.
Any idea are appreciated.
What I've tried:
The code above returns an empty array, while the ktFile is a valid kotlin file and can be compiled within my mixed Java-Kotlin project (all necessary "PsiFile"s are included in the "Project" of "ktFile").
This code returns an empty list.
But at the same time, all Java files are in the same project and all of them resolves to correct references.
How can I resolve Kotlin references?
First of all, you're only looking for references on the leaf nodes. It's not the case that only leaf nodes can contain references. If you don't find any references on a leaf node, you need to use `PsiElement.getParent()` to look for references in its parent nodes.
Second, an element can contain multiple references (and Kotlin elements do). Instead of calling `getReference().resolve()`, you need to call `getReferences()` and iterate over them until you find one which is resolved to a non-null element.
I will try that. Do you know by any chance the sources of a plugin which illustrates this usage ?
I don't, sorry.
@yole I tried this, according to you:
And I get an empty list...