fix double renaming issue
- rebuild resource mapping cache on external layout file changes - move duplicated code to AndroidResourceManager
This commit is contained in:
committed by
Yan Zhulanow
parent
1b50b226ae
commit
b5e267ed56
+4
@@ -21,8 +21,12 @@ import com.intellij.psi.PsiElement
|
||||
import com.intellij.openapi.project.Project
|
||||
|
||||
abstract class AndroidResourceManager(val project: Project, val searchPath: String?) {
|
||||
private val idPrefix = "@+id/"
|
||||
abstract fun getLayoutXmlFiles(): Collection<PsiFile>
|
||||
abstract fun idToXmlAttribute(id: String): PsiElement?
|
||||
abstract fun renameXmlAttr(elem: PsiElement, newName: String)
|
||||
abstract fun renameProperty(oldName: String, newName: String)
|
||||
public fun nameToId(name: String): String = idPrefix + name
|
||||
public fun idToName(id: String): String = id.replace(idPrefix, "")
|
||||
public fun isResourceId(str: String?): Boolean = str?.startsWith(idPrefix) ?: false
|
||||
}
|
||||
|
||||
@@ -52,24 +52,8 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() {
|
||||
val processor = ServiceManager.getService(jetProperty.getProject(), javaClass<AndroidUIXmlProcessor>())
|
||||
val resourceManager = processor!!.resourceManager
|
||||
val attr = resourceManager.idToXmlAttribute(oldName) as XmlAttribute
|
||||
for (file in resourceManager.getLayoutXmlFiles()) {
|
||||
if (file is XmlFile) {
|
||||
file.accept(object : XmlElementVisitor() {
|
||||
override fun visitElement(element: PsiElement) {
|
||||
element.acceptChildren(this)
|
||||
}
|
||||
override fun visitXmlTag(tag: XmlTag?) {
|
||||
val idPrefix = "@+id/"
|
||||
val attribute = tag?.getAttribute("android:id")
|
||||
if (attribute != null && attribute.getValue() == idPrefix + oldName) {
|
||||
allRenames[XmlAttributeValueWrapper(attribute.getValueElement()!!)] = idPrefix + newName
|
||||
}
|
||||
tag?.acceptChildren(this)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
val name = AndroidResourceUtil.getResourceNameByReferenceText(newName!!)
|
||||
allRenames[XmlAttributeValueWrapper(attr.getValueElement()!!)] = resourceManager.nameToId(newName!!)
|
||||
val name = AndroidResourceUtil.getResourceNameByReferenceText(newName)
|
||||
for (resField in AndroidResourceUtil.findIdFields(attr)) {
|
||||
allRenames.put(resField, AndroidResourceUtil.getFieldNameByResourceName(name!!))
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ public class IDEAndroidResourceManager(project: Project, searchPath: String?) :
|
||||
}
|
||||
|
||||
private fun setupElementCache() {
|
||||
idToXmlAttributeCache.clear()
|
||||
for (file in getLayoutXmlFiles()) {
|
||||
if (file is XmlFile) {
|
||||
file.accept(object : XmlElementVisitor() {
|
||||
@@ -41,11 +42,10 @@ public class IDEAndroidResourceManager(project: Project, searchPath: String?) :
|
||||
element.acceptChildren(this)
|
||||
}
|
||||
override fun visitXmlTag(tag: XmlTag?) {
|
||||
val idPrefix = "@+id/"
|
||||
val attribute = tag?.getAttribute("android:id")
|
||||
val s = attribute?.getValue()
|
||||
if (attribute != null && (s?.startsWith(idPrefix) ?: false)) {
|
||||
idToXmlAttributeCache[s!!.replace(idPrefix, "")] = attribute
|
||||
if (attribute != null && s != null && isResourceId(s)) {
|
||||
idToXmlAttributeCache[idToName(s)] = attribute
|
||||
}
|
||||
tag?.acceptChildren(this)
|
||||
}
|
||||
@@ -55,7 +55,13 @@ public class IDEAndroidResourceManager(project: Project, searchPath: String?) :
|
||||
|
||||
}
|
||||
override fun idToXmlAttribute(id: String): PsiElement? {
|
||||
return idToXmlAttributeCache[id]
|
||||
val element = idToXmlAttributeCache[id]
|
||||
// element not in cache - files might have changed
|
||||
if (element == null) {
|
||||
setupElementCache()
|
||||
return idToXmlAttributeCache[id]
|
||||
}
|
||||
return element
|
||||
}
|
||||
override fun renameXmlAttr(elem: PsiElement, newName: String) {
|
||||
val xmlAttr = elem as XmlAttribute
|
||||
|
||||
Reference in New Issue
Block a user