From 4942f5eb668dd9fe9ffd2b81d9bc9c6f433e55aa Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Sat, 10 Apr 2021 15:30:19 +0200 Subject: [PATCH] Stubs: add classId to KtTypeAlias stubs --- .../org/jetbrains/kotlin/psi/KtTypeAlias.kt | 9 +++++++- .../kotlin/psi/psiUtil/ClassIdCalculator.kt | 4 ++-- .../kotlin/psi/stubs/StubInterfaces.kt | 2 +- .../stubs/elements/KtTypeAliasElementType.kt | 23 +++++++++++++++++-- .../psi/stubs/impl/KotlinTypeAliasStubImpl.kt | 5 +++- .../stubBuilder/typeAliasClsStubBuilding.kt | 2 +- .../TopLevelMembersKt/TopLevelMembersKt.txt | 2 +- .../stubBuilder/TypeAliases/TypeAliases.txt | 4 ++-- 8 files changed, 40 insertions(+), 11 deletions(-) diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtTypeAlias.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KtTypeAlias.kt index e9471848e0c..ca37db1d412 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtTypeAlias.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtTypeAlias.kt @@ -21,11 +21,13 @@ import com.intellij.navigation.ItemPresentationProviders import com.intellij.psi.PsiElement import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.psi.psiUtil.ClassIdCalculator import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub import org.jetbrains.kotlin.psi.stubs.KotlinTypeAliasStub import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes -class KtTypeAlias : KtTypeParameterListOwnerStub, KtNamedDeclaration { +class KtTypeAlias : KtTypeParameterListOwnerStub, KtNamedDeclaration, KtClassLikeDeclaration { constructor(node: ASTNode) : super(node) constructor(stub: KotlinTypeAliasStub) : super(stub, KtStubElementTypes.TYPEALIAS) @@ -50,5 +52,10 @@ class KtTypeAlias : KtTypeParameterListOwnerStub, KtNamedDe } } + override fun getClassId(): ClassId? { + stub?.let { return it.getClassId() } + return ClassIdCalculator.calculateClassId(this) + } + override fun getPresentation() = ItemPresentationProviders.getItemPresentation(this) } diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/psiUtil/ClassIdCalculator.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/psiUtil/ClassIdCalculator.kt index eb1a81802f6..7e6402bf44e 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/psiUtil/ClassIdCalculator.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/psiUtil/ClassIdCalculator.kt @@ -15,13 +15,13 @@ internal object ClassIdCalculator { fun calculateClassId(declaration: KtClassLikeDeclaration): ClassId? { var ktFile: KtFile? = null var element: PsiElement? = declaration - val containingClasses = mutableListOf() + val containingClasses = mutableListOf() while (element != null) { when (element) { is KtEnumEntry -> { return null } - is KtClassOrObject -> { + is KtClassLikeDeclaration -> { containingClasses += element } is KtFile -> { diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/StubInterfaces.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/StubInterfaces.kt index ece668a1d86..03f7b4d1596 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/StubInterfaces.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/StubInterfaces.kt @@ -45,7 +45,7 @@ interface KotlinClassifierStub { fun getClassId(): ClassId? } -interface KotlinTypeAliasStub : KotlinStubWithFqName { +interface KotlinTypeAliasStub : KotlinClassifierStub, KotlinStubWithFqName { fun isTopLevel(): Boolean } diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtTypeAliasElementType.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtTypeAliasElementType.kt index 1300ac4e9cf..55518205086 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtTypeAliasElementType.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/elements/KtTypeAliasElementType.kt @@ -21,9 +21,13 @@ import com.intellij.psi.stubs.StubElement import com.intellij.psi.stubs.StubInputStream import com.intellij.psi.stubs.StubOutputStream import com.intellij.util.io.StringRef +import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.psi.KtTypeAlias import org.jetbrains.kotlin.psi.psiUtil.safeFqNameForLazyResolve +import org.jetbrains.kotlin.psi.stubs.KotlinClassStub +import org.jetbrains.kotlin.psi.stubs.KotlinFileStub import org.jetbrains.kotlin.psi.stubs.KotlinTypeAliasStub +import org.jetbrains.kotlin.psi.stubs.StubUtils import org.jetbrains.kotlin.psi.stubs.impl.KotlinTypeAliasStubImpl class KtTypeAliasElementType(debugName: String) : @@ -32,21 +36,36 @@ class KtTypeAliasElementType(debugName: String) : override fun createStub(psi: KtTypeAlias, parentStub: StubElement<*>?): KotlinTypeAliasStub { val name = StringRef.fromString(psi.name) val fqName = StringRef.fromString(psi.safeFqNameForLazyResolve()?.asString()) + val classId = parentStub?.let { createNestedClassId(it, psi) } val isTopLevel = psi.isTopLevel() - return KotlinTypeAliasStubImpl(parentStub, name, fqName, isTopLevel) + return KotlinTypeAliasStubImpl(parentStub, name, fqName, classId, isTopLevel) + } + + private fun createNestedClassId(parentStub: StubElement<*>, typeAlias: KtTypeAlias): ClassId? { + val typeAliasName = typeAlias.nameAsName ?: return null + return when { + parentStub.stubType == KtStubElementTypes.CLASS_BODY -> { + val parentClass = parentStub.parentStub as? KotlinClassStub + parentClass?.getClassId()?.createNestedClassId(typeAliasName) + } + parentStub is KotlinFileStub -> ClassId(parentStub.getPackageFqName(), typeAliasName) + else -> null + } } override fun serialize(stub: KotlinTypeAliasStub, dataStream: StubOutputStream) { dataStream.writeName(stub.name) dataStream.writeName(stub.getFqName()?.asString()) + StubUtils.serializeClassId(dataStream, stub.getClassId()) dataStream.writeBoolean(stub.isTopLevel()) } override fun deserialize(dataStream: StubInputStream, parentStub: StubElement<*>?): KotlinTypeAliasStub { val name = dataStream.readName() val fqName = dataStream.readName() + val classId = StubUtils.deserializeClassId(dataStream) val isTopLevel = dataStream.readBoolean() - return KotlinTypeAliasStubImpl(parentStub, name, fqName, isTopLevel) + return KotlinTypeAliasStubImpl(parentStub, name, fqName, classId, isTopLevel) } override fun indexStub(stub: KotlinTypeAliasStub, sink: IndexSink) { diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinTypeAliasStubImpl.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinTypeAliasStubImpl.kt index 282662c3485..3e9633a3b1a 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinTypeAliasStubImpl.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/stubs/impl/KotlinTypeAliasStubImpl.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.psi.stubs.impl import com.intellij.psi.PsiElement import com.intellij.psi.stubs.StubElement import com.intellij.util.io.StringRef +import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.KtTypeAlias import org.jetbrains.kotlin.psi.stubs.KotlinTypeAliasStub @@ -28,14 +29,16 @@ class KotlinTypeAliasStubImpl( parent: StubElement?, private val name: StringRef?, private val qualifiedName: StringRef?, + private val classId: ClassId?, private val isTopLevel: Boolean ) : KotlinStubBaseImpl(parent, KtStubElementTypes.TYPEALIAS), KotlinTypeAliasStub { - override fun getName(): String? = StringRef.toString(name) override fun getFqName(): FqName? = StringRef.toString(qualifiedName)?.let(::FqName) + override fun getClassId(): ClassId? = classId + override fun isTopLevel(): Boolean = isTopLevel } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/typeAliasClsStubBuilding.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/typeAliasClsStubBuilding.kt index 7f901610d6d..f18ba102717 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/typeAliasClsStubBuilding.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/typeAliasClsStubBuilding.kt @@ -31,7 +31,7 @@ fun createTypeAliasStub( } val typeAlias = KotlinTypeAliasStubImpl( - parent, classId.shortClassName.ref(), classId.asSingleFqName().ref(), + parent, classId.shortClassName.ref(), classId.asSingleFqName().ref(), classId, isTopLevel = !classId.isNestedClass ) diff --git a/idea/testData/decompiler/stubBuilder/TopLevelMembersKt/TopLevelMembersKt.txt b/idea/testData/decompiler/stubBuilder/TopLevelMembersKt/TopLevelMembersKt.txt index 4695f5ef7b4..bf0dfa0931f 100644 --- a/idea/testData/decompiler/stubBuilder/TopLevelMembersKt/TopLevelMembersKt.txt +++ b/idea/testData/decompiler/stubBuilder/TopLevelMembersKt/TopLevelMembersKt.txt @@ -154,7 +154,7 @@ PsiJetFileStubImpl[package=foo.TopLevelMembers] USER_TYPE REFERENCE_EXPRESSION[referencedName=kotlin] REFERENCE_EXPRESSION[referencedName=Int] - TYPEALIAS[fqName=foo.TopLevelMembers.Alias, isTopLevel=true, name=Alias] + TYPEALIAS[classId=foo/TopLevelMembers/Alias, fqName=foo.TopLevelMembers.Alias, isTopLevel=true, name=Alias] MODIFIER_LIST[private] TYPE_PARAMETER_LIST TYPE_PARAMETER[fqName=null, isInVariance=false, isOutVariance=false, name=E] diff --git a/idea/testData/decompiler/stubBuilder/TypeAliases/TypeAliases.txt b/idea/testData/decompiler/stubBuilder/TypeAliases/TypeAliases.txt index 2a8583b5d0a..7aafcb92318 100644 --- a/idea/testData/decompiler/stubBuilder/TypeAliases/TypeAliases.txt +++ b/idea/testData/decompiler/stubBuilder/TypeAliases/TypeAliases.txt @@ -107,7 +107,7 @@ PsiJetFileStubImpl[package=test] MODIFIER_LIST[public] VALUE_PARAMETER_LIST CLASS_BODY - TYPEALIAS[fqName=test.TypeAliases.B, isTopLevel=false, name=B] + TYPEALIAS[classId=test/TypeAliases.B, fqName=test.TypeAliases.B, isTopLevel=false, name=B] MODIFIER_LIST[public] TYPE_REFERENCE FUNCTION_TYPE @@ -123,7 +123,7 @@ PsiJetFileStubImpl[package=test] USER_TYPE REFERENCE_EXPRESSION[referencedName=kotlin] REFERENCE_EXPRESSION[referencedName=Unit] - TYPEALIAS[fqName=test.TypeAliases.Parametrized, isTopLevel=false, name=Parametrized] + TYPEALIAS[classId=test/TypeAliases.Parametrized, fqName=test.TypeAliases.Parametrized, isTopLevel=false, name=Parametrized] MODIFIER_LIST[private] ANNOTATION_ENTRY[hasValueArguments=false, shortName=Ann] CONSTRUCTOR_CALLEE