|
| 1 | +package io.github.amichne.kast.intellij |
| 2 | + |
| 3 | +import com.intellij.openapi.application.ApplicationManager |
| 4 | +import com.intellij.openapi.project.Project |
| 5 | +import com.intellij.psi.PsiElement |
| 6 | +import com.intellij.psi.PsiRecursiveElementWalkingVisitor |
| 7 | +import com.intellij.psi.search.GlobalSearchScope |
| 8 | +import com.intellij.psi.search.searches.ReferencesSearch |
| 9 | +import io.github.amichne.kast.shared.analysis.callHierarchyDeclaration |
| 10 | +import io.github.amichne.kast.shared.analysis.resolvedFilePath |
| 11 | +import io.github.amichne.kast.shared.analysis.toSymbolModel |
| 12 | +import io.github.amichne.kast.shared.hierarchy.CallEdge |
| 13 | +import io.github.amichne.kast.shared.hierarchy.CallEdgeResolver |
| 14 | +import io.github.amichne.kast.shared.hierarchy.callSiteLocation |
| 15 | + |
| 16 | +/** |
| 17 | + * IntelliJ-backend implementation of [CallEdgeResolver]. |
| 18 | + * |
| 19 | + * Uses [ReferencesSearch] and [GlobalSearchScope.projectScope] for incoming |
| 20 | + * edges, and a [PsiRecursiveElementWalkingVisitor] walk for outgoing edges. |
| 21 | + * |
| 22 | + * Each method acquires its own short-lived read lock so that the caller |
| 23 | + * (recursive [io.github.amichne.kast.shared.hierarchy.CallHierarchyEngine]) |
| 24 | + * does **not** need to hold the IDE read lock for the entire traversal. |
| 25 | + */ |
| 26 | +internal class IntelliJCallEdgeResolver( |
| 27 | + private val project: Project, |
| 28 | + private val workspacePrefix: String, |
| 29 | +) : CallEdgeResolver { |
| 30 | + |
| 31 | + override fun incomingEdges( |
| 32 | + target: PsiElement, |
| 33 | + timeoutCheck: () -> Boolean, |
| 34 | + onFileVisited: (filePath: String) -> Unit, |
| 35 | + ): List<CallEdge> = ApplicationManager.getApplication().runReadAction<List<CallEdge>> { |
| 36 | + val edges = mutableListOf<CallEdge>() |
| 37 | + val visitedFiles = mutableSetOf<String>() |
| 38 | + val searchScope = GlobalSearchScope.projectScope(project) |
| 39 | + |
| 40 | + ReferencesSearch.search(target, searchScope).forEach { ref -> |
| 41 | + if (timeoutCheck()) return@runReadAction edges |
| 42 | + val element = ref.element |
| 43 | + val filePath = element.resolvedFilePath().value |
| 44 | + if (visitedFiles.add(filePath)) { |
| 45 | + onFileVisited(filePath) |
| 46 | + } |
| 47 | + if (!filePath.startsWith(workspacePrefix)) return@forEach |
| 48 | + |
| 49 | + val caller = element.callHierarchyDeclaration() ?: return@forEach |
| 50 | + edges += CallEdge( |
| 51 | + target = caller, |
| 52 | + symbol = caller.toSymbolModel(containingDeclaration = null), |
| 53 | + callSite = ref.callSiteLocation(), |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + edges |
| 58 | + } |
| 59 | + |
| 60 | + override fun outgoingEdges( |
| 61 | + target: PsiElement, |
| 62 | + timeoutCheck: () -> Boolean, |
| 63 | + onFileVisited: (filePath: String) -> Unit, |
| 64 | + ): List<CallEdge> = ApplicationManager.getApplication().runReadAction<List<CallEdge>> { |
| 65 | + val declaration = target.callHierarchyDeclaration() ?: return@runReadAction emptyList() |
| 66 | + val filePath = declaration.resolvedFilePath().value |
| 67 | + onFileVisited(filePath) |
| 68 | + val edges = mutableListOf<CallEdge>() |
| 69 | + |
| 70 | + declaration.accept( |
| 71 | + object : PsiRecursiveElementWalkingVisitor() { |
| 72 | + override fun visitElement(element: PsiElement) { |
| 73 | + if (timeoutCheck()) { |
| 74 | + stopWalking() |
| 75 | + return |
| 76 | + } |
| 77 | + // Skip nested declarations to avoid expanding inner hierarchy targets. |
| 78 | + if (element !== declaration && element.callHierarchyDeclaration() === element) { |
| 79 | + return |
| 80 | + } |
| 81 | + element.references.forEach { reference -> |
| 82 | + val resolved = reference.resolve() ?: return@forEach |
| 83 | + if (resolved.containingFile == null) return@forEach |
| 84 | + val resolvedPath = resolved.resolvedFilePath().value |
| 85 | + if (!resolvedPath.startsWith(workspacePrefix)) return@forEach |
| 86 | + edges += CallEdge( |
| 87 | + target = resolved, |
| 88 | + symbol = resolved.toSymbolModel(containingDeclaration = null), |
| 89 | + callSite = reference.callSiteLocation(), |
| 90 | + ) |
| 91 | + } |
| 92 | + super.visitElement(element) |
| 93 | + } |
| 94 | + }, |
| 95 | + ) |
| 96 | + |
| 97 | + edges |
| 98 | + } |
| 99 | +} |
0 commit comments