XmlFile (final XmlFile file) { return new Object() { @Nullable XmlFile findContext(final XmlFile file, Set<? super PsiElement> processed) { if (file != null) { processed.add(file); final XmlFile contextFile = getContextFile(file); return (contextFile == null || processed.contains(contextFile))? file : findContext(contextFile, processed); } return null; } }.findContext(file, new HashSet<>()); }