diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/psiUtil/psiUtils.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/psiUtil/psiUtils.kt index 188e8cd0fca..e0cbccc4366 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/psiUtil/psiUtils.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/psiUtil/psiUtils.kt @@ -239,6 +239,28 @@ inline fun PsiElement.forEachDescendantOfType( }) } +inline fun PsiElement.forEachDescendantOfTypeInPreorder(noinline action: (T) -> Unit) { + forEachDescendantOfTypeInPreorder({ true }, action) +} + +inline fun 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 PsiElement.anyDescendantOfType(noinline predicate: (T) -> Boolean = { true }): Boolean { return findDescendantOfType(predicate) != null }