[kotlin fir] fix creating symbol not in analysis scope

^KTIJ-21504 fixed
This commit is contained in:
Ilya Kirillov
2022-04-19 13:17:28 +02:00
parent 25d6439471
commit 3b4eb2d788
7 changed files with 113 additions and 7 deletions
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.api.impl.base.components
import com.intellij.psi.PsiElement
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.components.KtAnalysisScopeProvider
import org.jetbrains.kotlin.analysis.api.tokens.ValidityToken
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.analysis.project.structure.allDirectDependencies
import org.jetbrains.kotlin.psi.psiUtil.contains
class KtAnalysisScopeProviderImpl(
override val analysisSession: KtAnalysisSession,
override val token: ValidityToken
) : KtAnalysisScopeProvider() {
private val allModules = analysisSession.useSiteModule.collectAllDependenciesWithSelf()
private val analysisScope = GlobalSearchScope.union(allModules.map { it.contentScope })
override fun getAnalysisScope(): GlobalSearchScope = analysisScope
override fun canBeAnalysed(psi: PsiElement): Boolean {
return analysisScope.contains(psi)
}
}
private fun KtModule.collectAllDependenciesWithSelf(): List<KtModule> {
val stack = mutableListOf(this)
val visited = mutableSetOf<KtModule>()
while (stack.isNotEmpty()) {
val current = stack.removeLast()
if (visited.add(current)) continue
current.allDirectDependencies().forEach { dependency ->
if (dependency !in visited) stack += dependency
}
}
return visited.toList()
}