Skip to content

Commit 55ad595

Browse files
committed
feat(kotlin): add callee lookup for Kotlin functions #308
Implement `lookupCallee` and `findCallees` methods in `KotlinRelatedClassProvider` to identify and return callee functions for a given Kotlin function. Added corresponding test case in `KotlinRelatedClassProviderTest`.
1 parent eacc191 commit 55ad595

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

kotlin/src/main/kotlin/cc/unitmesh/kotlin/provider/KotlinRelatedClassesProvider.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package cc.unitmesh.kotlin.provider
22

33
import cc.unitmesh.devti.provider.RelatedClassesProvider
44
import cc.unitmesh.kotlin.util.KotlinTypeResolver
5+
import com.intellij.openapi.project.Project
56
import com.intellij.psi.PsiElement
67
import com.intellij.psi.PsiFile
8+
import org.jetbrains.kotlin.idea.structuralsearch.visitor.KotlinRecursiveElementWalkingVisitor
9+
import org.jetbrains.kotlin.psi.KtCallExpression
10+
import org.jetbrains.kotlin.psi.KtNamedFunction
711

812
class KotlinRelatedClassProvider : RelatedClassesProvider {
913
override fun lookupIO(element: PsiElement): List<PsiElement> {
@@ -13,4 +17,25 @@ class KotlinRelatedClassProvider : RelatedClassesProvider {
1317
override fun lookupIO(element: PsiFile): List<PsiElement> {
1418
return KotlinTypeResolver.resolveByElement(element).values.filterNotNull().toList()
1519
}
20+
21+
override fun lookupCallee(project: Project, element: PsiElement): List<PsiElement> {
22+
return when (element) {
23+
is KtNamedFunction -> findCallees(project, element)
24+
else -> emptyList()
25+
}
26+
}
27+
28+
fun findCallees(project: Project, method: KtNamedFunction): List<KtNamedFunction> {
29+
val calledMethods = mutableSetOf<KtNamedFunction>()
30+
method.accept(object : KotlinRecursiveElementWalkingVisitor() {
31+
override fun visitCallExpression(expression: KtCallExpression) {
32+
val psiElement = expression.calleeExpression?.node?.psi?.reference?.resolve() ?: return
33+
if (psiElement is KtNamedFunction) {
34+
calledMethods.add(psiElement)
35+
}
36+
}
37+
})
38+
39+
return calledMethods.toList()
40+
}
1641
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cc.unitmesh.kotlin.provider
2+
3+
import com.intellij.psi.PsiFileFactory
4+
import com.intellij.testFramework.LightPlatformTestCase
5+
import org.intellij.lang.annotations.Language
6+
import org.jetbrains.kotlin.psi.KtNamedFunction
7+
8+
class KotlinRelatedClassProviderTest : LightPlatformTestCase() {
9+
@Language("Kotlin")
10+
val code = """
11+
fun bar() {
12+
println("bar")
13+
}
14+
15+
fun baz() {
16+
println("baz")
17+
}
18+
19+
fun foo() {
20+
bar()
21+
baz()
22+
}
23+
""".trimIndent()
24+
25+
fun testShouldReturnCorrectCallees() {
26+
// given
27+
val file = PsiFileFactory.getInstance(project).createFileFromText("test.kt", code)
28+
val lastFunction = file.children.last()
29+
30+
// when
31+
val result = KotlinRelatedClassProvider().findCallees(project, lastFunction as KtNamedFunction)
32+
33+
// then
34+
assertEquals(2, result.size)
35+
}
36+
}

0 commit comments

Comments
 (0)