[kotlin] Migrate 'getKtModule()' usages with 'ProjectStructureProvider'

^KT-57559 Fixed

GitOrigin-RevId: cbaad6e87550f214992763659b7e125468a6349e
This commit is contained in:
Yan Zhulanow
2023-04-18 18:17:54 +09:00
committed by intellij-monorepo-bot
parent 97795afd93
commit 538e6379a6
14 changed files with 59 additions and 43 deletions

View File

@@ -6,7 +6,7 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.util.SmartList
import org.jetbrains.kotlin.analysis.project.structure.getKtModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
import org.jetbrains.kotlin.analysis.providers.KotlinResolutionScopeProvider
import org.jetbrains.kotlin.idea.base.analysis.api.utils.isKotlinBuiltins
import org.jetbrains.kotlin.idea.stubindex.*
@@ -68,10 +68,9 @@ class HLIndexHelper(val project: Project, private val scope: GlobalSearchScope)
private fun getShortName(fqName: String) = Name.identifier(fqName.substringAfterLast('.'))
@OptIn(ExperimentalStdlibApi::class)
fun createForPosition(position: PsiElement): HLIndexHelper {
val module = position.getKtModule()
val project = module.project
val project = position.project
val module = ProjectStructureProvider.getModule(project, position, null)
val scope = KotlinResolutionScopeProvider.getInstance(project).getResolutionScope(module)
return HLIndexHelper(project, scope)
}

View File

@@ -44,7 +44,7 @@ inline fun <reified T : KtModule> IdeaModuleInfo.toKtModuleOfType(): @kotlin.int
internal class ProjectStructureProviderIdeImpl(private val project: Project) : ProjectStructureProvider() {
override fun getKtModuleForKtElement(element: PsiElement): KtModule {
val config = ModuleInfoProvider.Configuration(createSourceLibraryInfoForLibraryBinaries = false)
val moduleInfo = ModuleInfoProvider.getInstance(element.project).firstOrNull(element, config)
val moduleInfo = ModuleInfoProvider.getInstance(project).firstOrNull(element, config)
?: NotUnderContentRootModuleInfo(project, element.containingFile as? KtFile)
val virtualFile = element.containingFile?.virtualFile
@@ -83,7 +83,7 @@ internal class ProjectStructureProviderIdeImpl(private val project: Project) : P
companion object {
fun getInstance(project: Project): ProjectStructureProviderIdeImpl {
return project.getService(ProjectStructureProvider::class.java) as ProjectStructureProviderIdeImpl
return ProjectStructureProvider.getInstance(project) as ProjectStructureProviderIdeImpl
}
}
}

View File

@@ -18,7 +18,7 @@ import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.analysis.api.symbols.KtDeclarationSymbol
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule
import org.jetbrains.kotlin.analysis.project.structure.getKtModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeInsight.lineMarkers.shared.NavigationPopupDescriptor
import org.jetbrains.kotlin.idea.codeInsight.lineMarkers.shared.TestableLineMarkerNavigator
@@ -197,10 +197,18 @@ fun hasExpectForActual(declaration: KtDeclaration): Boolean {
}
}
internal fun getModulesStringForMarkerTooltip(
navigatableDeclarations: Collection<SmartPsiElementPointer<KtDeclaration>>?
): String? =
navigatableDeclarations.takeUnless { it.isNullOrEmpty() }?.mapNotNull { it.element }?.joinToString { it.getKtModule().moduleName }
internal fun getModulesStringForMarkerTooltip(navigatableDeclarations: Collection<SmartPsiElementPointer<KtDeclaration>>?): String? {
if (navigatableDeclarations.isNullOrEmpty()) {
return null
}
val project = navigatableDeclarations.first().project
val projectStructureProvider = ProjectStructureProvider.getInstance(project)
return navigatableDeclarations
.mapNotNull { it.element }
.joinToString { projectStructureProvider.getModule(it, null).moduleName }
}
private val KtModule.moduleName: String
get() = (this as? KtSourceModule)?.moduleName ?: moduleDescription

