From 6636dc116327269b0cf9877adbdde178533645d2 Mon Sep 17 00:00:00 2001 From: Dmitrii Gridin Date: Wed, 13 Nov 2024 14:56:27 +0100 Subject: [PATCH] [Analysis API] KaAnalysisScopeProvider: support non-physical files Non-physical files may not have a virtual file under the hood, but they are still dangling files, so they should be analyzed properly. The change is covered by `StandaloneSessionBuilderTest.testNonPhysicalFile` ^KT-66783 --- .../components/KaBaseAnalysisScopeProviderImpl.kt | 12 ++++++++++-- .../api/impl/base/sessions/KaGlobalSearchScope.kt | 15 +++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/components/KaBaseAnalysisScopeProviderImpl.kt b/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/components/KaBaseAnalysisScopeProviderImpl.kt index 96c93e6f125c1..00daae0265d4e 100644 --- a/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/components/KaBaseAnalysisScopeProviderImpl.kt +++ b/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/components/KaBaseAnalysisScopeProviderImpl.kt @@ -10,8 +10,10 @@ import com.intellij.psi.search.GlobalSearchScope import org.jetbrains.kotlin.analysis.api.KaImplementationDetail import org.jetbrains.kotlin.analysis.api.KaSession import org.jetbrains.kotlin.analysis.api.components.KaAnalysisScopeProvider +import org.jetbrains.kotlin.analysis.api.getModule import org.jetbrains.kotlin.analysis.api.impl.base.sessions.KaGlobalSearchScope import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion +import org.jetbrains.kotlin.analysis.api.projectStructure.isDangling import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.psiUtil.contains @@ -28,7 +30,13 @@ class KaBaseAnalysisScopeProviderImpl( } private fun PsiElement.isFromGeneratedModule(): Boolean { - val file = (containingFile as? KtFile)?.virtualFile ?: return false - return useSiteScope.isFromGeneratedModule(file, analysisSession.useSiteModule) + val ktFile = containingFile as? KtFile ?: return false + if (ktFile.isDangling) { + val module = analysisSession.getModule(ktFile) + return useSiteScope.isFromGeneratedModule(module) + } + + val virtualFile = ktFile.virtualFile ?: return false + return useSiteScope.isFromGeneratedModule(virtualFile) } } diff --git a/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/sessions/KaGlobalSearchScope.kt b/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/sessions/KaGlobalSearchScope.kt index 273a4e7f4cbd8..539cc02c08a77 100644 --- a/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/sessions/KaGlobalSearchScope.kt +++ b/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/sessions/KaGlobalSearchScope.kt @@ -36,16 +36,23 @@ class KaGlobalSearchScope( } override fun contains(file: VirtualFile): Boolean { - return (baseScope.contains(file) && !shadowedScope.contains(file)) || isFromGeneratedModule(file, useSiteModule) + return (baseScope.contains(file) && !shadowedScope.contains(file)) || isFromGeneratedModule(file) } override fun toString(): String { return "Analysis scope for $useSiteModule (base: $baseScope, shadowed: $shadowedScope)" } - fun isFromGeneratedModule(file: VirtualFile, useSiteModule: KaModule): Boolean { + /** + * To support files from [org.jetbrains.kotlin.analysis.api.resolve.extensions.KaResolveExtensionProvider] + * which are not dangling files + */ + fun isFromGeneratedModule(file: VirtualFile): Boolean { val analysisContextModule = file.analysisContextModule ?: return false - if (analysisContextModule == useSiteModule) return true - return analysisContextModule in useSiteModule.allDirectDependencies() + return isFromGeneratedModule(analysisContextModule) + } + + fun isFromGeneratedModule(analysisContextModule: KaModule): Boolean { + return analysisContextModule == useSiteModule || analysisContextModule in useSiteModule.allDirectDependencies() } }