diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 4a5bc0cd621..d5ff179660f 100755 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -1302,6 +1302,10 @@ fun main(args: Array) { testClass { model("android/findUsages", recursive = false, extension = null) } + + testClass { + model("android/usageHighlighting", recursive = false, extension = null) + } } testGroup("idea/idea-android/tests", "idea/testData") { diff --git a/idea/src/META-INF/android.xml b/idea/src/META-INF/android.xml index dc7ef279011..701c939623b 100644 --- a/idea/src/META-INF/android.xml +++ b/idea/src/META-INF/android.xml @@ -61,6 +61,8 @@ + + diff --git a/plugins/android-extensions/android-extensions-idea/src/org/jetbrains/kotlin/AndroidExtensionsReferenceSearchExecutor.kt b/plugins/android-extensions/android-extensions-idea/src/org/jetbrains/kotlin/AndroidExtensionsReferenceSearchExecutor.kt new file mode 100644 index 00000000000..c29b9d18859 --- /dev/null +++ b/plugins/android-extensions/android-extensions-idea/src/org/jetbrains/kotlin/AndroidExtensionsReferenceSearchExecutor.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin + +import com.intellij.openapi.application.QueryExecutorBase +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiReference +import com.intellij.psi.search.LocalSearchScope +import com.intellij.psi.search.searches.ReferencesSearch +import com.intellij.psi.xml.XmlAttributeValue +import com.intellij.util.Processor +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.references.KtSimpleNameReference +import org.jetbrains.kotlin.psi.KtElement +import org.jetbrains.kotlin.psi.KtReferenceExpression +import org.jetbrains.kotlin.psi.KtSimpleNameExpression +import org.jetbrains.kotlin.psi.KtTreeVisitorVoid +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +import org.jetbrains.kotlin.resolve.source.getPsi + + +class AndroidExtensionsReferenceSearchExecutor : QueryExecutorBase() { + override fun processQuery(queryParameters: ReferencesSearch.SearchParameters, consumer: Processor) { + val elementToSearch = queryParameters.elementToSearch as? XmlAttributeValue ?: return + val scopeElements = (queryParameters.effectiveSearchScope as? LocalSearchScope)?.scope ?: return + val referenceName = elementToSearch.value.substringAfterLast("/") + + scopeElements.filterIsInstance().forEach { + it.accept(object : KtTreeVisitorVoid() { + override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) { + if (expression.text == referenceName && expression.isReferenceTo(elementToSearch)) { + expression.references.firstOrNull { it is KtSimpleNameReference }?.let { + consumer.process(it) + } + } + super.visitReferenceExpression(expression) + } + }) + } + } + + private fun KtReferenceExpression.isReferenceTo(element: PsiElement): Boolean = + getTargetPropertyDescriptor()?.source?.getPsi() == element + + private fun KtReferenceExpression.getTargetPropertyDescriptor(): PropertyDescriptor? = + analyze(BodyResolveMode.PARTIAL)[BindingContext.REFERENCE_TARGET, this] as? PropertyDescriptor +} \ No newline at end of file diff --git a/plugins/android-extensions/android-extensions-idea/testData/android/usageHighlighting/simple/res/layout/layout.xml b/plugins/android-extensions/android-extensions-idea/testData/android/usageHighlighting/simple/res/layout/layout.xml new file mode 100644 index 00000000000..1f019bffe41 --- /dev/null +++ b/plugins/android-extensions/android-extensions-idea/testData/android/usageHighlighting/simple/res/layout/layout.xml @@ -0,0 +1,10 @@ + + +