Fix Android extensions property usage highlighting

#KT-10736 Fixed
This commit is contained in:
Vyacheslav Gerasimov
2017-06-13 15:27:43 +03:00
parent f8617f6676
commit f51e3ab90c
7 changed files with 206 additions and 0 deletions
@@ -1302,6 +1302,10 @@ fun main(args: Array<String>) {
testClass<AbstractAndroidFindUsagesTest> {
model("android/findUsages", recursive = false, extension = null)
}
testClass<AbstractAndroidUsageHighlightingTest> {
model("android/usageHighlighting", recursive = false, extension = null)
}
}
testGroup("idea/idea-android/tests", "idea/testData") {
+2
View File
@@ -61,6 +61,8 @@
<annotator language="kotlin" implementationClass="org.jetbrains.kotlin.android.AndroidResourceReferenceAnnotator" />
<referencesSearch implementation="org.jetbrains.kotlin.AndroidExtensionsReferenceSearchExecutor"/>
</extensions>
<extensions defaultExtensionNs="org.jetbrains.kotlin">
@@ -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<PsiReference, ReferencesSearch.SearchParameters>() {
override fun processQuery(queryParameters: ReferencesSearch.SearchParameters, consumer: Processor<PsiReference>) {
val elementToSearch = queryParameters.elementToSearch as? XmlAttributeValue ?: return
val scopeElements = (queryParameters.effectiveSearchScope as? LocalSearchScope)?.scope ?: return
val referenceName = elementToSearch.value.substringAfterLast("/")
scopeElements.filterIsInstance<KtElement>().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
}
@@ -0,0 +1,10 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
@@ -0,0 +1,33 @@
package com.myapp
import android.app.Activity
import android.os.Bundle
import java.io.File
import kotlinx.android.synthetic.main.layout.*
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
<info descr="null"><caret>login</info>.setOnClickListener {
}
<info descr="null">login</info>.text = "Login"
val login = "Login"
val login42 = login + "42"
}
fun foo(): String {
val login = "Login"
return login + "42"
}
}
class AnotherActivity : Activity() {
private val login: String = "login"
fun test() {
login.length
(this as Activity).<info descr="null">login</info>.text = "login"
}
}
@@ -0,0 +1,50 @@
/*
* 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.android
import com.intellij.codeInsight.daemon.impl.HighlightInfo
import com.intellij.codeInsight.daemon.impl.HighlightInfoType
import com.intellij.codeInsight.highlighting.actions.HighlightUsagesAction
import com.intellij.testFramework.ExpectedHighlightingData
abstract class AbstractAndroidUsageHighlightingTest : KotlinAndroidTestCase() {
fun doTest(path: String) {
copyResourceDirectoryForTest(path)
val virtualFile = myFixture.copyFileToProject(path + getTestName(true) + ".kt", "src/" + getTestName(true) + ".kt")
myFixture.configureFromExistingVirtualFile(virtualFile)
val document = myFixture.editor.document
val data = ExpectedHighlightingData(
document,
false,
false,
true,
false,
myFixture.file)
data.init()
myFixture.testAction(HighlightUsagesAction())
val infos = myFixture.editor.markupModel.allHighlighters.map {
HighlightInfo.newHighlightInfo(HighlightInfoType.INFORMATION).range(it.startOffset, it.endOffset).create()
}
data.checkResult(infos, document.text)
}
}
@@ -0,0 +1,44 @@
/*
* 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.android;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("plugins/android-extensions/android-extensions-idea/testData/android/usageHighlighting")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class AndroidUsageHighlightingTestGenerated extends AbstractAndroidUsageHighlightingTest {
public void testAllFilesPresentInUsageHighlighting() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("plugins/android-extensions/android-extensions-idea/testData/android/usageHighlighting"), Pattern.compile("^([^\\.]+)$"), TargetBackend.ANY, false);
}
@TestMetadata("simple")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("plugins/android-extensions/android-extensions-idea/testData/android/usageHighlighting/simple/");
doTest(fileName);
}
}