Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix imports and keywords in names #93

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions docs/mutable-utils/styles/jetbrains-mono.css

This file was deleted.

7 changes: 4 additions & 3 deletions kopykat-ksp/src/main/kotlin/at/kopyk/CopyConstructors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import at.kopyk.utils.fullName
import at.kopyk.utils.getPrimaryConstructorProperties
import at.kopyk.utils.lang.takeIfInstanceOf
import at.kopyk.utils.minimal
import at.kopyk.utils.sanitizedName
import com.google.devtools.ksp.getVisibility
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSAnnotation
Expand Down Expand Up @@ -75,7 +76,7 @@ private fun TypeCompileScope.copyConstructorFileSpec(
to.properties.toList().zipByName(from.getAllProperties().toList()) { to, from ->
propertyDefinition(others, from, to)
}.filterNotNull()
addReturn("${to.baseName}(${propertyAssignments.joinToString()})")
addReturn("${to.sanitizedName}(${propertyAssignments.joinToString()})")
}
}
}
Expand All @@ -94,10 +95,10 @@ private fun propertyDefinition(
): String? {
val fromType = from.typeDeclaration
val toType = to.typeDeclaration
val propertyName = from.baseName
val propertyName = from.sanitizedName
return when {
fromType == null || toType == null -> null
others.hasCopyConstructor(fromType, toType) -> "$propertyName = ${toType.baseName}(from.$propertyName)"
others.hasCopyConstructor(fromType, toType) -> "$propertyName = ${toType.sanitizedName}(from.$propertyName)"
else -> "$propertyName = from.$propertyName"
}
}
Expand Down
3 changes: 2 additions & 1 deletion kopykat-ksp/src/main/kotlin/at/kopyk/CopyMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import at.kopyk.utils.fullName
import at.kopyk.utils.lang.joinWithWhen
import at.kopyk.utils.lang.mapRun
import at.kopyk.utils.lang.onEachRun
import at.kopyk.utils.sanitizedName
import at.kopyk.utils.typeCategory
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.ksp.toKModifier
Expand All @@ -33,7 +34,7 @@ internal val TypeCompileScope.copyMapFunctionKt: FileSpec
defaultValue = "{ it }",
)
}
.mapRun { "$baseName = $baseName(this, this.$baseName)" }
.mapRun { "$sanitizedName = $sanitizedName(this, this.$sanitizedName)" }
.run { addReturn(repeatOnSubclasses(joinToString(), "copy")) }
}
}
Expand Down
34 changes: 32 additions & 2 deletions kopykat-ksp/src/main/kotlin/at/kopyk/MutableCopy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ import at.kopyk.utils.lang.forEachRun
import at.kopyk.utils.lang.joinWithWhen
import at.kopyk.utils.mutable
import at.kopyk.utils.onKnownCategory
import at.kopyk.utils.sanitizedName
import at.kopyk.utils.typeCategory
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.ksp.toKModifier

internal val TypeCompileScope.mutableCopyKt: FileSpec
get() =
buildFile(target.mutable.reflectionName()) {
addImports()
addGeneratedMarker()
addDslMarkerClass()
addMutableCopy()
Expand All @@ -45,6 +50,31 @@ internal val TypeCompileScope.mutableCopyKt: FileSpec
}
}

internal fun FileCompilerScope.addImports() {
with(element) {
val targetPkg = target.packageName
mutationInfo.forEach { (declaration, mutationInfo) ->
if (declaration.type.resolve().hasMutableCopy()) {
mutationInfo.className.usedPackages.forEach { propertyPkg ->
if (propertyPkg != targetPkg) {
file.addImport(propertyPkg, "freeze", "toMutable")
}
}
}
}
}
}

@Suppress("RecursivePropertyAccessor")
internal val TypeName.usedPackages: Set<String>
get() =
when (this) {
is ClassName -> setOf(this.packageName)
is ParameterizedTypeName ->
this.rawType.usedPackages + this.typeArguments.flatMap { it.usedPackages }
else -> emptySet()
}

