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

proposal to update generated function names #265

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
64 changes: 5 additions & 59 deletions compiler/src/main/kotlin/motif/compiler/Names.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package motif.compiler

import androidx.room.compiler.processing.XAnnotation
import androidx.room.compiler.processing.XType
import com.google.common.annotations.VisibleForTesting
import java.util.Locale
import motif.ast.compiler.CompilerAnnotation
import motif.ast.compiler.CompilerType
import motif.models.Type
Expand Down Expand Up @@ -49,14 +51,11 @@ private class UniqueNameSet(blacklist: Iterable<String>) {
object Names {

@JvmStatic
@VisibleForTesting
fun safeName(typeMirror: XType, annotation: XAnnotation?): String {
var name = XNameVisitor.visit(typeMirror)
val name = XNameVisitor.visit(typeMirror)
val annotationString = annotationString(annotation)
name = "$annotationString$name".decapitalize()
if (name in KEYWORDS) {
name += "_"
}
return name
return "$annotationString${name}_".replaceFirstChar { it.lowercase(Locale.ENGLISH) }
}

private fun annotationString(annotation: XAnnotation?): String {
Expand All @@ -67,56 +66,3 @@ object Names {
}
}
}

private val KEYWORDS =
setOf(
"abstract",
"continue",
"for",
"new",
"switch",
"assert",
"default",
"goto",
"package",
"synchronized",
"boolean",
"do",
"if",
"private",
"this",
"break",
"double",
"implements",
"protected",
"throw",
"byte",
"else",
"import",
"public",
"throws",
"case",
"enum",
"instanceof",
"return",
"transient",
"catch",
"extends",
"int",
"short",
"try",
"char",
"final",
"interface",
"static",
"void",
"class",
"finally",
"long",
"strictfp",
"volatile",
"const",
"float",
"native",
"super",
"while")
32 changes: 17 additions & 15 deletions compiler/src/test/java/motif/compiler/NamesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
@OptIn(ExperimentalProcessingApi::class)
class XNamesTest(private val processorType: ProcessorType, private val srcLang: SourceLanguage) {
class NamesTest(private val processorType: ProcessorType, private val srcLang: SourceLanguage) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching the file name


companion object {
@JvmStatic
Expand All @@ -49,64 +49,66 @@ class XNamesTest(private val processorType: ProcessorType, private val srcLang:

@Test
fun primitive() {
assertSafeName("int", "kotlin.Int", "integer")
assertSafeName("int", "kotlin.Int", "integer_")
}

@Test
fun boxed() {
assertSafeName("java.lang.Integer", "kotlin.Int?", "integer")
assertSafeName("java.lang.Integer", "kotlin.Int?", "integer_")
}

@Test
fun raw() {
assertSafeName("java.util.HashMap", "java.util.HashMap<*, *>", "hashMap")
assertSafeName("java.util.HashMap", "java.util.HashMap<*, *>", "hashMap_")
}

@Test
fun typeArgument() {
assertSafeName(
"java.util.HashMap<String, Integer>",
"java.util.HashMap<String, Integer>",
"stringIntegerHashMap")
"stringIntegerHashMap_")
}

@Test
fun wildcard() {
assertSafeName(
"java.util.HashMap<? extends String, ? super Integer>",
"java.util.HashMap<out String, out Integer>",
"stringIntegerHashMap")
"stringIntegerHashMap_")
}

@Test
fun wildcardUnbounded() {
assertSafeName("java.util.HashMap<?, ?>", "java.util.HashMap<*, *>", "hashMap")
assertSafeName("java.util.HashMap<?, ?>", "java.util.HashMap<*, *>", "hashMap_")
}

@Test
fun typeVariable() {
assertSafeName("java.util.HashMap<String, A>", "java.util.HashMap<String, A>", "stringAHashMap")
assertSafeName(
"java.util.HashMap<String, A>", "java.util.HashMap<String, A>", "stringAHashMap_")
}

@Test
fun typeVariableUnbounded() {
assertSafeName("java.util.HashMap<String, B>", "java.util.HashMap<String, B>", "stringBHashMap")
assertSafeName(
"java.util.HashMap<String, B>", "java.util.HashMap<String, B>", "stringBHashMap_")
}

@Test
fun nested() {
assertSafeName(
"java.util.HashMap<java.util.HashMap<String, Integer>, Integer>",
"java.util.HashMap<java.util.HashMap<String, Integer>, Integer>",
"stringIntegerHashMapIntegerHashMap")
"stringIntegerHashMapIntegerHashMap_")
}

@Test
fun innerClass() {
assertSafeName(
"java.util.Map.Entry<String, Integer>",
"java.util.Map.Entry<String, Integer>",
"stringIntegerMapEntry")
"stringIntegerMapEntry_")
}

@Test
Expand All @@ -116,22 +118,22 @@ class XNamesTest(private val processorType: ProcessorType, private val srcLang:

@Test
fun named() {
assertSafeName("String", "String", "fooString", "@javax.inject.Named(\"Foo\")")
assertSafeName("String", "String", "fooString_", "@javax.inject.Named(\"Foo\")")
}

@Test
fun customQualifier() {
assertSafeName("String", "String", "fooString", qualifierString = "@Foo")
assertSafeName("String", "String", "fooString_", qualifierString = "@Foo")
}

@Test
fun array() {
assertSafeName("String[]", "Array<String>", "stringArray")
assertSafeName("String[]", "Array<String>", "stringArray_")
}

@Test
fun enum() {
assertSafeName("LogLevel", "LogLevel", "logLevel")
assertSafeName("LogLevel", "LogLevel", "logLevel_")
}

@Test
Expand Down
1 change: 1 addition & 0 deletions intellij/src/test/kotlin/motif/intellij/TestHarness.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class TestHarness : LightJavaCodeInsightFixtureTestCase() {
addLibrary(Inject::class)
addLibrary(Scope::class)
addLibrary(Nullable::class)
addLibrary(JvmSuppressWildcards::class)
}

private fun addLibrary(clazz: KClass<*>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
---- String | Objects.string ----
[ Required ]
[ Consumed By ]
* Scope | Scope.string()
* Scope | Scope.string_()

---- Scope | implicit ----
[ Required ]
Expand Down
7 changes: 1 addition & 6 deletions viewmodel/src/main/kotlin/motif/viewmodel/TestRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,5 @@ object TestRenderer {

/** HACK: Map kotlin types to Java for graph validation (issue when KSP processes Java sources) */
private fun String.toJvmSimpleName(): String {
return when (this) {
"Int" -> "int"
"Boolean" -> "boolean"
"Byte" -> "byte"
else -> this
}
return this.replace(", ", ",")
}