Using PsiElement for recursive code walk?

Once I've found a reference to an element using;

I'd like to take the element that is found, find the method that's containing that element and do a reference search on that.  I need to walk my code all the way to completion.  I'm having trouble picturing what exactly an element is, along with what it's siblings, parents and children are.  I had hoped that getting the parent would give me the containing method of the element but no dice.  How do I find the containing method and get the element of it so I can call a reference search on it?

Additionally, I can't find a good explanation on what a PsiElement is.  In that if I were to have a PsiElement I can't quite picture how that sits in reference to my code.


1 comment
Official comment

PSI is syntactic tree of the code, and PsiElement is any node of it, see for more details.

A PsiElement might have some associated references, that are returned by its getReferences method, and that return this PsiElement from their getElement method. ReferencesSearch returns such references. Once you obtain a PSI element from them, you can analyze their surroundings. To find the closest containing method, the easiest way is to traverse parents until you find one, or use PsiTreeUtil.getParentOfType which does exactly that.

Please sign in to leave a comment.