PsiElement ( @NotNull PsiElement ancestor, @NotNull PsiElement descendant) { if (ancestor == descendant) { return null; } PsiElement child = descendant; PsiElement parent = child.getParent(); while (!parent.equals(ancestor)) { child = parent; parent = child.getParent(); if (parent == null) { return null; } } return child; }