From 938248bd2943dea07219d6c1ba7277e814123d47 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Tue, 24 Mar 2015 20:25:20 +0300 Subject: [PATCH] Search for all properties in layout files corresponding to the given JetProperty --- .../resolve/android/AndroidResourceManager.kt | 2 +- .../android/AndroidFindMemberUsagesHandler.kt | 27 ++++++++++--------- .../android/AndroidGotoDeclarationHandler.kt | 8 +++--- .../plugin/android/AndroidRenameProcessor.kt | 19 ++++++++----- .../android/IDEAndroidResourceManager.kt | 16 +++++------ 5 files changed, 40 insertions(+), 32 deletions(-) diff --git a/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidResourceManager.kt b/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidResourceManager.kt index a0e1fa27b22..103bb7a85c0 100644 --- a/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidResourceManager.kt +++ b/plugins/android-compiler-plugin/src/org/jetbrains/kotlin/lang/resolve/android/AndroidResourceManager.kt @@ -32,7 +32,7 @@ public abstract class AndroidResourceManager(val project: Project) { public abstract val androidModuleInfo: AndroidModuleInfo? - public open fun propertyToXmlAttribute(property: JetProperty): PsiElement? = null + public open fun propertyToXmlAttributes(property: JetProperty): List = listOf() public fun getLayoutXmlFiles(): Map> { val info = androidModuleInfo diff --git a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidFindMemberUsagesHandler.kt b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidFindMemberUsagesHandler.kt index 6dead75f56c..bde83931079 100644 --- a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidFindMemberUsagesHandler.kt +++ b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidFindMemberUsagesHandler.kt @@ -58,10 +58,9 @@ class AndroidFindMemberUsagesHandler( val moduleInfo = declaration.getModuleInfo() as? ModuleSourceInfo ?: return super.getPrimaryElements() val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass()) - val psiElement = parser?.resourceManager?.propertyToXmlAttribute(property) as? XmlAttribute - if (psiElement != null && psiElement.getValueElement() != null) { - return array(psiElement.getValueElement()!!) - } + val psiElements = parser?.resourceManager?.propertyToXmlAttributes(property) + val valueElements = psiElements?.map { (it as? XmlAttribute)?.getValueElement() as? PsiElement }?.filterNotNull() + if (valueElements != null && valueElements.isNotEmpty()) return valueElements.copyToArray() return super.getPrimaryElements() } @@ -77,17 +76,21 @@ class AndroidFindMemberUsagesHandler( val moduleInfo = declaration.getModuleInfo() as? ModuleSourceInfo ?: return super.getPrimaryElements() val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass()) - val psiElement = parser?.resourceManager?.propertyToXmlAttribute(property) as? XmlAttribute - if (psiElement != null) { - val res = ArrayList() - val fields = AndroidResourceUtil.findIdFields(psiElement) - for (field in fields) { - res.add(field) + val psiElements = parser?.resourceManager?.propertyToXmlAttributes(property) ?: listOf() + + val res = ArrayList() + for (psiElement in psiElements) { + if (psiElement is XmlAttribute) { + val fields = AndroidResourceUtil.findIdFields(psiElement) + for (field in fields) { + res.add(field) + } + res.add(declaration) } - res.add(declaration) - return res.copyToArray() } + if (res.isNotEmpty()) return res.copyToArray() + return super.getSecondaryElements() } diff --git a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidGotoDeclarationHandler.kt b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidGotoDeclarationHandler.kt index fb33da1fe10..0eb3908d1af 100644 --- a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidGotoDeclarationHandler.kt +++ b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidGotoDeclarationHandler.kt @@ -41,11 +41,9 @@ public class AndroidGotoDeclarationHandler : GotoDeclarationHandler { if (moduleInfo !is ModuleSourceInfo) return null val parser = ModuleServiceManager.getService(moduleInfo.module, javaClass()) - val psiElement = parser.resourceManager.propertyToXmlAttribute(property) as? XmlAttribute - val attributeValue = psiElement?.getValueElement() - if (attributeValue != null) { - return array(attributeValue) - } + val psiElements = parser.resourceManager.propertyToXmlAttributes(property) + val valueElements = psiElements.map { (it as? XmlAttribute)?.getValueElement() as? PsiElement }.filterNotNull() + if (valueElements.isNotEmpty()) return valueElements.copyToArray() } return null } diff --git a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidRenameProcessor.kt b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidRenameProcessor.kt index 6ca91abd1ae..e47aaa450e0 100644 --- a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidRenameProcessor.kt +++ b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/AndroidRenameProcessor.kt @@ -96,12 +96,19 @@ public class AndroidRenameProcessor : RenamePsiElementProcessor() { val processor = ModuleServiceManager.getService(module, javaClass()) val resourceManager = processor.resourceManager - val attr = resourceManager.propertyToXmlAttribute(jetProperty) as XmlAttribute - val attributeValue = attr.getValueElement() ?: return - allRenames[XmlAttributeValueWrapper(attributeValue)] = nameToIdDeclaration(newName) - val name = AndroidResourceUtil.getResourceNameByReferenceText(newName) ?: return - for (resField in AndroidResourceUtil.findIdFields(attr)) { - allRenames.put(resField, AndroidResourceUtil.getFieldNameByResourceName(name)) + + val psiElements = resourceManager.propertyToXmlAttributes(jetProperty).map { it as? XmlAttribute }.filterNotNull() + + for (psiElement in psiElements) { + val valueElement = psiElement.getValueElement() + + if (valueElement != null) { + allRenames[XmlAttributeValueWrapper(valueElement)] = nameToIdDeclaration(newName) + val name = AndroidResourceUtil.getResourceNameByReferenceText(newName) ?: return + for (resField in AndroidResourceUtil.findIdFields(psiElement)) { + allRenames.put(resField, AndroidResourceUtil.getFieldNameByResourceName(name)) + } + } } } diff --git a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/IDEAndroidResourceManager.kt b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/IDEAndroidResourceManager.kt index b314ef4df14..7ac7bae1bd6 100644 --- a/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/IDEAndroidResourceManager.kt +++ b/plugins/android-idea-plugin/src/org/jetbrains/kotlin/plugin/android/IDEAndroidResourceManager.kt @@ -30,23 +30,23 @@ public class IDEAndroidResourceManager(val module: Module) : AndroidResourceMana override val androidModuleInfo: AndroidModuleInfo? by Delegates.lazy { module.androidFacet?.toAndroidModuleInfo() } - override fun propertyToXmlAttribute(property: JetProperty): PsiElement? { - val fqPath = property.getFqName()?.pathSegments() ?: return null - if (fqPath.size() <= AndroidConst.SYNTHETIC_PACKAGE_PATH_LENGTH) return null + override fun propertyToXmlAttributes(property: JetProperty): List { + val fqPath = property.getFqName()?.pathSegments() ?: return listOf() + if (fqPath.size() <= AndroidConst.SYNTHETIC_PACKAGE_PATH_LENGTH) return listOf() val layoutPackageName = fqPath[AndroidConst.SYNTHETIC_PACKAGE_PATH_LENGTH].asString() val layoutFiles = getLayoutXmlFiles()[layoutPackageName] - if (layoutFiles == null || layoutFiles.isEmpty()) return null + if (layoutFiles == null || layoutFiles.isEmpty()) return listOf() val propertyName = property.getName() - var ret: PsiElement? = null + val attributes = arrayListOf() val visitor = AndroidXmlVisitor { retId, wClass, valueElement -> - if (retId == propertyName) ret = valueElement + if (retId == propertyName) attributes.add(valueElement) } - layoutFiles.first().accept(visitor) - return ret + layoutFiles.forEach { it.accept(visitor) } + return attributes } private val Module.androidFacet: AndroidFacet?