KT-11588 Type aliases

Type alias stubs indexing (required for index-based declaration providers)
This commit is contained in:
Dmitry Petrov
2016-05-18 17:19:47 +03:00
parent 8bf87a9a4f
commit a2ec580119
17 changed files with 257 additions and 8 deletions
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.psi.stubs.elements
import com.intellij.psi.stubs.IndexSink
import com.intellij.psi.stubs.StubElement
import com.intellij.psi.stubs.StubInputStream
import com.intellij.psi.stubs.StubOutputStream
@@ -47,4 +48,8 @@ class KtTypeAliasElementType(debugName: String) :
val isTopLevel = dataStream.readBoolean()
return KotlinTypeAliasStubImpl(KtStubElementTypes.TYPEALIAS, parentStub, name, fqName, isTopLevel)
}
override fun indexStub(stub: KotlinTypeAliasStub, sink: IndexSink) {
StubIndexService.getInstance().indexTypeAlias(stub, sink)
}
}
@@ -35,6 +35,9 @@ open class StubIndexService protected constructor() {
open fun indexFunction(stub: KotlinFunctionStub, sink: IndexSink) {
}
open fun indexTypeAlias(stub: KotlinTypeAliasStub, sink: IndexSink) {
}
open fun indexObject(stub: KotlinObjectStub, sink: IndexSink) {
}
@@ -480,7 +480,7 @@ class TypeResolver(
}
override fun boundsViolationInSubstitution(bound: KotlinType, argument: KotlinType, typeParameter: TypeParameterDescriptor) {
TODO()
TODO("boundsViolationInSubstitution")
}
}
@@ -529,9 +529,11 @@ class TypeResolver(
"Type alias expansion: result for ${typeAliasExpansion.descriptor} is ${expandedProjection.projectionKind}, should be invariant"
}
val abbreviatedType = KotlinTypeImpl.create(annotations, typeAliasExpansion.descriptor.typeConstructor,
val abbreviatedType = KotlinTypeImpl.create(annotations,
typeAliasExpansion.descriptor.typeConstructor,
originalProjection.type.isMarkedNullable,
typeAliasExpansion.arguments, MemberScope.Empty)
typeAliasExpansion.arguments,
MemberScope.Empty)
return expandedType.withAbbreviatedType(abbreviatedType)
}
@@ -0,0 +1,14 @@
//ALLOW_AST_ACCESS
package test
typealias S = String
typealias SS = S
typealias SSS = SS
val x1: S = ""
val x2: SS = ""
val x3: SSS = ""
val x4: S? = ""
val x5: SS? = ""
val x6: SSS? = ""
@@ -0,0 +1,17 @@
package test
public typealias S = kotlin.String
public typealias SS = test.S
public typealias SSS = test.SS
public val x1: test.S [= kotlin.String] = ""
public fun <get-x1>(): test.S [= kotlin.String]
public val x2: test.SS [= kotlin.String] = ""
public fun <get-x2>(): test.SS [= kotlin.String]
public val x3: test.SSS [= kotlin.String] = ""
public fun <get-x3>(): test.SSS [= kotlin.String]
public val x4: test.S? [= kotlin.String?] = ""
public fun <get-x4>(): test.S? [= kotlin.String?]
public val x5: test.SS? [= kotlin.String?] = ""
public fun <get-x5>(): test.SS? [= kotlin.String?]
public val x6: test.SSS? [= kotlin.String?] = ""
public fun <get-x6>(): test.SSS? [= kotlin.String?]
@@ -0,0 +1,13 @@
//ALLOW_AST_ACCESS
package test
typealias L<T> = List<T>
typealias LL<T> = L<T>
typealias LLL<T> = LL<T>
fun test1(x: L<String>) {}
fun test2(x: LL<String>) {}
fun test3(x: LLL<String>) {}
fun test4(x: L<L<String>>) {}
fun test5(x: LL<LL<String>>) {}
fun test6(x: LLL<LLL<String>>) {}
@@ -0,0 +1,11 @@
package test
public typealias L</*0*/ T> = kotlin.collections.List<T>
public typealias LL</*0*/ T> = test.L<T>
public typealias LLL</*0*/ T> = test.LL<T>
public fun test1(/*0*/ x: test.L<kotlin.String> [= kotlin.collections.List<out kotlin.String>]): kotlin.Unit
public fun test2(/*0*/ x: test.LL<kotlin.String> [= kotlin.collections.List<out kotlin.String>]): kotlin.Unit
public fun test3(/*0*/ x: test.LLL<kotlin.String> [= kotlin.collections.List<out kotlin.String>]): kotlin.Unit
public fun test4(/*0*/ x: test.L<test.L<kotlin.String> [= kotlin.collections.List<out kotlin.String>]> [= kotlin.collections.List<out test.L<kotlin.String> [= kotlin.collections.List<out kotlin.String>]>]): kotlin.Unit
public fun test5(/*0*/ x: test.LL<test.LL<kotlin.String> [= kotlin.collections.List<out kotlin.String>]> [= kotlin.collections.List<out test.LL<kotlin.String> [= kotlin.collections.List<out kotlin.String>]>]): kotlin.Unit
public fun test6(/*0*/ x: test.LLL<test.LLL<kotlin.String> [= kotlin.collections.List<out kotlin.String>]> [= kotlin.collections.List<out test.LLL<kotlin.String> [= kotlin.collections.List<out kotlin.String>]>]): kotlin.Unit
@@ -111,8 +111,8 @@ public class RecursiveDescriptorProcessor {
@Override
public Boolean visitTypeAliasDescriptor(TypeAliasDescriptor descriptor, D data) {
// TODO typealias
return true;
return applyWorker(descriptor, data)
&& visitChildren(descriptor.getDeclaredTypeParameters(), data);
}
@Override
@@ -4796,6 +4796,27 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest {
}
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/typealias")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Typealias extends AbstractLoadJavaTest {
public void testAllFilesPresentInTypealias() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/loadJava/compiledKotlin/typealias"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("Basic.kt")
public void testBasic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/typealias/Basic.kt");
doTestCompiledKotlin(fileName);
}
@TestMetadata("Generic.kt")
public void testGeneric() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/typealias/Generic.kt");
doTestCompiledKotlin(fileName);
}
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/visibility")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -3029,6 +3029,27 @@ public class LoadKotlinWithTypeTableTestGenerated extends AbstractLoadKotlinWith
}
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/typealias")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Typealias extends AbstractLoadKotlinWithTypeTableTest {
public void testAllFilesPresentInTypealias() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/loadJava/compiledKotlin/typealias"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("Basic.kt")
public void testBasic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/typealias/Basic.kt");
doTest(fileName);
}
@TestMetadata("Generic.kt")
public void testGeneric() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/typealias/Generic.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/visibility")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -3031,6 +3031,27 @@ public class JvmRuntimeDescriptorLoaderTestGenerated extends AbstractJvmRuntimeD
}
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/typealias")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Typealias extends AbstractJvmRuntimeDescriptorLoaderTest {
public void testAllFilesPresentInTypealias() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/loadJava/compiledKotlin/typealias"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("Basic.kt")
public void testBasic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/typealias/Basic.kt");
doTest(fileName);
}
@TestMetadata("Generic.kt")
public void testGeneric() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/typealias/Generic.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/visibility")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -142,6 +142,19 @@ public class IdeStubIndexService extends StubIndexService {
}
}
@Override
public void indexTypeAlias(KotlinTypeAliasStub stub, IndexSink sink) {
// TODO short name index for type aliases?
if (stub.isTopLevel()) {
FqName fqName = stub.getFqName();
if (fqName != null) {
sink.occurrence(KotlinTopLevelTypeAliasFqNameIndex.getInstance().getKey(), fqName.asString());
sink.occurrence(KotlinTopLevelTypeAliasByPackageIndex.getInstance().getKey(), fqName.parent().asString());
}
}
}
@Override
public void indexProperty(KotlinPropertyStub stub, IndexSink sink) {
String name = stub.getName();
@@ -0,0 +1,41 @@
/*
* Copyright 2010-2016 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.idea.stubindex
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.stubs.StringStubIndexExtension
import com.intellij.psi.stubs.StubIndex
import com.intellij.psi.stubs.StubIndexKey
import org.jetbrains.kotlin.psi.KtTypeAlias
class KotlinTopLevelTypeAliasByPackageIndex : StringStubIndexExtension<KtTypeAlias>() {
override fun getKey(): StubIndexKey<String, KtTypeAlias> = KEY
override fun get(s: String, project: Project, scope: GlobalSearchScope): Collection<KtTypeAlias> =
StubIndex.getElements<String, KtTypeAlias>(
KEY, s, project,
KotlinSourceFilterScope.sourcesAndLibraries(scope, project),
KtTypeAlias::class.java)
companion object {
val KEY = KotlinIndexUtil.createIndexKey(KotlinTopLevelTypeAliasByPackageIndex::class.java)
val INSTANCE = KotlinTopLevelTypeAliasByPackageIndex()
@JvmStatic fun getInstance() = INSTANCE
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2016 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.idea.stubindex
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.stubs.StringStubIndexExtension
import com.intellij.psi.stubs.StubIndex
import com.intellij.psi.stubs.StubIndexKey
import org.jetbrains.kotlin.psi.KtTypeAlias
class KotlinTopLevelTypeAliasFqNameIndex : StringStubIndexExtension<KtTypeAlias>() {
override fun getKey(): StubIndexKey<String, KtTypeAlias> = KEY
override fun get(s: String, project: Project, scope: GlobalSearchScope): Collection<KtTypeAlias> =
StubIndex.getElements<String, KtTypeAlias>(
KEY, s, project,
KotlinSourceFilterScope.sourcesAndLibraries(scope, project),
KtTypeAlias::class.java)
companion object {
val KEY = KotlinIndexUtil.createIndexKey(KotlinTopLevelTypeAliasFqNameIndex::class.java)
val INSTANCE = KotlinTopLevelTypeAliasFqNameIndex()
@JvmStatic fun getInstance() = INSTANCE
}
}
@@ -46,6 +46,7 @@ class StubBasedPackageMemberDeclarationProvider(
if (kindFilter.acceptsKinds(DescriptorKindFilter.CLASSIFIERS_MASK)) {
addFromIndex(KotlinTopLevelClassByPackageIndex.getInstance())
addFromIndex(KotlinTopLevelTypeAliasByPackageIndex.getInstance())
}
if (kindFilter.acceptsKinds(DescriptorKindFilter.FUNCTIONS_MASK)) {
@@ -82,11 +83,12 @@ class StubBasedPackageMemberDeclarationProvider(
}
override fun getPackageFiles(): Collection<KtFile> {
return PackageIndexUtil.findFilesWithExactPackage(fqName, searchScope, project)
return PackageIndexUtil.findFilesWithExactPackage(fqName, searchScope, project)
}
override fun getTypeAliasDeclarations(name: Name): Collection<KtTypeAlias> =
emptyList() // TODO stub index for type aliases
override fun getTypeAliasDeclarations(name: Name): Collection<KtTypeAlias> {
return KotlinTopLevelTypeAliasFqNameIndex.getInstance().get(childName(name), project, searchScope)
}
private fun childName(name: Name): String {
return fqName.child(ResolveSessionUtils.safeNameForLazyResolve(name)).asString()
+2
View File
@@ -563,6 +563,7 @@
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinTopLevelClassByPackageIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinTopLevelFunctionByPackageIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinTopLevelPropertyByPackageIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinTopLevelTypeAliasByPackageIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinClassShortNameIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinFullClassNameIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinPropertyShortNameIndex"/>
@@ -570,6 +571,7 @@
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinSuperClassIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinTopLevelFunctionFqnNameIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinTopLevelPropertyFqnNameIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinTopLevelTypeAliasFqNameIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinTopLevelExtensionsByReceiverTypeIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinAnnotationsIndex"/>
<stubIndex implementation="org.jetbrains.kotlin.idea.stubindex.KotlinProbablyNothingFunctionShortNameIndex"/>
@@ -3029,6 +3029,27 @@ public class ResolveByStubTestGenerated extends AbstractResolveByStubTest {
}
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/typealias")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Typealias extends AbstractResolveByStubTest {
public void testAllFilesPresentInTypealias() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/loadJava/compiledKotlin/typealias"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("Basic.kt")
public void testBasic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/typealias/Basic.kt");
doTest(fileName);
}
@TestMetadata("Generic.kt")
public void testGeneric() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/loadJava/compiledKotlin/typealias/Generic.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/loadJava/compiledKotlin/visibility")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)