From a75da632a6df40d6e5ce9de38ed2d547391c7638 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 31 Jan 2019 17:22:43 +0300 Subject: [PATCH] Revert extension function in IndexUtils and better testing --- .../kotlin/generators/tests/GenerateTests.kt | 2 +- .../generators/tests/GenerateTests.kt.as32 | 2 +- .../generators/tests/GenerateTests.kt.as33 | 2 +- .../generators/tests/GenerateTests.kt.as34 | 2 +- .../kotlin/idea/stubindex/IndexUtils.kt | 31 +++++++++---------- .../typealiasExpansionIndex/recursion.kt | 1 - ...nTypeAliasByExpansionShortNameIndexTest.kt | 2 +- .../kotlin/idea/index/IndexExceptionTest.kt | 17 ++++++++++ ...yExpansionShortNameIndexTestGenerated.java | 7 +---- 9 files changed, 37 insertions(+), 29 deletions(-) delete mode 100644 idea/testData/typealiasExpansionIndex/recursion.kt rename idea/tests/org/jetbrains/kotlin/idea/{ => index}/AbstractKotlinTypeAliasByExpansionShortNameIndexTest.kt (98%) create mode 100644 idea/tests/org/jetbrains/kotlin/idea/index/IndexExceptionTest.kt rename idea/tests/org/jetbrains/kotlin/idea/{ => index}/KotlinTypeAliasByExpansionShortNameIndexTestGenerated.java (90%) diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index bc4c235abc5..c4dca11dbd5 100755 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -45,7 +45,7 @@ import org.jetbrains.kotlin.generators.util.KT_OR_KTS import org.jetbrains.kotlin.generators.util.KT_OR_KTS_WITHOUT_DOTS_IN_NAME import org.jetbrains.kotlin.generators.util.KT_WITHOUT_DOTS_IN_NAME import org.jetbrains.kotlin.idea.AbstractExpressionSelectionTest -import org.jetbrains.kotlin.idea.AbstractKotlinTypeAliasByExpansionShortNameIndexTest +import org.jetbrains.kotlin.idea.index.AbstractKotlinTypeAliasByExpansionShortNameIndexTest import org.jetbrains.kotlin.idea.AbstractSmartSelectionTest import org.jetbrains.kotlin.idea.actions.AbstractGotoTestOrCodeActionTest import org.jetbrains.kotlin.idea.caches.resolve.AbstractIdeCompiledLightClassTest diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 index 5a2b0960ff3..924dcf09bf6 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 @@ -45,7 +45,7 @@ import org.jetbrains.kotlin.generators.util.KT_OR_KTS import org.jetbrains.kotlin.generators.util.KT_OR_KTS_WITHOUT_DOTS_IN_NAME import org.jetbrains.kotlin.generators.util.KT_WITHOUT_DOTS_IN_NAME import org.jetbrains.kotlin.idea.AbstractExpressionSelectionTest -import org.jetbrains.kotlin.idea.AbstractKotlinTypeAliasByExpansionShortNameIndexTest +import org.jetbrains.kotlin.idea.index.AbstractKotlinTypeAliasByExpansionShortNameIndexTest import org.jetbrains.kotlin.idea.AbstractSmartSelectionTest import org.jetbrains.kotlin.idea.actions.AbstractGotoTestOrCodeActionTest import org.jetbrains.kotlin.idea.caches.resolve.AbstractIdeCompiledLightClassTest diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 index 5d3c4482b19..38dc4f50554 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 @@ -37,7 +37,7 @@ import org.jetbrains.kotlin.generators.util.KT_OR_KTS import org.jetbrains.kotlin.generators.util.KT_OR_KTS_WITHOUT_DOTS_IN_NAME import org.jetbrains.kotlin.generators.util.KT_WITHOUT_DOTS_IN_NAME import org.jetbrains.kotlin.idea.AbstractExpressionSelectionTest -import org.jetbrains.kotlin.idea.AbstractKotlinTypeAliasByExpansionShortNameIndexTest +import org.jetbrains.kotlin.idea.index.AbstractKotlinTypeAliasByExpansionShortNameIndexTest import org.jetbrains.kotlin.idea.AbstractSmartSelectionTest import org.jetbrains.kotlin.idea.actions.AbstractGotoTestOrCodeActionTest import org.jetbrains.kotlin.idea.caches.resolve.AbstractIdeCompiledLightClassTest diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as34 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as34 index 5d3c4482b19..38dc4f50554 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as34 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as34 @@ -37,7 +37,7 @@ import org.jetbrains.kotlin.generators.util.KT_OR_KTS import org.jetbrains.kotlin.generators.util.KT_OR_KTS_WITHOUT_DOTS_IN_NAME import org.jetbrains.kotlin.generators.util.KT_WITHOUT_DOTS_IN_NAME import org.jetbrains.kotlin.idea.AbstractExpressionSelectionTest -import org.jetbrains.kotlin.idea.AbstractKotlinTypeAliasByExpansionShortNameIndexTest +import org.jetbrains.kotlin.idea.index.AbstractKotlinTypeAliasByExpansionShortNameIndexTest import org.jetbrains.kotlin.idea.AbstractSmartSelectionTest import org.jetbrains.kotlin.idea.actions.AbstractGotoTestOrCodeActionTest import org.jetbrains.kotlin.idea.caches.resolve.AbstractIdeCompiledLightClassTest diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/IndexUtils.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/IndexUtils.kt index af5230353d9..750d9109262 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/IndexUtils.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/IndexUtils.kt @@ -30,7 +30,7 @@ fun indexTopLevelExtension(stub: KotlinCa if (stub.isExtension()) { val declaration = stub.psi val containingTypeReference = declaration.receiverTypeReference!! - index(containingTypeReference.typeElement, declaration, containingTypeReference) { typeName -> + containingTypeReference.typeElement?.index(declaration, containingTypeReference) { typeName -> val name = declaration.name ?: return@index sink.occurrence( KotlinTopLevelExtensionsByReceiverTypeIndex.INSTANCE.key, @@ -44,38 +44,35 @@ fun indexTypeAliasExpansion(stub: KotlinTypeAliasStub, sink: IndexSink) { val declaration = stub.psi val typeReference = declaration.getTypeReference() ?: return val typeElement = typeReference.typeElement ?: return - index(typeElement, declaration, typeReference) { typeName -> + typeElement.index(declaration, typeReference) { typeName -> sink.occurrence(KotlinTypeAliasByExpansionShortNameIndex.KEY, typeName) } } -private fun index( - typeElement: KtTypeElement?, +private fun KtTypeElement.index( declaration: KtTypeParameterListOwner, containingTypeReference: KtTypeReference, occurrence: (String) -> Unit ) { - fun internalIndex( - typeElement: KtTypeElement?, + fun KtTypeElement.indexWithVisited( declaration: KtTypeParameterListOwner, containingTypeReference: KtTypeReference, visited: MutableSet, occurrence: (String) -> Unit ) { - if (typeElement == null) return - if (typeElement in visited) return + if (this in visited) return - visited.add(typeElement) + visited.add(this) - when (typeElement) { + when (this) { is KtUserType -> { - val referenceName = typeElement.referencedName ?: return + val referenceName = referencedName ?: return val typeParameter = declaration.typeParameters.firstOrNull { it.name == referenceName } if (typeParameter != null) { val bound = typeParameter.extendsBound if (bound != null) { - internalIndex(bound.typeElement, declaration, containingTypeReference, visited, occurrence) + bound.typeElement?.indexWithVisited(declaration, containingTypeReference, visited, occurrence) } else { occurrence("Any") } @@ -84,13 +81,13 @@ private fun index( occurrence(referenceName) - typeElement.aliasImportMap()[referenceName].forEach { occurrence(it) } + aliasImportMap()[referenceName].forEach { occurrence(it) } } - is KtNullableType -> internalIndex(typeElement.innerType, declaration, containingTypeReference, visited, occurrence) + is KtNullableType -> innerType?.indexWithVisited(declaration, containingTypeReference, visited, occurrence) is KtFunctionType -> { - val arity = typeElement.parameters.size + (if (typeElement.receiverTypeReference != null) 1 else 0) + val arity = parameters.size + (if (receiverTypeReference != null) 1 else 0) val suspendPrefix = if (containingTypeReference.modifierList?.hasModifier(KtTokens.SUSPEND_KEYWORD) == true) "Suspend" @@ -101,11 +98,11 @@ private fun index( is KtDynamicType -> occurrence("Any") - else -> error("Unsupported type: $typeElement") + else -> error("Unsupported type: $this") } } - internalIndex(typeElement, declaration, containingTypeReference, mutableSetOf(), occurrence) + indexWithVisited(declaration, containingTypeReference, mutableSetOf(), occurrence) } fun indexInternals(stub: KotlinCallableStubBase<*>, sink: IndexSink) { diff --git a/idea/testData/typealiasExpansionIndex/recursion.kt b/idea/testData/typealiasExpansionIndex/recursion.kt deleted file mode 100644 index 30a6d5685d5..00000000000 --- a/idea/testData/typealiasExpansionIndex/recursion.kt +++ /dev/null @@ -1 +0,0 @@ -//fun T.some() {} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/AbstractKotlinTypeAliasByExpansionShortNameIndexTest.kt b/idea/tests/org/jetbrains/kotlin/idea/index/AbstractKotlinTypeAliasByExpansionShortNameIndexTest.kt similarity index 98% rename from idea/tests/org/jetbrains/kotlin/idea/AbstractKotlinTypeAliasByExpansionShortNameIndexTest.kt rename to idea/tests/org/jetbrains/kotlin/idea/index/AbstractKotlinTypeAliasByExpansionShortNameIndexTest.kt index 8d1788013e0..6e0dff1a48a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/AbstractKotlinTypeAliasByExpansionShortNameIndexTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/index/AbstractKotlinTypeAliasByExpansionShortNameIndexTest.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea +package org.jetbrains.kotlin.idea.index import com.intellij.psi.search.GlobalSearchScope import org.jetbrains.kotlin.idea.stubindex.KotlinTypeAliasByExpansionShortNameIndex diff --git a/idea/tests/org/jetbrains/kotlin/idea/index/IndexExceptionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/index/IndexExceptionTest.kt new file mode 100644 index 00000000000..885c1ff1ea6 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/index/IndexExceptionTest.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.index + +import com.intellij.testFramework.LightProjectDescriptor +import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase + +class IndexExceptionTest : KotlinLightCodeInsightFixtureTestCase() { + fun testRecursionInTypeParameters() { + myFixture.configureByText("test.kt", "fun T.some() {}") + } + + override fun getProjectDescriptor(): LightProjectDescriptor = JAVA_LATEST +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/KotlinTypeAliasByExpansionShortNameIndexTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/index/KotlinTypeAliasByExpansionShortNameIndexTestGenerated.java similarity index 90% rename from idea/tests/org/jetbrains/kotlin/idea/KotlinTypeAliasByExpansionShortNameIndexTestGenerated.java rename to idea/tests/org/jetbrains/kotlin/idea/index/KotlinTypeAliasByExpansionShortNameIndexTestGenerated.java index 1e91afb01f7..a8f2db08268 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/KotlinTypeAliasByExpansionShortNameIndexTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/index/KotlinTypeAliasByExpansionShortNameIndexTestGenerated.java @@ -3,7 +3,7 @@ * that can be found in the license/LICENSE.txt file. */ -package org.jetbrains.kotlin.idea; +package org.jetbrains.kotlin.idea.index; import com.intellij.testFramework.TestDataPath; import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; @@ -39,11 +39,6 @@ public class KotlinTypeAliasByExpansionShortNameIndexTestGenerated extends Abstr runTest("idea/testData/typealiasExpansionIndex/generics.kt"); } - @TestMetadata("recursion.kt") - public void testRecursion() throws Exception { - runTest("idea/testData/typealiasExpansionIndex/recursion.kt"); - } - @TestMetadata("simpleType.kt") public void testSimpleType() throws Exception { runTest("idea/testData/typealiasExpansionIndex/simpleType.kt");