View File

@@ -7,7 +7,8 @@ import com.intellij.psi.tree.IElementType
import org.jetbrains.kotlin.analysis.api.fir.diagnostics.KtFirDiagnostic
import org.jetbrains.kotlin.analysis.project.structure.KtCompilerPluginsProvider
import org.jetbrains.kotlin.analysis.project.structure.KtCompilerPluginsProvider.CompilerPluginType
import org.jetbrains.kotlin.analysis.project.structure.getKtModuleOfType
import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
import org.jetbrains.kotlin.idea.base.analysis.api.utils.KtSymbolFromIndexProvider
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixRegistrar
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixesList
@@ -44,6 +45,7 @@ private val FACTORY = diagnosticFixFactory(KtFirDiagnostic.UnresolvedReference::
listOfNotNull(quickFix)
}
private fun isAssignmentPluginEnabled(project: Project, element: PsiElement): Boolean =
project.getService(KtCompilerPluginsProvider::class.java)
.isPluginOfTypeRegistered(element.getKtModuleOfType(project), CompilerPluginType.ASSIGNMENT)
private fun isAssignmentPluginEnabled(project: Project, element: PsiElement): Boolean {
val module = ProjectStructureProvider.getModule(project, element, contextualModule = null) as? KtSourceModule ?: return false
return project.getService(KtCompilerPluginsProvider::class.java).isPluginOfTypeRegistered(module, CompilerPluginType.ASSIGNMENT)
}

View File

@@ -4,12 +4,12 @@ package org.jetbrains.kotlin.idea.fir.low.level.api
import org.jetbrains.kotlin.analysis.low.level.api.fir.LLFirResolveSessionService
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.LLFirResolveSession
import org.jetbrains.kotlin.analysis.project.structure.getKtModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
import org.jetbrains.kotlin.psi.KtElement
internal inline fun <R> resolveWithClearCaches(context: KtElement, action: (LLFirResolveSession) -> R): R {
val project = context.project
val module = context.getKtModule(project)
val module = ProjectStructureProvider.getModule(project, context, null)
val resolveSession = LLFirResolveSessionService.getInstance(project).getFirResolveSessionNoCaching(module)
return action(resolveSession)
}

View File

