Skip to content

Commit

Permalink
Fix KtLibraryModuleBuilder's content scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ting-yuan committed Nov 7, 2023
1 parent bc203be commit 24110ac
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -190,7 +190,7 @@ class KotlinSymbolProcessing(
}
}

buildKtIncrementalSourceModule {
buildKspSourceModule {
configLanguageVersionSettings?.let { this.languageVersionSettings = it }
this.platform = platform
this.moduleName = compilerConfig.get(CommonConfigurationKeys.MODULE_NAME) ?: "<no module name provided>"
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

0 comments on commit 24110ac

Please sign in to comment.