Getting the PsiIdentifier (declaration) from a PsiReference (usage)

Answered

Hi,

Is it possible to get the declaration element from a usage element?
Similar to getting the usages of the element by using the ReferencesSearch.search method, but the other way around.

 

Thanks,

Jared

2 comments
Comment actions Permalink
Official comment

PsiReference#resolve normally returns the declaration element

Comment actions Permalink

So we managed to find a method to get the declaration. We noticed that when we used an action to get the element, it would always return the declaration. So we dug into the code and found a method called findTargetElement.

TargetElementUtil util = TargetElementUtil.getInstance();
PsiElement element = util.findTargetElement(editor, util.getReferenceSearchFlags(), elementPos);

This code snippet will get the declaration from any element from it's offset.

Hope this helps other people!

0

Please sign in to leave a comment.