Utility methods for tree-processing using PsiElementPattern?

Hi,

I'd like to use PsiElementPatterns to find elements in a tree. Something like

val dotMatcher: PsiElementPattern.Capture<RExpression> = psiElement(RExpression::class.java)
.withText(".")
.withParent(psiElement(RArgumentList::class.java))

PsiTreeUtil.findMatchingElements(myFixture.file, dotMatcher)
 

Unfortunately there is no findMatchingElements, and PsiTreeUtil does not contain any methods that accept PsiElementPattern.Capture as an argument. Is there a different utility class for ElementPatterns? If not what would a concise/recommended visitor implementation for such a usecase?

Thanks,
Holger

2 comments
Official comment

The most concise approach would be to SyntaxTraverser.psiTraverser(file).filter(yourPattern::accepts).first(). It's also a very inefficient approach. Fine for tests, but for production I'd suggest to use something more smart than full traversal.

Thanks Peter, that's what I was looking for.

-Holger

0

Please sign in to leave a comment.