@@ -714,45 +714,46 @@ private fun findContainingMethod(clazz: ClassNode, lambdaMethod: MethodNode): Pa
714714 return null
715715}
716716
717- private fun findAssociatedLambda (psiClass : PsiClass , clazz : ClassNode , lambdaMethod : MethodNode ): PsiElement ? {
717+ private fun findAssociatedLambda (project : Project , scope : GlobalSearchScope , clazz : ClassNode , lambdaMethod : MethodNode ): PsiElement ? {
718718 return RecursionManager .doPreventingRecursion(lambdaMethod, false ) {
719719 val pair = findContainingMethod(clazz, lambdaMethod) ? : return @doPreventingRecursion null
720720 val (containingMethod, locationInfo) = pair
721- val parent = findAssociatedLambda(psiClass, clazz, containingMethod)
722- ? : psiClass.findMethods(containingMethod.memberReference).firstOrNull()
723- ? : return @doPreventingRecursion null
721+ val containingBodyElements = findAssociatedLambda(project, scope, clazz, containingMethod)?.let (::listOf)
722+ ? : containingMethod.findBodyElements(clazz, project, scope).ifEmpty { return @doPreventingRecursion null }
724723
725- val psiFile = psiClass .containingFile ? : return @doPreventingRecursion null
724+ val psiFile = containingBodyElements.first() .containingFile ? : return @doPreventingRecursion null
726725 val matcher = locationInfo.createMatcher<PsiElement >(psiFile)
727- parent.accept(
728- object : JavaRecursiveElementWalkingVisitor () {
729- override fun visitAnonymousClass (aClass : PsiAnonymousClass ) {
730- // skip anonymous classes
731- }
732-
733- override fun visitClass (aClass : PsiClass ) {
734- // skip inner classes
735- }
736-
737- override fun visitLambdaExpression (expression : PsiLambdaExpression ) {
738- if (matcher.accept(expression)) {
739- stopWalking()
726+ for (bodyElement in containingBodyElements) {
727+ bodyElement.accept(
728+ object : JavaRecursiveElementWalkingVisitor () {
729+ override fun visitAnonymousClass (aClass : PsiAnonymousClass ) {
730+ // skip anonymous classes
740731 }
741- // skip walking inside the lambda
742- }
743732
744- override fun visitMethodReferenceExpression ( expression : PsiMethodReferenceExpression ) {
745- // walk inside the reference first, visits the qualifier first (it's first in the bytecode)
746- super .visitMethodReferenceExpression(expression)
733+ override fun visitClass ( aClass : PsiClass ) {
734+ // skip inner classes
735+ }
747736
748- if (expression.hasSyntheticMethod ) {
737+ override fun visitLambdaExpression (expression : PsiLambdaExpression ) {
749738 if (matcher.accept(expression)) {
750739 stopWalking()
751740 }
741+ // skip walking inside the lambda
752742 }
753- }
754- },
755- )
743+
744+ override fun visitMethodReferenceExpression (expression : PsiMethodReferenceExpression ) {
745+ // walk inside the reference first, visits the qualifier first (it's first in the bytecode)
746+ super .visitMethodReferenceExpression(expression)
747+
748+ if (expression.hasSyntheticMethod) {
749+ if (matcher.accept(expression)) {
750+ stopWalking()
751+ }
752+ }
753+ }
754+ },
755+ )
756+ }
756757
757758 matcher.result
758759 }
@@ -979,7 +980,7 @@ fun MethodNode.findSourceElement(
979980 // don't walk into stub compiled elements to look for lambdas
980981 return null
981982 }
982- return findAssociatedLambda(psiClass , clazz, this )
983+ return findAssociatedLambda(project, scope , clazz, this )
983984}
984985
985986fun MethodNode.findBodyElements (clazz : ClassNode , project : Project , scope : GlobalSearchScope ): List <PsiElement > {
0 commit comments