Skip to content

Commit

Permalink
Use the modifiers declared in the type (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
serras authored Mar 23, 2023
1 parent 474cd1f commit 2800bc0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions kopykat-ksp/src/main/kotlin/at/kopyk/CopyMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import at.kopyk.utils.lang.mapRun
import at.kopyk.utils.lang.onEachRun
import at.kopyk.utils.typeCategory
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.ksp.toKModifier

internal val TypeCompileScope.copyMapFunctionKt: FileSpec
get() = buildFile(fileName = target.append("CopyMap").reflectionName()) {
val parameterized = target.parameterized
addGeneratedMarker()
addInlinedFunction(name = "copyMap", receives = parameterized, returns = parameterized) {
visibility.toKModifier()?.let { addModifiers(it) }
properties
.onEachRun {
addParameter(
Expand Down
9 changes: 8 additions & 1 deletion kopykat-ksp/src/main/kotlin/at/kopyk/MutableCopy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import at.kopyk.utils.typeCategory
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ksp.toKModifier

internal val TypeCompileScope.mutableCopyKt: FileSpec
get() = buildFile(target.mutable.reflectionName()) {
Expand All @@ -46,6 +47,7 @@ internal val TypeCompileScope.mutableCopyKt: FileSpec
internal fun FileCompilerScope.addMutableCopy() {
with(element) {
file.addClass(target.mutable) {
visibility.toKModifier()?.let { addModifiers(it) }
addAnnotation(target.dslMarker)
addTypeVariables(typeVariableNames.map { it.makeInvariant() })
primaryConstructor {
Expand Down Expand Up @@ -75,6 +77,7 @@ internal fun FileCompilerScope.addFreezeFunction() {
with(element) {
onKnownCategory { category ->
addFunction(name = "freeze", receives = target.mutable.parameterized, returns = target.parameterized) {
visibility.toKModifier()?.let { addModifiers(it) }
addReturn(
when (category) {
Known.Class -> error("Plain classes are not supported as mutable")
Expand All @@ -93,6 +96,7 @@ internal fun FileCompilerScope.addToMutateFunction() {
with(element) {
val parameterized = target.mutable.parameterized
addFunction(name = "toMutable", receives = target.parameterized, returns = parameterized) {
visibility.toKModifier()?.let { addModifiers(it) }
addReturn("$parameterized(old = this, ${mutationInfo.joinAsAssignmentsWithMutation { toMutable(it) }})")
}
}
Expand Down Expand Up @@ -123,7 +127,10 @@ private fun FileCompilerScope.addRetrofittedCopyFunction() {

internal fun FileCompilerScope.addCopyFunction(block: FunSpec.Builder.() -> Unit) {
with(element) {
addInlinedFunction(name = "copy", receives = target.parameterized, returns = target.parameterized, block = block)
addInlinedFunction(name = "copy", receives = target.parameterized, returns = target.parameterized) {
visibility.toKModifier()?.let { addModifiers(it) }
block()
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions kopykat-ksp/src/main/kotlin/at/kopyk/utils/TypeCompileScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import at.kopyk.poet.parameterizedWhenNotEmpty
import at.kopyk.utils.lang.orElse
import at.kopyk.utils.lang.takeIfInstanceOf
import com.google.devtools.ksp.closestClassDeclaration
import com.google.devtools.ksp.getVisibility
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeAlias
import com.google.devtools.ksp.symbol.KSTypeReference
import com.google.devtools.ksp.symbol.Visibility
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
Expand All @@ -38,6 +40,7 @@ internal data class MutationInfo<out T : TypeName>(
internal sealed interface TypeCompileScope : KSDeclaration, LoggerScope {
val typeVariableNames: List<TypeVariableName>
val typeParameterResolver: TypeParameterResolver
val visibility: Visibility
val target: ClassName
val sealedTypes: Sequence<KSClassDeclaration>
val properties: Sequence<KSPropertyDeclaration>
Expand Down Expand Up @@ -80,6 +83,7 @@ internal class ClassCompileScope(
override val typeParameterResolver: TypeParameterResolver
get() = classDeclaration.typeParameters.toTypeParameterResolver().invariant()

override val visibility: Visibility = classDeclaration.getVisibility()
override val target: ClassName = classDeclaration.className
override val sealedTypes: Sequence<KSClassDeclaration> = classDeclaration.sealedTypes
override val properties: Sequence<KSPropertyDeclaration> = classDeclaration.getPrimaryConstructorProperties()
Expand Down Expand Up @@ -117,6 +121,12 @@ internal class TypeAliasCompileScope(
override val typeParameterResolver: TypeParameterResolver
get() = aliasDeclaration.typeParameters.toTypeParameterResolver().invariant()

override val visibility: Visibility
get() {
val ultimate = aliasDeclaration.ultimateDeclaration?.getVisibility() ?: Visibility.PUBLIC
return listOf(aliasDeclaration.getVisibility(), ultimate).minimal()
}

override val target: ClassName = aliasDeclaration.className
override val sealedTypes: Sequence<KSClassDeclaration> =
aliasDeclaration.ultimateDeclaration?.sealedTypes.orEmpty()
Expand Down
11 changes: 11 additions & 0 deletions kopykat-ksp/src/test/kotlin/at/kopyk/MutableCopyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ class MutableCopyTest {
""".evals("r" to 2)
}

@Test
fun `mutate one property, internal`() {
"""
|internal data class Person(val name: String, val age: Int)
|
|internal val p1 = Person("Alex", 1)
|internal val p2 = p1.copy { age = age + 1 }
|val r = p2.age
""".evals("r" to 2)
}

@Test
fun `weird package name, issue #78`() {
"""
Expand Down

0 comments on commit 2800bc0

Please sign in to comment.