add test for ReferencesSearch; use standard isReferenceTo() API (which is the same thing for Kotlin but more efficient for non-Kotlin references)

This commit is contained in:
Dmitry Jemerov
2015-06-08 18:57:05 +02:00
parent 7d02b522db
commit 34577a7e12
5 changed files with 81 additions and 11 deletions
@@ -171,4 +171,6 @@ public class JetSimpleNameReference(
override fun toString(): String {
return javaClass<JetSimpleNameReference>().getSimpleName() + ": " + expression.getText()
}
override fun getCanonicalText(): String = expression.getText()
}
@@ -53,7 +53,7 @@ public class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, Referenc
when {
!ReferenceRange.containsOffsetInElement(ref, offsetInElement) -> true
!ref.matchesTarget(unwrappedElement) -> true
!ref.isReferenceTo(unwrappedElement) -> true
else -> consumer.process(ref)
}
}
@@ -73,20 +73,12 @@ public class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, Referenc
public fun processJetClassOrObject(element: JetClassOrObject, queryParameters: ReferencesSearch.SearchParameters) {
val className = element.getName()
if (className != null) {
val lightClass = ApplicationManager.getApplication().runReadAction<PsiClass>(object : Computable<PsiClass?> {
override fun compute(): PsiClass? {
return LightClassUtil.getPsiClass(element)
}
})
val lightClass = runReadAction { LightClassUtil.getPsiClass(element) }
if (lightClass != null) {
searchNamedElement(queryParameters, lightClass, className)
if (element is JetObjectDeclaration && element.isCompanion()) {
val fieldForCompanionObject = ApplicationManager.getApplication().runReadAction<PsiField>(object : Computable<PsiField?> {
override fun compute(): PsiField? {
return LightClassUtil.getLightFieldForCompanionObject(element)
}
})
val fieldForCompanionObject = runReadAction { LightClassUtil.getLightFieldForCompanionObject(element) }
if (fieldForCompanionObject != null) {
searchNamedElement(queryParameters, fieldForCompanionObject)
}
+8
View File
@@ -0,0 +1,8 @@
data class A(val <caret>n: Int, val s: String, val o: Any)
fun test() {
val a = A(1, "2", Any())
a.n
a.component1()
val (x, y, z) = a
}
+12
View File
@@ -0,0 +1,12 @@
class A() {
fun p<caret>lus(rhs: A): A {
return A()
}
}
val a1 = A()
val a2 = A()
val a3 = a1 + a2
val a4 = a1.plus(a2)
val a5 = a1 plus a2
@@ -0,0 +1,56 @@
/*
* Copyright 2010-2015 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.search
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
import com.intellij.psi.search.searches.ReferencesSearch
import org.jetbrains.kotlin.idea.references.JetMultiDeclarationReference
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
import org.jetbrains.kotlin.psi.JetFunction
import org.jetbrains.kotlin.psi.JetParameter
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.junit.Assert
import java.io.File
public class KotlinReferencesSearchTest(): AbstractSearcherTest() {
override fun getTestDataPath(): String {
return File(PluginTestCaseBase.getTestDataPathBase(), "/search/references").getPath() + File.separator
}
public fun testPlus() {
val refs = doTest<JetFunction>()
Assert.assertEquals(3, refs.size())
Assert.assertEquals("+", refs[0].getCanonicalText())
Assert.assertEquals("plus", refs[1].getCanonicalText())
Assert.assertEquals("plus", refs[2].getCanonicalText())
}
public fun testParam() {
val refs = doTest<JetParameter>()
Assert.assertEquals(3, refs.size())
Assert.assertEquals("n", refs[0].getCanonicalText())
Assert.assertEquals("component1", refs[1].getCanonicalText())
Assert.assertTrue(refs[2] is JetMultiDeclarationReference)
}
private inline fun doTest<reified T: PsiElement>(): List<PsiReference> {
myFixture.configureByFile(getFileName())
val func = myFixture.getElementAtCaret().getParentOfType<T>(false)!!
return ReferencesSearch.search(func).findAll().sortBy { it.getElement().getTextRange().getStartOffset() }
}
}