internal fun FileCompilerScope.addMutableCopy() {
with(element) {
file.addClass(target.mutable) {
Expand Down Expand Up @@ -117,10 +147,10 @@ internal fun FileCompilerScope.addCopyClosure() {
private fun FileCompilerScope.addRetrofittedCopyFunction() {
with(element) {
addCopyFunction {
properties.forEachRun { addParameter(name = baseName, type = typeName, defaultValue = "this.$baseName") }
properties.forEachRun { addParameter(name = baseName, type = typeName, defaultValue = "this.$sanitizedName") }
addReturn(
sealedTypes.joinWithWhen { type ->
"is ${type.fullName} -> this.copy(${properties.joinToString { "${it.baseName} = ${it.baseName}" }})"
"is ${type.fullName} -> this.copy(${properties.joinToString { "${it.sanitizedName} = ${it.sanitizedName}" }})"
},
)
}
Expand Down
12 changes: 9 additions & 3 deletions kopykat-ksp/src/main/kotlin/at/kopyk/utils/KspExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import com.google.devtools.ksp.symbol.KSDeclarationContainer
import com.google.devtools.ksp.symbol.KSPropertyDeclaration

internal val KSDeclaration.baseName: String
get() = simpleName.asString().sanitize()
get() = simpleName.asString()

internal val KSDeclaration.sanitizedName: String
get() = baseName.sanitize()

internal val KSDeclaration.fullName: String
get() = qualifiedName?.asString() ?: simpleName.asString()
Expand Down Expand Up @@ -36,10 +39,13 @@ private fun KSClassDeclaration.hasPrimaryProperty(property: KSPropertyDeclaratio
/**
* Sanitizes each delimited section if it matches with Kotlin reserved keywords.
*/
private fun String.sanitize(
internal fun String.sanitize(
delimiter: String = ".",
prefix: String = "",
) = splitToSequence(delimiter).joinToString(delimiter, prefix) { if (it in KOTLIN_KEYWORDS) "`$it`" else it }
): String =
splitToSequence(delimiter).joinToString(delimiter, prefix) {
if (it in KOTLIN_KEYWORDS) "`$it`" else it
}

private val KOTLIN_KEYWORDS =
setOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal sealed interface TypeCompileScope : KSDeclaration, LoggerScope {
fun KSPropertyDeclaration.toAssignment(
wrapper: (String) -> String,
source: String? = null,
): String = "$baseName = ${wrapper("${source ?: ""}$baseName")}"
): String = "$sanitizedName = ${wrapper("${source ?: ""}$sanitizedName")}"

fun Sequence<Pair<KSPropertyDeclaration, MutationInfo<TypeName>>>.joinAsAssignmentsWithMutation(
wrapper: MutationInfo<TypeName>.(String) -> String,
Expand Down
11 changes: 11 additions & 0 deletions kopykat-ksp/src/test/kotlin/at/kopyk/CopyMapTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,15 @@ class CopyMapTest {
|val r = p2.second
""".evals("r" to 2)
}

@Test
fun `keyword as fields, issue #93`() {
"""
|data class Person(val `in`: String, val `out`: Int)
|
|val p1 = Person("Alex", 1)
|val p2 = p1.copyMap(`out` = { it + 1 })
|val r = p2.`out`
""".evals("r" to 2)
}
}
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 @@ -224,4 +224,15 @@ class MutableCopyTest {
|val r = p2.d
""".evals("r" to listOf("e", "f"))
}

@Test
fun `keyword as fields, issue #93`() {
"""
|data class Person(val `in`: String, val `out`: Int)
|
|val p1 = Person("Alex", 1)
|val p2 = p1.copy { `out` = `out` + 1 }
|val r = p2.`out`
""".evals("r" to 2)
}
}