[PSI] Add forEachDescendantOfTypeInPreorder

- `forEachDescendantOfTypeInPreorder` is a preorder variant of
  `forEachDescendantOfType`, which operates in postorder.
This commit is contained in:
Marco Pennekamp
2023-09-22 15:12:30 +02:00
committed by Space Team
parent cd8143af7b
commit 9ac639e0d3
@@ -239,6 +239,28 @@ inline fun <reified T : PsiElement> PsiElement.forEachDescendantOfType(
})
}
inline fun <reified T : PsiElement> PsiElement.forEachDescendantOfTypeInPreorder(noinline action: (T) -> Unit) {
forEachDescendantOfTypeInPreorder({ true }, action)
}
inline fun <reified T : PsiElement> PsiElement.forEachDescendantOfTypeInPreorder(
crossinline canGoInside: (PsiElement) -> Boolean,
noinline action: (T) -> Unit,
) {
checkDecompiledText()
this.accept(object : PsiRecursiveElementVisitor() {
override fun visitElement(element: PsiElement) {
if (element is T) {
action(element)
}
if (canGoInside(element)) {
super.visitElement(element)
}
}
})
}
inline fun <reified T : PsiElement> PsiElement.anyDescendantOfType(noinline predicate: (T) -> Boolean = { true }): Boolean {
return findDescendantOfType(predicate) != null
}