boolean (@Nullable PsiElement ancestor, @NotNull PsiElement element, boolean strict) { if (ancestor == null) return false; // fast path to avoid loading tree if (ancestor instanceof StubBasedPsiElement && ((StubBasedPsiElement<?>)ancestor).getStub() != null || element instanceof StubBasedPsiElement && ((StubBasedPsiElement<?>)element).getStub() != null) { if (ancestor.getContainingFile() != element.getContainingFile()) return false; } boolean stopAtFileLevel = !(ancestor instanceof PsiFile || ancestor instanceof PsiDirectory); PsiElement parent = strict ? element.getParent() : element; while (true) { if (parent == null) return false; if (parent.equals(ancestor)) return true; if (stopAtFileLevel && parent instanceof PsiFile) return false; parent = parent.getParent(); } }