@@ -10,7 +10,7 @@ import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeTokenFactory
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule
import org.jetbrains.kotlin.analysis.project.structure.getKtModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.config.LanguageVersionSettings
@@ -117,7 +117,7 @@ class Fe10WrapperContextImpl(
) : Fe10WrapperContext {
private val token: KtLifetimeToken = KtLifetimeTokenForKtSymbolBasedWrappers(project, ktElement)
private val module: KtModule = ktElement.getKtModule(project)
private val module: KtModule = ProjectStructureProvider.getModule(project, ktElement, null)
override fun <R> withAnalysisSession(f: KtAnalysisSession.() -> R): R {
return analyze(ktElement, token.factory, f)

View File

@@ -10,7 +10,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule
import org.jetbrains.kotlin.analysis.project.structure.getKtModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
class AssertKotlinFileInSpecificRootCommand(text: String, line: Int) : PlaybackCommandCoroutineAdapter(text, line) {
companion object {
@@ -28,7 +28,9 @@ class AssertKotlinFileInSpecificRootCommand(text: String, line: Int) : PlaybackC
if (file == null) {
throw IllegalStateException("Psi file of document is null")
}
if (file.getKtModule() !is KtSourceModule) {
val ktModule = ProjectStructureProvider.getModule(project, file, null)
if (ktModule !is KtSourceModule) {
throw IllegalStateException("File $file not in kt source root module")
}
}

View File

@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.types.Variance
internal class KotlinAnalysisApiBasedDeclarationNavigationPolicyImpl : KotlinDeclarationNavigationPolicy {
override fun getNavigationElement(declaration: KtDeclaration): KtElement {
val project = declaration.project
when (val ktModule = declaration.getKtModule(project)) {
when (val ktModule = ProjectStructureProvider.getModule(project, declaration, null)) {
is KtLibraryModule -> {
val librarySource = ktModule.librarySources ?: return declaration
val scope = librarySource.getContentScopeWithCommonDependencies()
@@ -37,7 +37,7 @@ internal class KotlinAnalysisApiBasedDeclarationNavigationPolicyImpl : KotlinDec
override fun getOriginalElement(declaration: KtDeclaration): KtElement {
val project = declaration.project
when (val ktModule = declaration.getKtModule(project)) {
when (val ktModule = ProjectStructureProvider.getModule(project, declaration, null)) {
is KtLibrarySourceModule -> {
val libraryBinary = ktModule.binaryLibrary
val scope = libraryBinary.getContentScopeWithCommonDependencies()

View File

@@ -9,7 +9,7 @@ import com.intellij.refactoring.suggested.startOffset
import com.intellij.testFramework.UsefulTestCase
import com.intellij.testFramework.common.runAll
import org.jetbrains.kotlin.analysis.project.structure.KtLibrarySourceModule
import org.jetbrains.kotlin.analysis.project.structure.getKtModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
import org.jetbrains.kotlin.idea.fir.invalidateCaches
import org.jetbrains.kotlin.idea.resolve.AbstractReferenceResolveTest
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
@@ -29,7 +29,7 @@ abstract class AbstractKotlinNavigationToLibrarySourceTest : AbstractReferenceRe
override fun performAdditionalResolveChecks(results: List<PsiElement>) {
for (result in results) {
val navigationElement = result.navigationElement
val ktModule = navigationElement.getKtModule()
val ktModule = ProjectStructureProvider.getModule(project, navigationElement, null)
UsefulTestCase.assertTrue(
"reference should be resolved to the psi element from ${KtLibrarySourceModule::class} but was resolved to ${ktModule::class}",
ktModule is KtLibrarySourceModule

View File

@@ -16,7 +16,6 @@ import org.jetbrains.uast.UElement
import org.jetbrains.uast.UExpression
import org.jetbrains.uast.UastLanguagePlugin
import org.jetbrains.uast.kotlin.FirKotlinConverter.convertDeclarationOrElement
import org.jetbrains.uast.kotlin.FirKotlinUastResolveProviderService
import org.jetbrains.uast.kotlin.psi.UastFakeSourceLightPrimaryConstructor
import org.jetbrains.uast.util.ClassSet
import org.jetbrains.uast.util.ClassSetsWrapper

View File

@@ -16,7 +16,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.markers.KtNamedSymbol
import org.jetbrains.kotlin.analysis.api.types.*
import org.jetbrains.kotlin.analysis.project.structure.KtLibraryModule
import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule
import org.jetbrains.kotlin.analysis.project.structure.getKtModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
import org.jetbrains.kotlin.asJava.toLightAnnotation
import org.jetbrains.kotlin.asJava.toLightElements
import org.jetbrains.kotlin.idea.references.mainReference
@@ -363,8 +363,10 @@ interface FirKotlinUastResolveProviderService : BaseKotlinUastResolveProviderSer
return resolveSyntheticJavaPropertyAccessorCall(ktExpression)
}
val project = ktExpression.project
val resolvedTargetElement = analyzeForUast(ktExpression) {
psiForUast(resolvedTargetSymbol, ktExpression.project)
psiForUast(resolvedTargetSymbol, project)
}
// Shortcut: if the resolution target is compiled class/member, package info, or pure Java declarations,
@@ -376,16 +378,18 @@ interface FirKotlinUastResolveProviderService : BaseKotlinUastResolveProviderSer
return resolvedTargetElement
}
when ((resolvedTargetElement as? KtDeclaration)?.getKtModule(ktExpression.project)) {
is KtSourceModule -> {
// `getMaybeLightElement` tries light element conversion first, and then something else for local declarations.
resolvedTargetElement.getMaybeLightElement(ktExpression)?.let { return it }
if (resolvedTargetElement != null) {
when (ProjectStructureProvider.getModule(project, resolvedTargetElement, null)) {
is KtSourceModule -> {
// `getMaybeLightElement` tries light element conversion first, and then something else for local declarations.
resolvedTargetElement.getMaybeLightElement(ktExpression)?.let { return it }
}
is KtLibraryModule -> {
// For decompiled declarations, we can try light element conversion (only).
(resolvedTargetElement as? KtDeclaration)?.toLightElements()?.singleOrNull()?.let { return it }
}
else -> {}
}
is KtLibraryModule -> {
// For decompiled declarations, we can try light element conversion (only).
(resolvedTargetElement as? KtDeclaration)?.toLightElements()?.singleOrNull()?.let { return it }
}
else -> {}
}
fun resolveToPsiClassOrEnumEntry(classOrObject: KtClassOrObject): PsiElement? {

View File

@@ -4,7 +4,7 @@ package org.jetbrains.uast.kotlin.internal
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.analysis.project.structure.KtNotUnderContentRootModule
import org.jetbrains.kotlin.analysis.project.structure.getKtModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.uast.kotlin.FirKotlinUastResolveProviderService
import org.jetbrains.uast.kotlin.unwrapFakeFileForLightClass
@@ -15,6 +15,7 @@ class FirCliKotlinUastResolveProviderService : FirKotlinUastResolveProviderServi
// The `getKtModule` optimization of `FirIdeaKotlinUastResolveProviderService` cannot be applied here, because `uast-kotlin-fir` is
// available externally and doesn't have access to `intellij.platform.projectModel`. See also KTIJ-24932.
val containingFile = psiElement.containingFile?.let(::unwrapFakeFileForLightClass) as? KtFile ?: return false
return containingFile.getKtModule(containingFile.project) !is KtNotUnderContentRootModule
val module = ProjectStructureProvider.getModule(containingFile.project, containingFile, contextualModule = null)
return module !is KtNotUnderContentRootModule
}
}

View File

@@ -10,11 +10,11 @@ import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.analysis.api.calls.KtCallableMemberCall
import org.jetbrains.kotlin.analysis.api.calls.symbol
import org.jetbrains.kotlin.analysis.api.components.buildClassType
import org.jetbrains.kotlin.analysis.api.getModule
import org.jetbrains.kotlin.analysis.api.lifetime.KtAlwaysAccessibleLifetimeTokenFactory
import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.types.*
import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule
import org.jetbrains.kotlin.analysis.project.structure.getKtModule
import org.jetbrains.kotlin.analysis.providers.DecompiledPsiDeclarationProvider.findPsi
import org.jetbrains.kotlin.asJava.findFacadeClass
import org.jetbrains.kotlin.asJava.getRepresentativeLightMethod
@@ -92,7 +92,7 @@ internal fun KtAnalysisSession.toPsiMethod(
is KtFunction -> {
// For JVM-invisible methods, such as @JvmSynthetic, LC conversion returns nothing, so fake it
fun handleLocalOrSynthetic(source: KtFunction): PsiMethod? {
val ktModule = source.getKtModule(context.project)
val ktModule = getModule(source)
if (ktModule !is KtSourceModule) return null
return getContainingLightClass(source)?.let { UastFakeSourceLightMethod(source, it) }
}

View File

@@ -5,7 +5,7 @@ package org.jetbrains.uast.kotlin.internal
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.analysis.project.structure.KtNotUnderContentRootModule
import org.jetbrains.kotlin.analysis.project.structure.getKtModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.uast.kotlin.FirKotlinUastResolveProviderService
import org.jetbrains.uast.kotlin.internal.util.ReadActionSingleValueCache
@@ -41,6 +41,7 @@ class FirIdeaKotlinUastResolveProviderService : FirKotlinUastResolveProviderServ
}
// The checks above might not work in all possible situations (e.g. scripts) and `getKtModule` is able to give a definitive answer.
return file.getKtModule(project) !is KtNotUnderContentRootModule
val module = ProjectStructureProvider.getModule(project, file, contextualModule = null)
return module !is KtNotUnderContentRootModule
}
}