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 {