ReferenceProvider on XmlTokenType.XML_NAME

Answered

I'm registering a ReferenceProvider for a XmlTag which refers to a file (like the Vuejs/Blade components)

Everything works but only for the XmlTokenType.XML_DATA_CHARACTERS tag, even if I use the xmlTag() pattern:

Is there a way to add Go to Declaration option but just for the XmlTokenType.XML_NAME ? (like the highlight word bar in the image)

My PsiReferenceContributor:

class BladeReferenceContributor : PsiReferenceContributor() {
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) =
registrar.registerReferenceProvider(XmlPatterns.xmlTag(), BladeTagReferenceProvider)
}

object BladeTagReferenceProvider : PsiReferenceProvider() {
override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> =
arrayOf(PsiReferenceBase.createSelfReference(element, element.project.getFile()))
}

fun Project.getFile(): PsiFile =
FilenameIndex.getFilesByName(this, "nav-link.blade.php", GlobalSearchScope.projectScope(this)).first()

I noticed that the following patterns give me the same result:

XmlPatterns.psiElement(XmlTokenType.XML_DATA_CHARACTERS)
XmlPatterns.xmlTag()

And I've tried XmlPatterns.psiElement(XmlTokenType.XML_NAME) without success

Note: I know that the function getFile returns the same file (it's just for simplicity) and XmlPatterns.xmlTag() will match every tag, again, it's just a minimal reproducible example

2
3 comments

There is already a Blade plugin by JB https://plugins.jetbrains.com/plugin/7569-blade, what are you trying to achieve?

 

Please see also related tutorial https://blog.jetbrains.com/webstorm/2022/03/building-a-plugin-for-webstorm-part-3/

0

The plugin doesn't support components using tag alias (and also showing HtmlUnknownTagInspection), only directives.

And the tutorial works fine with the xml attributes but I was trying to replicate this behavior with the xml tag name:

0

Go to Declaration doesn't work in your case because XML_TAG already has a reference to itself. You can change this behavior by creating your own XmlExtension(com.intellij.xml.XmlExtension) and overriding method XmlExtension.createTagNameReference().

If you use Laravel, take a look at Laravel Idea plugin which is able to resolve laravel components.

Also, you can vote for this ticket WI-41052 Blade: support aliased components 

0

Please sign in to leave a comment.