boolean (@Nullable PsiElement ancestor, @NotNull PsiElement element, boolean strict) { if (ancestor == null) return false; boolean stopAtFileLevel = !(ancestor instanceof PsiFile || ancestor instanceof PsiDirectory); PsiElement parent = strict ? element.getContext() : element; while (true) { if (parent == null) return false; if (parent.equals(ancestor)) return true; if (stopAtFileLevel && parent instanceof PsiFile) { PsiElement context = parent.getContext(); if (context == null) return false; } parent = parent.getContext(); } }