When a PSI element's parent can be null?
Answered
The PSIElement.getParent method Javadoc says:
/**
* Returns the parent of the PSI element.
*
* @return the parent of the element, or null if the element has no parent.
*/
@Contract(pure=true)
PsiElement getParent()
How is it possible for an element inside a file to have no parent? Even the toplevel PSI element in the file has the file itself set as the parent.
Please sign in to leave a comment.
A PsiElement is not necessarily bound to be located in a (physical) PsiFile, thus a parent could be non-existent at all. An example would be "synthetic" or "fake" PsiElements like com.intellij.psi.impl.SyntheticFileSystemItem
So, is it possible for a non-toplevel PSI element to have null parent? By "non-toplevel" I mean a PSI element whose normal parent is not the file element itself.
Yes, when PSI is in (temporary) invalid state e.g. due to modification. What problem are you trying to prevent?
I simply want to know how defensive I must be when calling `getParent`. For example when implementing editor actions like "move statement".