Reference Search returning empty collection

Hi I'm making a eye tracker plugin for IntelliJ and I'm trying to search for references but get a empty collection. 

I tried the code from https://intellij-support.jetbrains.com/hc/en-us/community/posts/206754855-ReferencesSearch-findAll-returns-Empty-Collection but I got a null from the PsiTreeUtil.getParentOfType(element, PsiVariable.class).

Here's my current code I'm using

@Override
public void actionPerformed(AnActionEvent event) {
project = event.getData(PlatformDataKeys.PROJECT);
psiFile = event.getData(LangDataKeys.PSI_FILE);
editor = event.getData(PlatformDataKeys.EDITOR);
element = event.getData(LangDataKeys.PSI_ELEMENT);

Editor data = event.getData(PlatformDataKeys.EDITOR);
CaretModel caretModel = data.getCaretModel();
int offset = caretModel.getOffset();

PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
PsiElement elementAt = psiFile.findElementAt(offset);
PsiIdentifier parentOfType = PsiTreeUtil.getParentOfType(elementAt, PsiIdentifier.class);

System.out.println("elementAt.getText() = " + elementAt.getText());

final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(event.getProject());
Collection<PsiReference> all= ReferencesSearch.search(parentOfType, projectScope).findAll();

System.out.println("all.size() = " + all.size());
}
5 comments

So the PSI tree doesn't have a parent element of the required type at the current invocation element (LangDataKeys.PSI_ELEMENT).

0

So do I have to give the PSI tree the element var before I search? If so how would I do that? 

Sorry I'm new at using this library. Thanks for your help. 

0

I tried the code below and I'm still getting a empty collection. It seems that I'm getting the right element but the search just returns an empty collection. Is there any other way to do a reference search?

 
public void actionPerformed(AnActionEvent event) {
PsiElement element = event.getData(LangDataKeys.PSI_ELEMENT);

System.out.println("elementAt.getText() = " + element.getText());

final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(event.getProject());
Collection<PsiReference> all= ReferencesSearch.search(element, projectScope).findAll();

System.out.println("all.size() = " + all.size());
}
0

If element really is the declaration element AND GSS.projectScope() actually does contain the searched reference(s), then please provide a bug report with more information https://youtrack.jetbrains.com/issues/IDEA

0

Please sign in to leave a comment.