diff --git a/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidConst.kt b/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidConst.kt index 160c87fdf2a..abb79aee113 100644 --- a/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidConst.kt +++ b/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidConst.kt @@ -17,6 +17,8 @@ package org.jetbrains.kotlin.lang.resolve.android import com.intellij.openapi.util.Key +import org.jetbrains.kotlin.lexer.JetKeywordToken +import org.jetbrains.kotlin.lexer.JetTokens public object AndroidConst { val ANDROID_USER_PACKAGE: Key = Key.create("ANDROID_USER_PACKAGE") @@ -35,14 +37,19 @@ public object AndroidConst { val CLEAR_FUNCTION_NAME = "clearFindViewByIdCache" val IGNORED_XML_WIDGET_TYPES = setOf("requestFocus", "merge", "tag", "check") + + val ESCAPED_IDENTIFIERS = JetTokens.KEYWORDS.getTypes() + .map { it as? JetKeywordToken }.filterNotNull().map { it.getValue() }.toSet() } public fun nameToIdDeclaration(name: String): String = AndroidConst.ID_DECLARATION_PREFIX + name public fun idToName(id: String): String? { - return if (isResourceIdDeclaration(id)) id.replace(AndroidConst.ID_DECLARATION_PREFIX, "") - else if (isResourceIdUsage(id)) id.replace(AndroidConst.ID_USAGE_PREFIX, "") - else null + val unescaped = + if (isResourceIdDeclaration(id)) id.replace(AndroidConst.ID_DECLARATION_PREFIX, "") + else if (isResourceIdUsage(id)) id.replace(AndroidConst.ID_USAGE_PREFIX, "") + else null + return if (unescaped != null) escapeWidgetId(unescaped) else null } public fun isResourceIdDeclaration(str: String?): Boolean = str?.startsWith(AndroidConst.ID_DECLARATION_PREFIX) ?: false @@ -58,4 +65,8 @@ public fun isWidgetTypeIgnored(xmlType: String): Boolean { public fun getRealWidgetType(xmlType: String): String = when (xmlType) { "fragment", "include" -> "View" else -> xmlType +} + +public fun escapeWidgetId(id: String): String { + return if (id in AndroidConst.ESCAPED_IDENTIFIERS) "`$id`" else id } \ No newline at end of file diff --git a/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/layout.kt b/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/layout.kt index f0a0fe45c1c..8154a1f4269 100644 --- a/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/layout.kt +++ b/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/layout.kt @@ -18,3 +18,9 @@ val Activity.fragmentTag: ft val Fragment.fragmentTag: ft get() = getView().findViewById(0) : View +val Activity.`fun`: ft + get() = findViewById(0) as TextView + +val Fragment.`fun`: ft + get() = getView().findViewById(0) as TextView + diff --git a/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/layout1.kt b/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/layout1.kt index f9efcc0df30..879f5e3ac58 100644 --- a/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/layout1.kt +++ b/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/layout1.kt @@ -12,3 +12,6 @@ val View.includeTag: ft val View.fragmentTag: ft get() = findViewById(0) : View +val View.`fun`: ft + get() = findViewById(0) as TextView + diff --git a/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/res/layout/layout.xml b/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/res/layout/layout.xml index 0198b69b328..b5448700338 100644 --- a/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/res/layout/layout.xml +++ b/plugins/android-compiler-plugin/testData/android/converter/simple/specialTags/res/layout/layout.xml @@ -16,4 +16,9 @@ android:layout_height="wrap_content" android:layout_width="wrap_content" /> + + \ No newline at end of file