sometimes psiFile.getVirtualFile() returns null in my referenceContributor

Answered

My referenceContributor extends from PsiReferenceContributor and is registered for XmlPatterns.xmlAttributeValue().

In its 

new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element,
@NotNull ProcessingContext context) {

I get the VirtualFile by using element.getContainingFile().getVirtualFile(), I need it for checking the file path.

I add a break point on that line, when user opens the XML file the break point get triggered, it works fine. But later when user click Ctrl+Space on any XML attribute value, it gets triggered again, this time the virtual file is null.

I don't understand why the referenceContributor get triggered, isn't the CompletionContributor for handling Ctrl+Space? (I tested, actually both get triggered.)

And why the virtual file is null, document says it's an in-memory file? But the XML file is stored in disk.

Thanks

0
2 comments
Official comment

CompletionContributors need to work with references, so they call reference contributors.

During completion, an in-memory file copy is created (see CompletionContributor javadoc for more details), that's why you see null virtual file. You can address that by calling PsiFile#getOriginalFile first.

Avatar
Permanently deleted user

Thank you Peter.

0

Please sign in to leave a comment.