diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt index bf1e41d0cd..4324210717 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt @@ -30,7 +30,8 @@ import com.google.devtools.ksp.impl.symbol.kotlin.analyze import com.google.devtools.ksp.processing.* import com.google.devtools.ksp.processing.impl.CodeGeneratorImpl import com.google.devtools.ksp.processing.impl.JvmPlatformInfoImpl -import com.google.devtools.ksp.standalone.buildKtIncrementalSourceModule +import com.google.devtools.ksp.standalone.buildKspLibraryModule +import com.google.devtools.ksp.standalone.buildKspSourceModule import com.google.devtools.ksp.symbol.KSFile import com.google.devtools.ksp.symbol.KSNode import com.google.devtools.ksp.symbol.Origin @@ -56,7 +57,6 @@ import com.intellij.psi.impl.file.impl.JavaFileManager import com.intellij.psi.search.DelegatingGlobalSearchScope import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.search.ProjectScope -import com.intellij.util.io.URLUtil import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals import org.jetbrains.kotlin.analysis.api.impl.base.util.LibraryUtils import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeTokenProvider @@ -173,7 +173,7 @@ class KotlinSymbolProcessing( fun KtModuleBuilder.addModuleDependencies(moduleName: String) { val libraryRoots = compilerConfig.jvmModularRoots + compilerConfig.jvmClasspathRoots addRegularDependency( - buildKtLibraryModule { + buildKspLibraryModule { this.platform = platform addBinaryRoots(libraryRoots.map { it.toPath() }) libraryName = "Library for $moduleName" @@ -190,7 +190,7 @@ class KotlinSymbolProcessing( } } - buildKtIncrementalSourceModule { + buildKspSourceModule { configLanguageVersionSettings?.let { this.languageVersionSettings = it } this.platform = platform this.moduleName = compilerConfig.get(CommonConfigurationKeys.MODULE_NAME) ?: "" diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspLibraryModuleBuilder.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspLibraryModuleBuilder.kt new file mode 100644 index 0000000000..a372bf9f2b --- /dev/null +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspLibraryModuleBuilder.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2023 Google LLC + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") +package com.google.devtools.ksp.standalone + +import com.intellij.psi.search.ProjectScope +import org.jetbrains.kotlin.analysis.project.structure.KtLibraryModule +import org.jetbrains.kotlin.analysis.project.structure.KtLibrarySourceModule +import org.jetbrains.kotlin.analysis.project.structure.builder.KtBinaryModuleBuilder +import org.jetbrains.kotlin.analysis.project.structure.builder.KtModuleBuilderDsl +import org.jetbrains.kotlin.analysis.project.structure.builder.KtModuleProviderBuilder +import org.jetbrains.kotlin.analysis.project.structure.impl.KtLibraryModuleImpl +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreProjectEnvironment +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract + +@KtModuleBuilderDsl +class KspLibraryModuleBuilder( + private val kotlinCoreProjectEnvironment: KotlinCoreProjectEnvironment +) : KtBinaryModuleBuilder() { + public lateinit var libraryName: String + public var librarySources: KtLibrarySourceModule? = null + + override fun build(): KtLibraryModule { + val binaryRoots = getBinaryRoots() + val contentScope = ProjectScope.getLibrariesScope(kotlinCoreProjectEnvironment.project) + return KtLibraryModuleImpl( + directRegularDependencies, + directDependsOnDependencies, + directFriendDependencies, + contentScope, + platform, + kotlinCoreProjectEnvironment.project, + binaryRoots, + libraryName, + librarySources, + ) + } +} + +@OptIn(ExperimentalContracts::class) +inline fun KtModuleProviderBuilder.buildKspLibraryModule(init: KspLibraryModuleBuilder.() -> Unit): KtLibraryModule { + contract { + callsInPlace(init, InvocationKind.EXACTLY_ONCE) + } + return KspLibraryModuleBuilder(kotlinCoreProjectEnvironment).apply(init).build() +} diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KtIncrementalSourceModuleBuilder.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspSourceModuleBuilder.kt similarity index 94% rename from kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KtIncrementalSourceModuleBuilder.kt rename to kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspSourceModuleBuilder.kt index 36b1d941a2..af20fbc3ef 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KtIncrementalSourceModuleBuilder.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/standalone/KspSourceModuleBuilder.kt @@ -41,7 +41,7 @@ import kotlin.io.path.isDirectory import kotlin.io.path.pathString @KtModuleBuilderDsl -class KtIncrementalSourceModuleBuilder( +class KspModuleBuilder( private val kotlinCoreProjectEnvironment: KotlinCoreProjectEnvironment, ) : KtModuleBuilder() { public lateinit var moduleName: String @@ -98,11 +98,11 @@ class KtIncrementalSourceModuleBuilder( } @OptIn(ExperimentalContracts::class) -public inline fun KtModuleProviderBuilder.buildKtIncrementalSourceModule( - init: KtIncrementalSourceModuleBuilder.() -> Unit +public inline fun KtModuleProviderBuilder.buildKspSourceModule( + init: KspModuleBuilder.() -> Unit ): KtSourceModule { contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) } - return KtIncrementalSourceModuleBuilder(kotlinCoreProjectEnvironment).apply(init).build() + return KspModuleBuilder(kotlinCoreProjectEnvironment).apply(init).build() }