Rename/find usages not working with reference to XML tag

已回答

Hey,

I have a custom reference pointing to an XmlTag inside of a XML file, for example content.xml. When I try a rename or find usages on the referenced XmlTag it’s not working. When I debug, the reference's isReferenceTo() method is called with a XmlElementDecl instead of the expected XmlTag. The XmlElementDecl has the same name as the XML tag and is in a content.xml.dtd file, which only exists in memory (it’s not created by me and the XML file has no DTD).

How can I achieve that the rename and find usages is attempted on the actual XmlTag instead of on a pseudo DTD element?

0

Does said file have an actual DTD reference in its header?

0

No, it does not have a DTD referenced in the header. The files usually look like this:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:Folder"/>
0

Hello Victor, then you invoke find usages/rename on a tag name, it searches not for exact tag but for its declaration. To change this, you can add a reference from tag name to the tag itself.

0

Dmitry Avdeev, I finally found time to try this, but it's not yet working for me. I added a reference provider that adds references from the start and end tag name to the XML tag itself:

class XmlReferenceProvider: PsiReferenceProvider() {

   override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> {
       if (element is XmlTag) {
           var references = arrayOf<PsiReference>()
           val startTagNameElement = XmlTagUtil.getStartTagNameElement(element)
           if (startTagNameElement != null) {
               references += XmlTagNameReference(element, startTagNameElement.textRangeInParent)
           }
           val endTagNameElement = XmlTagUtil.getEndTagNameElement(element)
           if (endTagNameElement != null) {
               references += XmlTagNameReference(element, endTagNameElement.textRangeInParent)
           }
           return references
       }
       return PsiReference.EMPTY_ARRAY
   }
}

My custom XmlTagNameReference resolves to the XML tag, but I still get the XmlElementDecl. Am I missing something?

0

请先登录再写评论。