Change displayed name of PsiElement
Hi,
I have a PsiReferenceContributor, which successfully triggers my ReferenceProvider, which provides the references seen in the following picture.
These references are generated using the following class (Kotlin):
class BasicReference(origin: PsiElement, private val target: PsiElement) : PsiReferenceBase<PsiElement>(origin) {
override fun getVariants(): Array<Any?> {
return arrayOfNulls(0)
}
override fun resolve(): PsiElement {
return target
}
}
...where target is a PhpFile. I want to change the displayed names (in the picture), to show the files without the extensions .phtml, and to shorten the displayed path to one word (standard/special - because there is no other difference between these paths). How can I do that?
Please sign in to leave a comment.
Your reference returns one target, but the screenshot shows several of them. Why is that?
Because the picture shows instances of that class, which are instantiated using the following method:
Basically, I need to scan a folder, find all phtml files, and make a reference out of them. The function above achieves that, but I want to change the name of the created reference to something else. How can I achieve that?
This is not the intended way of using references. Usually references are created as fast as possible, based on the surrounding PSI, and the resolution (which can be very expensive) happens inside `resolve` method. If you need several targets, you don't create several references. Instead, you create one PsiPolyVariantReference and return several targets from it.
There's no easy way currently to change the presentation (similar question is asked at https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000731870-GotoDeclarationHandler-with-multiple-targets-How-to-change-the-rendering-of-found-targets- , and we'll probably add such possibility in future). You could resolve to a (Renameable)FakePsiElement where you could control rendering (via ItemPresentation), but then you'd have to ensure that following the reference navigates you to the correct file (by implementing getNavigationElement), and that rename works (by correctly handling PsiReference#isReferenceTo and possibly handleElementRename).