PsiElement () { final ProcessingContext context = new ProcessingContext(); final ResolvingVisitor visitor = new ResolvingVisitor(PATTERN.with(myCondition).save(TARGET), context) { @Override public void visitXmlTag(@NotNull XmlTag tag) { super.visitXmlTag(tag); if (shouldContinue()) { visitSubTags(tag); } } @Override protected boolean shouldContinue() { return context.get(TARGET) == null; } }; process(visitor); return context.get(TARGET); }