[AA] implement infrastructure for scripts

^KT-60728
This commit is contained in:
Dmitrii Gridin
2023-07-26 16:03:01 +02:00
committed by Space Team
parent e5cd674f33
commit c01c113af4
6 changed files with 113 additions and 3 deletions
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2023 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.test.configurators
import com.intellij.mock.MockApplication
import com.intellij.mock.MockProject
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestServiceRegistrar
import org.jetbrains.kotlin.scripting.compiler.plugin.definitions.CliScriptDefinitionProvider
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionProvider
import org.jetbrains.kotlin.test.services.TestServices
object AnalysisApiScriptTestServiceRegistrar : AnalysisApiTestServiceRegistrar() {
override fun registerProjectExtensionPoints(project: MockProject, testServices: TestServices) {
}
override fun registerProjectServices(project: MockProject, testServices: TestServices) {
}
override fun registerProjectModelServices(project: MockProject, testServices: TestServices) {
project.registerService(ScriptDefinitionProvider::class.java, CliScriptDefinitionProvider())
}
override fun registerApplicationServices(application: MockApplication, testServices: TestServices) {
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 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.
*/
@@ -34,7 +34,8 @@ class AnalysisApiKtModuleProviderImpl(
return modulesByName.getValue(moduleName).ktModule
}
override fun getModuleFiles(module: TestModule): List<PsiFile> = modulesByName.getValue(module.name).files
override fun getModuleFiles(module: TestModule): List<PsiFile> =
(modulesByName[module.name] ?: modulesByName.getValue(module.files.single().name)).files
override fun registerProjectStructure(modules: KtModuleProjectStructure) {
require(!this::modulesStructure.isInitialized)
@@ -58,6 +59,7 @@ fun List<KtModuleWithFiles>.associateByName(): Map<String, KtModuleWithFiles> {
is KtSdkModule -> ktModule.sdkName
is KtBuiltinsModule -> "Builtins for ${ktModule.platform}"
is KtNotUnderContentRootModule -> ktModule.name
is KtScriptModule -> ktModule.file.name
else -> error("Unsupported module type: " + ktModule.javaClass.name)
}
}
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2023 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.test.framework.project.structure
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.KtModuleWithFiles
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
class KtScriptModuleFactory : KtModuleFactory {
override fun createModule(testModule: TestModule, testServices: TestServices, project: Project): KtModuleWithFiles {
val ktFile = TestModuleStructureFactory.createSourcePsiFiles(testModule, testServices, project).single() as KtFile
val module = KtScriptModuleImpl(
ktFile,
testModule.targetPlatform,
testModule.languageVersionSettings,
project,
)
return KtModuleWithFiles(module, listOf(ktFile))
}
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2023 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.test.framework.project.structure
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.project.structure.*
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
import org.jetbrains.kotlin.test.getAnalyzerServices
class KtScriptModuleImpl(
override val file: KtFile,
override val platform: TargetPlatform,
override val languageVersionSettings: LanguageVersionSettings,
override val project: Project,
) : KtModuleWithModifiableDependencies(), KtScriptModule {
override val contentScope: GlobalSearchScope get() = GlobalSearchScope.fileScope(file)
override val analyzerServices: PlatformDependentAnalyzerServices get() = platform.getAnalyzerServices()
override val directRegularDependencies: MutableList<KtModule> = mutableListOf()
override val directDependsOnDependencies: MutableList<KtModule> = mutableListOf()
override val directFriendDependencies: MutableList<KtModule> = mutableListOf()
}
@@ -50,7 +50,8 @@ object TestModuleStructureFactory {
val binaryModulesBySourceRoots = mutableMapOf<Set<Path>, KtBinaryModule>()
for (testModule in moduleStructure.modules) {
when (val ktModule = moduleEntriesByName.getValue(testModule.name).ktModule) {
val moduleWithFiles = moduleEntriesByName[testModule.name] ?: moduleEntriesByName.getValue(testModule.files.single().name)
when (val ktModule = moduleWithFiles.ktModule) {
is KtNotUnderContentRootModule -> {
// Not-under-content-root modules have no external dependencies on purpose
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2023 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.low.level.api.fir.test.configurators
import com.intellij.openapi.Disposable
import org.jetbrains.kotlin.analysis.api.impl.base.test.configurators.AnalysisApiScriptTestServiceRegistrar
import org.jetbrains.kotlin.analysis.test.framework.project.structure.KtModuleFactory
import org.jetbrains.kotlin.analysis.test.framework.project.structure.KtScriptModuleFactory
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestServiceRegistrar
import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder
object AnalysisApiFirScriptTestConfigurator : AnalysisApiFirSourceLikeTestConfigurator(analyseInDependentSession = false) {
override fun configureTest(builder: TestConfigurationBuilder, disposable: Disposable) {
super.configureTest(builder, disposable)
builder.apply {
useAdditionalService<KtModuleFactory> { KtScriptModuleFactory() }
}
}
override val serviceRegistrars: List<AnalysisApiTestServiceRegistrar> = super.serviceRegistrars + AnalysisApiScriptTestServiceRegistrar
}