PsiReference to an XML file

Hi

I'm implementing a reference contributor for a custom XML. I am trying to find if the custom xml file is being referenced in any of the other xml files

<RowIterator
elementName="name"
pickLocation="NewPerson.push()"/>

This code is in one xml file called Main.myxml. And then NewPerson.myxml is another file

Objective: When I do find usages of NewPerson.myxml, I should be able to find its usage in Main.myxml i.e. Main xml should come in the results

I am implementing the contributor. Here is the code

<psi.referenceContributor implementation="com.abc.search.CustomXmlReferenceContributor"/>

 



public class CustomXmlReferenceContributor extends PsiReferenceContributor {
@Override
public void registerReferenceProviders(@NotNull final PsiReferenceRegistrar registrar) {


registrar.registerReferenceProvider(PlatformPatterns.psiElement(XmlTag.class),
new PsiReferenceProvider() {
@NotNull
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
XmlTag tag = (XmlTag) element;
XmlAttribute attribute = tag.getAttribute("pickLocation");
if (attribute == null) {
return PsiReference.EMPTY_ARRAY;
}
String attributeValue = attribute.getValue();
System.out.println(attributeValue);
System.out.println(element.getContainingFile());

int i = attributeValue.indexOf(".push");
if (i != -1) {
attributeValue = attributeValue.substring(0, i);
}

PsiFile[] files = FilenameIndex.getFilesByName(element.getProject(), attributeValue + ".myxml", ProjectScope.getProjectScope(element.getProject()));

if (files.length == 0) {
return PsiReference.EMPTY_ARRAY;
}

String finalAttributeValue = attributeValue;
return new PsiReference[]{new PsiReference() {
public PsiElement getElement() {
return tag;
}

public TextRange getRangeInElement() {
final int start = tag.getTextRange().getStartOffset();
return tag.getValue().getTextRange().shiftRight(-start);
}

@Nullable
public PsiElement resolve() {
return files.length > 0 ? files[0] : null;
}

@NotNull
public String getCanonicalText() {
return finalAttributeValue;
}

public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
throw new UnsupportedOperationException();
}

public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
throw new UnsupportedOperationException();
}

public boolean isReferenceTo(PsiElement element) {
return resolve() == element;
}

public Object[] getVariants() {
return null;
}

public boolean isSoft() {
return false;
}
}
};
}
});

}
}

But this does not work. What am I missing? I would really appreciate any help or tip how this whole thing works..

Thanks in advance!

1

I have a contributor which looks through all the xml tags. And when an xml tag is found and has the required attribute, it forms the file name and looks through the File index. If it finds that file, it creates a reference to that file. And that reference is to that xml file is known through the resolve method?

I am guessing this is how it works. May be I am missing a piece. May be I don't know how it works.

 

1

Hi, how do you know it doesn't work? Did you try to debug your code? Do you see it to be invoked?

0

请先登录再写评论。