Android Extensions: Support '.', '-' and ':' in View identifiers (KT-22700)

This commit is contained in:
Yan Zhulanow
2018-02-07 22:44:14 +03:00
parent ba61a93dc7
commit 510d5ece0f
3 changed files with 27 additions and 2 deletions
@@ -32,7 +32,7 @@ object AndroidConst {
val ID_ATTRIBUTE_NO_NAMESPACE: String = "id"
val CLASS_ATTRIBUTE_NO_NAMESPACE: String = "class"
private val IDENTIFIER_WORD_REGEX = "[(?:\\p{L}\\p{M}*)0-9_\\.]+"
private val IDENTIFIER_WORD_REGEX = "[(?:\\p{L}\\p{M}*)0-9_\\.\\:\\-]+"
val IDENTIFIER_REGEX = "^@(\\+)?(($IDENTIFIER_WORD_REGEX)\\:)?id\\/($IDENTIFIER_WORD_REGEX)$".toRegex()
val CLEAR_FUNCTION_NAME = "clearFindViewByIdCache"
@@ -58,7 +58,21 @@ object AndroidConst {
fun androidIdToName(id: String): ResourceIdentifier? {
val values = AndroidConst.IDENTIFIER_REGEX.matchEntire(id)?.groupValues ?: return null
val packageName = values[3]
return ResourceIdentifier(values[4], if (packageName.isEmpty()) null else packageName)
return ResourceIdentifier(
getJavaIdentifierNameForResourceName(values[4]),
if (packageName.isEmpty()) null else packageName
)
}
// See also AndroidResourceUtil#getFieldNameByResourceName()
fun getJavaIdentifierNameForResourceName(styleName: String) = buildString {
for (char in styleName) {
when (char) {
'.', '-', ':' -> append('_')
else -> append(char)
}
}
}
fun isWidgetTypeIgnored(xmlType: String): Boolean {
@@ -21,4 +21,10 @@
android:layout_height="wrap_content"
android:text="Sign in" />
<Button
android:id="@+id/log_i.n"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign in" />
</FrameLayout>
@@ -17,6 +17,10 @@ kotlinx.android.synthetic.main
kotlinx.android.synthetic.main.test
public val android.app.Activity.log_i_n: android.widget.Button!
public val android.app.Dialog.log_i_n: android.widget.Button!
public val android.app.Fragment.log_i_n: android.widget.Button!
public val kotlinx.android.extensions.LayoutContainer.log_i_n: android.widget.Button!
public val android.app.Activity.sonuç: android.widget.TextView!
public val android.app.Dialog.sonuç: android.widget.TextView!
public val android.app.Fragment.sonuç: android.widget.TextView!
@@ -33,6 +37,7 @@ kotlinx.android.synthetic.main.test
kotlinx.android.synthetic.main.test.view
public val android.view.View.log_i_n: android.widget.Button!
public val android.view.View.sonuç: android.widget.TextView!
public val android.view.View.привет: android.widget.EditText!
public val android.view.View.こんにちは: android.widget.Button!