Skip to content

Commit

Permalink
[kotlin] Refactor KotlinResolveScopeEnlarger interop.
Browse files Browse the repository at this point in the history
Move the functionality that allows K1 `KotlinResolveScopeEnlarger`s to
work with the AA `IdeKotlinByModulesResolutionScopeProvider` into its
own dedicated `IdeKotlinResolveScopeEnlargerBridge`. This will ensure
that K1 and AA resolve-scope enlargers are treated the same.
  • Loading branch information
blueshiftlabs committed Dec 20, 2024
1 parent 282009d commit 1a0814f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
implementation="org.jetbrains.kotlin.idea.base.analysisApiPlatform.IdeKotlinStatisticsStartupActivity"/>
</extensions>

<extensions defaultExtensionNs="org.jetbrains.kotlin">
<kotlinResolutionScopeEnlarger implementation="org.jetbrains.kotlin.idea.base.analysisApiPlatform.IdeKotlinResolveScopeEnlargerBridge"/>
</extensions>

<projectListeners>
<listener
class="org.jetbrains.kotlin.idea.base.analysisApiPlatform.IdeKotlinModulePackageNamesProvider$ModuleStateModificationListener"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package org.jetbrains.kotlin.idea.base.analysisApiPlatform
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinGlobalSearchScopeMerger
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeEnlarger
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeProvider
import org.jetbrains.kotlin.analysis.api.projectStructure.*
import org.jetbrains.kotlin.analysis.decompiler.psi.BuiltinsVirtualFileProvider
Expand Down Expand Up @@ -31,7 +32,7 @@ internal class IdeKotlinByModulesResolutionScopeProvider(private val project: Pr
is KaDanglingFileModule -> {
val scopes = listOf(
module.file.fileScope(),
getResolutionScope(module.contextModule)
KotlinResolutionScopeEnlarger.getEnlargedResolutionScope(module.contextModule)
)

GlobalSearchScope.union(scopes)
Expand Down Expand Up @@ -59,16 +60,7 @@ internal class IdeKotlinByModulesResolutionScopeProvider(private val project: Pr
scope
}

return if (module is KaSourceModule) {
// remove dependency on `ModuleInfo` after KT-69980 is fixed
KotlinResolveScopeEnlarger.enlargeScope(
adjustedScope,
module.openapiModule,
isTestScope = module.sourceModuleKind == KaSourceModuleKind.TEST,
)
} else {
adjustedScope
}
return adjustedScope
}

private fun adjustScope(baseScope: GlobalSearchScope, module: KaSourceModule): GlobalSearchScope {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.base.analysisApiPlatform

import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeEnlarger
import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule
import org.jetbrains.kotlin.analysis.api.projectStructure.KaSourceModule
import org.jetbrains.kotlin.idea.base.projectStructure.KaSourceModuleKind
import org.jetbrains.kotlin.idea.base.projectStructure.KotlinResolveScopeEnlarger
import org.jetbrains.kotlin.idea.base.projectStructure.openapiModule
import org.jetbrains.kotlin.idea.base.projectStructure.sourceModuleKind

/** Bridges FE1.0 [KotlinResolveScopeEnlarger] to AA [KotlinResolutionScopeEnlarger]. */
class IdeKotlinResolveScopeEnlargerBridge : KotlinResolutionScopeEnlarger {
override fun getAdditionalResolutionScope(module: KaModule): GlobalSearchScope? {
if (module !is KaSourceModule) return null

return KotlinResolveScopeEnlarger.enlargeScope(
GlobalSearchScope.EMPTY_SCOPE,
module.openapiModule,
isTestScope = module.sourceModuleKind == KaSourceModuleKind.TEST,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package org.jetbrains.kotlin.idea.base.fir.analysisApiPlatform.inheritors

import org.jetbrains.kotlin.analysis.api.platform.declarations.KotlinDirectInheritorsProvider
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeEnlarger
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeProvider
import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule
import org.jetbrains.kotlin.idea.base.psi.classIdIfNonLocal
Expand All @@ -16,6 +17,6 @@ abstract class AbstractDirectInheritorsProviderTest : AbstractInheritorsProvider

override fun resolveInheritors(targetClass: KtClass, useSiteModule: KaModule): List<ClassId> =
KotlinDirectInheritorsProvider.getInstance(project)
.getDirectKotlinInheritors(targetClass, KotlinResolutionScopeProvider.getInstance(project).getResolutionScope(useSiteModule))
.getDirectKotlinInheritors(targetClass, KotlinResolutionScopeEnlarger.getEnlargedResolutionScope(useSiteModule))
.mapNotNull { it.classIdIfNonLocal }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package org.jetbrains.kotlin.idea.vfilefinder

import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeEnlarger
import org.jetbrains.kotlin.analysis.api.platform.projectStructure.KotlinResolutionScopeProvider
import org.jetbrains.kotlin.analyzer.ModuleInfo
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
Expand All @@ -20,7 +21,7 @@ class IdeVirtualFileFinderFactory : VirtualFileFinderFactory, MetadataFinderFact
val ideaModuleInfo = module.getCapability(ModuleInfo.Capability) as? IdeaModuleInfo

val scope = if (ideaModuleInfo != null) {
KotlinResolutionScopeProvider.getInstance(project).getResolutionScope(ideaModuleInfo.toKaModule())
KotlinResolutionScopeEnlarger.getEnlargedResolutionScope(ideaModuleInfo.toKaModule())
} else {
GlobalSearchScope.allScope(project)
}
Expand Down

0 comments on commit 1a0814f

Please sign in to comment.