Skip to content

Commit

Permalink
[Analysis API] KaAnalysisScopeProvider: support non-physical files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dimonchik0036 authored and qodana-bot committed Nov 22, 2024
1 parent 3d2d953 commit 6636dc1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 6636dc1

Please sign in to comment.