AA: introduce KtModule builders
This commit is contained in:
committed by
Ilya Kirillov
parent
d592dab4b6
commit
8528f6244d
+13
@@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.builder
|
||||||
|
|
||||||
|
import java.nio.file.Path
|
||||||
|
|
||||||
|
@KtModuleBuilderDsl
|
||||||
|
public abstract class KtBinaryModuleBuilder : KtModuleBuilder() {
|
||||||
|
public lateinit var binaryRoots: Collection<Path>
|
||||||
|
}
|
||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.builder
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtLibraryModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtLibrarySourceModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.impl.KtLibraryModuleImpl
|
||||||
|
import kotlin.contracts.ExperimentalContracts
|
||||||
|
import kotlin.contracts.InvocationKind
|
||||||
|
import kotlin.contracts.contract
|
||||||
|
|
||||||
|
@KtModuleBuilderDsl
|
||||||
|
public class KtLibraryModuleBuilder : KtBinaryModuleBuilder() {
|
||||||
|
public lateinit var libraryName: String
|
||||||
|
public var librarySources: KtLibrarySourceModule? = null
|
||||||
|
public var isBuiltinsContainingStdlib: Boolean = false
|
||||||
|
|
||||||
|
override fun build(): KtLibraryModule {
|
||||||
|
return KtLibraryModuleImpl(
|
||||||
|
directRegularDependencies,
|
||||||
|
directRefinementDependencies,
|
||||||
|
directFriendDependencies,
|
||||||
|
contentScope,
|
||||||
|
platform,
|
||||||
|
project,
|
||||||
|
binaryRoots,
|
||||||
|
libraryName,
|
||||||
|
librarySources,
|
||||||
|
isBuiltinsContainingStdlib,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
public inline fun buildKtLibraryModule(init: KtLibraryModuleBuilder.() -> Unit): KtLibraryModule {
|
||||||
|
contract {
|
||||||
|
callsInPlace(init, InvocationKind.EXACTLY_ONCE)
|
||||||
|
}
|
||||||
|
return KtLibraryModuleBuilder().apply(init).build()
|
||||||
|
}
|
||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.builder
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtLibraryModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtLibrarySourceModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.impl.KtLibrarySourceModuleImpl
|
||||||
|
import kotlin.contracts.ExperimentalContracts
|
||||||
|
import kotlin.contracts.InvocationKind
|
||||||
|
import kotlin.contracts.contract
|
||||||
|
|
||||||
|
@KtModuleBuilderDsl
|
||||||
|
public class KtLibrarySourceModuleBuilder : KtModuleBuilder() {
|
||||||
|
public lateinit var libraryName: String
|
||||||
|
public lateinit var binaryLibrary: KtLibraryModule
|
||||||
|
|
||||||
|
override fun build(): KtLibrarySourceModule {
|
||||||
|
return KtLibrarySourceModuleImpl(
|
||||||
|
directRegularDependencies,
|
||||||
|
directRefinementDependencies,
|
||||||
|
directFriendDependencies,
|
||||||
|
contentScope,
|
||||||
|
platform,
|
||||||
|
project,
|
||||||
|
libraryName,
|
||||||
|
binaryLibrary
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
public inline fun buildKtLibrarySourceModule(init: KtLibrarySourceModuleBuilder.() -> Unit): KtLibrarySourceModule {
|
||||||
|
contract {
|
||||||
|
callsInPlace(init, InvocationKind.EXACTLY_ONCE)
|
||||||
|
}
|
||||||
|
return KtLibrarySourceModuleBuilder().apply(init).build()
|
||||||
|
}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.builder
|
||||||
|
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtModule
|
||||||
|
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||||
|
|
||||||
|
@KtModuleBuilderDsl
|
||||||
|
public abstract class KtModuleBuilder {
|
||||||
|
public val directRegularDependencies: MutableList<KtModule> = mutableListOf()
|
||||||
|
public val directRefinementDependencies: MutableList<KtModule> = mutableListOf()
|
||||||
|
public val directFriendDependencies: MutableList<KtModule> = mutableListOf()
|
||||||
|
|
||||||
|
public lateinit var contentScope: GlobalSearchScope
|
||||||
|
public lateinit var platform: TargetPlatform
|
||||||
|
public lateinit var project: Project
|
||||||
|
|
||||||
|
public abstract fun build(): KtModule
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.builder
|
||||||
|
|
||||||
|
@DslMarker
|
||||||
|
internal annotation class KtModuleBuilderDsl
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.builder
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtSdkModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.impl.KtSdkModuleImpl
|
||||||
|
import kotlin.contracts.ExperimentalContracts
|
||||||
|
import kotlin.contracts.InvocationKind
|
||||||
|
import kotlin.contracts.contract
|
||||||
|
|
||||||
|
@KtModuleBuilderDsl
|
||||||
|
public class KtSdkModuleBuilder : KtBinaryModuleBuilder() {
|
||||||
|
public lateinit var sdkName: String
|
||||||
|
|
||||||
|
override fun build(): KtSdkModule {
|
||||||
|
return KtSdkModuleImpl(
|
||||||
|
directRegularDependencies,
|
||||||
|
directRefinementDependencies,
|
||||||
|
directFriendDependencies,
|
||||||
|
contentScope,
|
||||||
|
platform,
|
||||||
|
project,
|
||||||
|
binaryRoots,
|
||||||
|
sdkName
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
public inline fun buildKtSdkModule(init: KtSdkModuleBuilder.() -> Unit): KtSdkModule {
|
||||||
|
contract {
|
||||||
|
callsInPlace(init, InvocationKind.EXACTLY_ONCE)
|
||||||
|
}
|
||||||
|
return KtSdkModuleBuilder().apply(init).build()
|
||||||
|
}
|
||||||
+44
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.builder
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.impl.KtSourceModuleImpl
|
||||||
|
import org.jetbrains.kotlin.config.ApiVersion
|
||||||
|
import org.jetbrains.kotlin.config.LanguageVersion
|
||||||
|
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||||
|
import org.jetbrains.kotlin.config.LanguageVersionSettingsImpl
|
||||||
|
import kotlin.contracts.ExperimentalContracts
|
||||||
|
import kotlin.contracts.InvocationKind
|
||||||
|
import kotlin.contracts.contract
|
||||||
|
|
||||||
|
@KtModuleBuilderDsl
|
||||||
|
public class KtSourceModuleBuilder : KtModuleBuilder() {
|
||||||
|
public lateinit var moduleName: String
|
||||||
|
public var languageVersionSettings: LanguageVersionSettings =
|
||||||
|
LanguageVersionSettingsImpl(LanguageVersion.LATEST_STABLE, ApiVersion.LATEST)
|
||||||
|
|
||||||
|
override fun build(): KtSourceModule {
|
||||||
|
return KtSourceModuleImpl(
|
||||||
|
directRegularDependencies,
|
||||||
|
directRefinementDependencies,
|
||||||
|
directFriendDependencies,
|
||||||
|
contentScope,
|
||||||
|
platform,
|
||||||
|
project,
|
||||||
|
moduleName,
|
||||||
|
languageVersionSettings,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalContracts::class)
|
||||||
|
public inline fun buildKtSourceModule(init: KtSourceModuleBuilder.() -> Unit): KtSourceModule {
|
||||||
|
contract {
|
||||||
|
callsInPlace(init, InvocationKind.EXACTLY_ONCE)
|
||||||
|
}
|
||||||
|
return KtSourceModuleBuilder().apply(init).build()
|
||||||
|
}
|
||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.impl
|
||||||
|
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtLibraryModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtLibrarySourceModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtModule
|
||||||
|
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||||
|
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
|
||||||
|
import java.nio.file.Path
|
||||||
|
|
||||||
|
internal class KtLibraryModuleImpl(
|
||||||
|
override val directRegularDependencies: List<KtModule>,
|
||||||
|
override val directRefinementDependencies: List<KtModule>,
|
||||||
|
override val directFriendDependencies: List<KtModule>,
|
||||||
|
override val contentScope: GlobalSearchScope,
|
||||||
|
override val platform: TargetPlatform,
|
||||||
|
override val project: Project,
|
||||||
|
private val binaryRoots: Collection<Path>,
|
||||||
|
override val libraryName: String,
|
||||||
|
override val librarySources: KtLibrarySourceModule?,
|
||||||
|
internal val isBuiltinsContainingStdlib: Boolean,
|
||||||
|
) : KtLibraryModule, KtModuleWithPlatform {
|
||||||
|
override val analyzerServices: PlatformDependentAnalyzerServices = super.analyzerServices
|
||||||
|
|
||||||
|
override fun getBinaryRoots(): Collection<Path> = binaryRoots
|
||||||
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.impl
|
||||||
|
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtLibraryModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtLibrarySourceModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtModule
|
||||||
|
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||||
|
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
|
||||||
|
|
||||||
|
internal class KtLibrarySourceModuleImpl(
|
||||||
|
override val directRegularDependencies: List<KtModule>,
|
||||||
|
override val directRefinementDependencies: List<KtModule>,
|
||||||
|
override val directFriendDependencies: List<KtModule>,
|
||||||
|
override val contentScope: GlobalSearchScope,
|
||||||
|
override val platform: TargetPlatform,
|
||||||
|
override val project: Project,
|
||||||
|
override val libraryName: String,
|
||||||
|
override val binaryLibrary: KtLibraryModule,
|
||||||
|
) : KtLibrarySourceModule, KtModuleWithPlatform {
|
||||||
|
override val analyzerServices: PlatformDependentAnalyzerServices = super.analyzerServices
|
||||||
|
}
|
||||||
-126
@@ -1,126 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2022 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.project.structure.impl
|
|
||||||
|
|
||||||
import com.intellij.openapi.project.Project
|
|
||||||
import com.intellij.openapi.vfs.VirtualFile
|
|
||||||
import com.intellij.openapi.vfs.VirtualFileManager
|
|
||||||
import com.intellij.psi.search.GlobalSearchScope
|
|
||||||
import com.intellij.psi.search.ProjectScope
|
|
||||||
import com.intellij.util.io.URLUtil
|
|
||||||
import org.jetbrains.kotlin.analysis.api.impl.base.util.LibraryUtils
|
|
||||||
import org.jetbrains.kotlin.analysis.project.structure.*
|
|
||||||
import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM
|
|
||||||
import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots
|
|
||||||
import org.jetbrains.kotlin.cli.jvm.config.jvmModularRoots
|
|
||||||
import org.jetbrains.kotlin.config.*
|
|
||||||
import org.jetbrains.kotlin.platform.TargetPlatform
|
|
||||||
import org.jetbrains.kotlin.platform.jvm.JdkPlatform
|
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
|
||||||
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
|
|
||||||
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices
|
|
||||||
import java.nio.file.Path
|
|
||||||
import java.nio.file.Paths
|
|
||||||
|
|
||||||
internal abstract class BaseKtModuleByCompilerConfiguration(
|
|
||||||
private val compilerConfig: CompilerConfiguration,
|
|
||||||
val project: Project,
|
|
||||||
) {
|
|
||||||
val analyzerServices: PlatformDependentAnalyzerServices
|
|
||||||
get() = JvmPlatformAnalyzerServices
|
|
||||||
|
|
||||||
val directFriendDependencies: List<KtModule>
|
|
||||||
get() = emptyList()
|
|
||||||
|
|
||||||
val directRefinementDependencies: List<KtModule>
|
|
||||||
get() = emptyList()
|
|
||||||
|
|
||||||
val languageVersionSettings: LanguageVersionSettings
|
|
||||||
get() = LanguageVersionSettingsImpl(LanguageVersion.LATEST_STABLE, ApiVersion.LATEST)
|
|
||||||
|
|
||||||
val moduleName: String
|
|
||||||
get() = compilerConfig.get(CommonConfigurationKeys.MODULE_NAME) ?: "<no module name provided>"
|
|
||||||
|
|
||||||
val platform: TargetPlatform
|
|
||||||
get() = TargetPlatform(setOf(JdkPlatform(JvmTarget.DEFAULT)))
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class KtSourceModuleByCompilerConfiguration(
|
|
||||||
compilerConfig: CompilerConfiguration,
|
|
||||||
project: Project,
|
|
||||||
ktFiles: List<KtFile>,
|
|
||||||
) : BaseKtModuleByCompilerConfiguration(compilerConfig, project), KtSourceModule {
|
|
||||||
override val directRegularDependencies: List<KtBinaryModule> by lazy {
|
|
||||||
val libraryRoots = compilerConfig.jvmModularRoots + compilerConfig.jvmClasspathRoots
|
|
||||||
val libraryRootsByType = libraryRoots.groupBy { it.isDirectory }
|
|
||||||
buildList {
|
|
||||||
libraryRootsByType[true]?.let { directories ->
|
|
||||||
directories.forEach {
|
|
||||||
// E.g., project/app/build/intermediates/javac/debug/classes
|
|
||||||
val root = it.toPath()
|
|
||||||
add(KtLibraryModuleByCompilerConfiguration(compilerConfig, project, root))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
libraryRootsByType[false]?.let { jars ->
|
|
||||||
jars.forEach {
|
|
||||||
// E.g., project/libs/libA/a.jar
|
|
||||||
val root = it.toPath()
|
|
||||||
add(KtLibraryModuleByCompilerConfiguration(compilerConfig, project, root))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
compilerConfig.get(JVMConfigurationKeys.JDK_HOME)?.let { jdkHome ->
|
|
||||||
val vfm = VirtualFileManager.getInstance()
|
|
||||||
val jdkHomePath = jdkHome.toPath()
|
|
||||||
val jdkHomeVirtualFile = vfm.findFileByNioPath(jdkHomePath)
|
|
||||||
val binaryRoots = LibraryUtils.findClassesFromJdkHome(jdkHomePath).map {
|
|
||||||
Paths.get(URLUtil.extractPath(it))
|
|
||||||
}
|
|
||||||
add(KtSdkModuleByCompilerConfiguration(compilerConfig, project, "JDK", jdkHomeVirtualFile, binaryRoots))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override val contentScope: GlobalSearchScope by lazy {
|
|
||||||
TopDownAnalyzerFacadeForJVM.newModuleSearchScope(project, ktFiles)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class KtLibraryModuleByCompilerConfiguration(
|
|
||||||
compilerConfig: CompilerConfiguration,
|
|
||||||
project: Project,
|
|
||||||
root: Path,
|
|
||||||
) : BaseKtModuleByCompilerConfiguration(compilerConfig, project), KtLibraryModule {
|
|
||||||
override val directRegularDependencies: List<KtModule> get() = emptyList()
|
|
||||||
|
|
||||||
override val libraryName: String
|
|
||||||
get() = moduleName
|
|
||||||
|
|
||||||
override val librarySources: KtLibrarySourceModule?
|
|
||||||
get() = null
|
|
||||||
|
|
||||||
override val contentScope: GlobalSearchScope by lazy {
|
|
||||||
ProjectScope.getLibrariesScope(project)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val binaryRoots = listOf(root)
|
|
||||||
|
|
||||||
override fun getBinaryRoots(): Collection<Path> = binaryRoots
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class KtSdkModuleByCompilerConfiguration(
|
|
||||||
compilerConfig: CompilerConfiguration,
|
|
||||||
project: Project,
|
|
||||||
override val sdkName: String,
|
|
||||||
sdkHome: VirtualFile?,
|
|
||||||
private val binaryRoots: Collection<Path>
|
|
||||||
) : BaseKtModuleByCompilerConfiguration(compilerConfig, project), KtSdkModule {
|
|
||||||
override val directRegularDependencies: List<KtModule> get() = emptyList()
|
|
||||||
|
|
||||||
override val contentScope: GlobalSearchScope =
|
|
||||||
GlobalSearchScope.fileScope(project, sdkHome)
|
|
||||||
|
|
||||||
override fun getBinaryRoots(): Collection<Path> = binaryRoots
|
|
||||||
}
|
|
||||||
+83
-5
@@ -5,24 +5,100 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.analysis.project.structure.impl
|
package org.jetbrains.kotlin.analysis.project.structure.impl
|
||||||
|
|
||||||
|
import com.google.common.io.Files
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.openapi.vfs.VirtualFileManager
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
|
import com.intellij.psi.search.ProjectScope
|
||||||
|
import com.intellij.util.io.URLUtil
|
||||||
|
import org.jetbrains.kotlin.analysis.api.impl.base.util.LibraryUtils
|
||||||
import org.jetbrains.kotlin.analysis.project.structure.KtBinaryModule
|
import org.jetbrains.kotlin.analysis.project.structure.KtBinaryModule
|
||||||
import org.jetbrains.kotlin.analysis.project.structure.KtLibraryModule
|
import org.jetbrains.kotlin.analysis.project.structure.KtLibraryModule
|
||||||
import org.jetbrains.kotlin.analysis.project.structure.KtModule
|
import org.jetbrains.kotlin.analysis.project.structure.KtModule
|
||||||
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
|
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
|
||||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtLibraryModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtSdkModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtSourceModule
|
||||||
|
import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM
|
||||||
|
import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots
|
||||||
|
import org.jetbrains.kotlin.cli.jvm.config.jvmModularRoots
|
||||||
|
import org.jetbrains.kotlin.config.*
|
||||||
|
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||||
|
import org.jetbrains.kotlin.platform.jvm.JdkPlatform
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
|
import java.nio.file.Paths
|
||||||
|
|
||||||
internal class KtModuleProviderByCompilerConfiguration(
|
internal class KtModuleProviderByCompilerConfiguration(
|
||||||
compilerConfig: CompilerConfiguration,
|
compilerConfig: CompilerConfiguration,
|
||||||
project: Project,
|
project: Project,
|
||||||
ktFiles: List<KtFile>,
|
ktFiles: List<KtFile>,
|
||||||
) : ProjectStructureProvider() {
|
) : ProjectStructureProvider() {
|
||||||
private val sourceModule = KtSourceModuleByCompilerConfiguration(compilerConfig, project, ktFiles)
|
private val sourceModule = buildKtSourceModule {
|
||||||
|
val platform = TargetPlatform(setOf(JdkPlatform(JvmTarget.DEFAULT)))
|
||||||
|
val moduleName = compilerConfig.get(CommonConfigurationKeys.MODULE_NAME) ?: "<no module name provided>"
|
||||||
|
|
||||||
|
val libraryRoots = compilerConfig.jvmModularRoots + compilerConfig.jvmClasspathRoots
|
||||||
|
val libraryRootsByType = libraryRoots.groupBy { it.isDirectory }
|
||||||
|
libraryRootsByType[true]?.let { directories ->
|
||||||
|
directories.forEach {
|
||||||
|
// E.g., project/app/build/intermediates/javac/debug/classes
|
||||||
|
val root = it.toPath()
|
||||||
|
directRegularDependencies.add(
|
||||||
|
buildKtLibraryModule {
|
||||||
|
contentScope = ProjectScope.getLibrariesScope(project)
|
||||||
|
this.platform = platform
|
||||||
|
this.project = project
|
||||||
|
binaryRoots = listOf(root)
|
||||||
|
libraryName = "$moduleName-${root.toString().replace("/", "-")}"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
libraryRootsByType[false]?.let { jars ->
|
||||||
|
jars.forEach {
|
||||||
|
// E.g., project/libs/libA/a.jar
|
||||||
|
val root = it.toPath()
|
||||||
|
directRegularDependencies.add(
|
||||||
|
buildKtLibraryModule {
|
||||||
|
contentScope = ProjectScope.getLibrariesScope(project)
|
||||||
|
this.platform = platform
|
||||||
|
this.project = project
|
||||||
|
binaryRoots = listOf(root)
|
||||||
|
libraryName = Files.getNameWithoutExtension(root.toString())
|
||||||
|
isBuiltinsContainingStdlib =
|
||||||
|
libraryName.startsWith("kotlin-stdlib") &&
|
||||||
|
!libraryName.contains("common") && !libraryName.contains("jdk")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
compilerConfig.get(JVMConfigurationKeys.JDK_HOME)?.let { jdkHome ->
|
||||||
|
val vfm = VirtualFileManager.getInstance()
|
||||||
|
val jdkHomePath = jdkHome.toPath()
|
||||||
|
val jdkHomeVirtualFile = vfm.findFileByNioPath(jdkHomePath)
|
||||||
|
val binaryRoots = LibraryUtils.findClassesFromJdkHome(jdkHomePath).map {
|
||||||
|
Paths.get(URLUtil.extractPath(it))
|
||||||
|
}
|
||||||
|
directRegularDependencies.add(
|
||||||
|
buildKtSdkModule {
|
||||||
|
contentScope = GlobalSearchScope.fileScope(project, jdkHomeVirtualFile)
|
||||||
|
this.platform = platform
|
||||||
|
this.project = project
|
||||||
|
this.binaryRoots = binaryRoots
|
||||||
|
sdkName = "JDK for $moduleName"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
contentScope = TopDownAnalyzerFacadeForJVM.newModuleSearchScope(project, ktFiles)
|
||||||
|
this.platform = platform
|
||||||
|
this.project = project
|
||||||
|
this.moduleName = moduleName
|
||||||
|
}
|
||||||
|
|
||||||
private val binaryModules: Collection<KtBinaryModule> by lazy {
|
private val binaryModules: Collection<KtBinaryModule> by lazy {
|
||||||
sourceModule.directRegularDependencies
|
sourceModule.directRegularDependencies.filterIsInstance<KtBinaryModule>()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getKtModuleForKtElement(element: PsiElement): KtModule {
|
override fun getKtModuleForKtElement(element: PsiElement): KtModule {
|
||||||
@@ -30,7 +106,7 @@ internal class KtModuleProviderByCompilerConfiguration(
|
|||||||
return if (containingFile in sourceModule.contentScope) {
|
return if (containingFile in sourceModule.contentScope) {
|
||||||
sourceModule
|
sourceModule
|
||||||
} else {
|
} else {
|
||||||
sourceModule.directRegularDependencies.find { libModule -> containingFile in libModule.contentScope }
|
binaryModules.find { libModule -> containingFile in libModule.contentScope }
|
||||||
?: error("Can't find module for ${containingFile.path}")
|
?: error("Can't find module for ${containingFile.path}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -40,6 +116,8 @@ internal class KtModuleProviderByCompilerConfiguration(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getStdlibWithBuiltinsModule(module: KtModule): KtLibraryModule? {
|
override fun getStdlibWithBuiltinsModule(module: KtModule): KtLibraryModule? {
|
||||||
TODO("Not yet implemented")
|
return binaryModules
|
||||||
|
.filterIsInstance<KtLibraryModuleImpl>()
|
||||||
|
.firstOrNull { it.isBuiltinsContainingStdlib }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.impl
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices
|
||||||
|
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
|
||||||
|
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||||
|
import org.jetbrains.kotlin.platform.isCommon
|
||||||
|
import org.jetbrains.kotlin.platform.js.isJs
|
||||||
|
import org.jetbrains.kotlin.platform.jvm.isJvm
|
||||||
|
import org.jetbrains.kotlin.platform.konan.isNative
|
||||||
|
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
|
||||||
|
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices
|
||||||
|
import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformAnalyzerServices
|
||||||
|
|
||||||
|
internal fun TargetPlatform.getAnalyzerServices(): PlatformDependentAnalyzerServices {
|
||||||
|
return when {
|
||||||
|
isJvm() -> JvmPlatformAnalyzerServices
|
||||||
|
isJs() -> JsPlatformAnalyzerServices
|
||||||
|
isNative() -> NativePlatformAnalyzerServices
|
||||||
|
isCommon() -> CommonPlatformAnalyzerServices
|
||||||
|
else -> error("Unknown target platform: $this")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.impl
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||||
|
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
|
||||||
|
|
||||||
|
internal interface KtModuleWithPlatform {
|
||||||
|
val platform: TargetPlatform
|
||||||
|
|
||||||
|
val analyzerServices: PlatformDependentAnalyzerServices
|
||||||
|
get() = platform.getAnalyzerServices()
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.impl
|
||||||
|
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtSdkModule
|
||||||
|
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||||
|
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
|
||||||
|
import java.nio.file.Path
|
||||||
|
|
||||||
|
internal class KtSdkModuleImpl(
|
||||||
|
override val directRegularDependencies: List<KtModule>,
|
||||||
|
override val directRefinementDependencies: List<KtModule>,
|
||||||
|
override val directFriendDependencies: List<KtModule>,
|
||||||
|
override val contentScope: GlobalSearchScope,
|
||||||
|
override val platform: TargetPlatform,
|
||||||
|
override val project: Project,
|
||||||
|
private val binaryRoots: Collection<Path>,
|
||||||
|
override val sdkName: String,
|
||||||
|
) : KtSdkModule, KtModuleWithPlatform {
|
||||||
|
override val analyzerServices: PlatformDependentAnalyzerServices = super.analyzerServices
|
||||||
|
|
||||||
|
override fun getBinaryRoots(): Collection<Path> = binaryRoots
|
||||||
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2022 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.project.structure.impl
|
||||||
|
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtModule
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule
|
||||||
|
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||||
|
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||||
|
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
|
||||||
|
|
||||||
|
internal class KtSourceModuleImpl(
|
||||||
|
override val directRegularDependencies: List<KtModule>,
|
||||||
|
override val directRefinementDependencies: List<KtModule>,
|
||||||
|
override val directFriendDependencies: List<KtModule>,
|
||||||
|
override val contentScope: GlobalSearchScope,
|
||||||
|
override val platform: TargetPlatform,
|
||||||
|
override val project: Project,
|
||||||
|
override val moduleName: String,
|
||||||
|
override val languageVersionSettings: LanguageVersionSettings,
|
||||||
|
) : KtSourceModule, KtModuleWithPlatform {
|
||||||
|
override val analyzerServices: PlatformDependentAnalyzerServices = super.analyzerServices
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user