Stubs: add classId to KtTypeAlias stubs

This commit is contained in:
Ilya Kirillov
2021-04-10 15:30:19 +02:00
parent fdb091d98e
commit 4942f5eb66
8 changed files with 40 additions and 11 deletions
@@ -21,11 +21,13 @@ import com.intellij.navigation.ItemPresentationProviders
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.lexer.KtTokens 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.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.KotlinTypeAliasStub import org.jetbrains.kotlin.psi.stubs.KotlinTypeAliasStub
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
class KtTypeAlias : KtTypeParameterListOwnerStub<KotlinTypeAliasStub>, KtNamedDeclaration { class KtTypeAlias : KtTypeParameterListOwnerStub<KotlinTypeAliasStub>, KtNamedDeclaration, KtClassLikeDeclaration {
constructor(node: ASTNode) : super(node) constructor(node: ASTNode) : super(node)
constructor(stub: KotlinTypeAliasStub) : super(stub, KtStubElementTypes.TYPEALIAS) constructor(stub: KotlinTypeAliasStub) : super(stub, KtStubElementTypes.TYPEALIAS)
@@ -50,5 +52,10 @@ class KtTypeAlias : KtTypeParameterListOwnerStub<KotlinTypeAliasStub>, KtNamedDe
} }
} }
override fun getClassId(): ClassId? {
stub?.let { return it.getClassId() }
return ClassIdCalculator.calculateClassId(this)
}
override fun getPresentation() = ItemPresentationProviders.getItemPresentation(this) override fun getPresentation() = ItemPresentationProviders.getItemPresentation(this)
} }
@@ -15,13 +15,13 @@ internal object ClassIdCalculator {
fun calculateClassId(declaration: KtClassLikeDeclaration): ClassId? { fun calculateClassId(declaration: KtClassLikeDeclaration): ClassId? {
var ktFile: KtFile? = null var ktFile: KtFile? = null
var element: PsiElement? = declaration var element: PsiElement? = declaration
val containingClasses = mutableListOf<KtClassOrObject>() val containingClasses = mutableListOf<KtClassLikeDeclaration>()
while (element != null) { while (element != null) {
when (element) { when (element) {
is KtEnumEntry -> { is KtEnumEntry -> {
return null return null
} }
is KtClassOrObject -> { is KtClassLikeDeclaration -> {
containingClasses += element containingClasses += element
} }
is KtFile -> { is KtFile -> {
@@ -45,7 +45,7 @@ interface KotlinClassifierStub {
fun getClassId(): ClassId? fun getClassId(): ClassId?
} }
interface KotlinTypeAliasStub : KotlinStubWithFqName<KtTypeAlias> { interface KotlinTypeAliasStub : KotlinClassifierStub, KotlinStubWithFqName<KtTypeAlias> {
fun isTopLevel(): Boolean fun isTopLevel(): Boolean
} }
@@ -21,9 +21,13 @@ import com.intellij.psi.stubs.StubElement
import com.intellij.psi.stubs.StubInputStream import com.intellij.psi.stubs.StubInputStream
import com.intellij.psi.stubs.StubOutputStream import com.intellij.psi.stubs.StubOutputStream
import com.intellij.util.io.StringRef import com.intellij.util.io.StringRef
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.psi.KtTypeAlias import org.jetbrains.kotlin.psi.KtTypeAlias
import org.jetbrains.kotlin.psi.psiUtil.safeFqNameForLazyResolve 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.KotlinTypeAliasStub
import org.jetbrains.kotlin.psi.stubs.StubUtils
import org.jetbrains.kotlin.psi.stubs.impl.KotlinTypeAliasStubImpl import org.jetbrains.kotlin.psi.stubs.impl.KotlinTypeAliasStubImpl
class KtTypeAliasElementType(debugName: String) : class KtTypeAliasElementType(debugName: String) :
@@ -32,21 +36,36 @@ class KtTypeAliasElementType(debugName: String) :
override fun createStub(psi: KtTypeAlias, parentStub: StubElement<*>?): KotlinTypeAliasStub { override fun createStub(psi: KtTypeAlias, parentStub: StubElement<*>?): KotlinTypeAliasStub {
val name = StringRef.fromString(psi.name) val name = StringRef.fromString(psi.name)
val fqName = StringRef.fromString(psi.safeFqNameForLazyResolve()?.asString()) val fqName = StringRef.fromString(psi.safeFqNameForLazyResolve()?.asString())
val classId = parentStub?.let { createNestedClassId(it, psi) }
val isTopLevel = psi.isTopLevel() 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) { override fun serialize(stub: KotlinTypeAliasStub, dataStream: StubOutputStream) {
dataStream.writeName(stub.name) dataStream.writeName(stub.name)
dataStream.writeName(stub.getFqName()?.asString()) dataStream.writeName(stub.getFqName()?.asString())
StubUtils.serializeClassId(dataStream, stub.getClassId())
dataStream.writeBoolean(stub.isTopLevel()) dataStream.writeBoolean(stub.isTopLevel())
} }
override fun deserialize(dataStream: StubInputStream, parentStub: StubElement<*>?): KotlinTypeAliasStub { override fun deserialize(dataStream: StubInputStream, parentStub: StubElement<*>?): KotlinTypeAliasStub {
val name = dataStream.readName() val name = dataStream.readName()
val fqName = dataStream.readName() val fqName = dataStream.readName()
val classId = StubUtils.deserializeClassId(dataStream)
val isTopLevel = dataStream.readBoolean() val isTopLevel = dataStream.readBoolean()
return KotlinTypeAliasStubImpl(parentStub, name, fqName, isTopLevel) return KotlinTypeAliasStubImpl(parentStub, name, fqName, classId, isTopLevel)
} }
override fun indexStub(stub: KotlinTypeAliasStub, sink: IndexSink) { override fun indexStub(stub: KotlinTypeAliasStub, sink: IndexSink) {
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.psi.stubs.impl
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.stubs.StubElement import com.intellij.psi.stubs.StubElement
import com.intellij.util.io.StringRef import com.intellij.util.io.StringRef
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtTypeAlias import org.jetbrains.kotlin.psi.KtTypeAlias
import org.jetbrains.kotlin.psi.stubs.KotlinTypeAliasStub import org.jetbrains.kotlin.psi.stubs.KotlinTypeAliasStub
@@ -28,14 +29,16 @@ class KotlinTypeAliasStubImpl(
parent: StubElement<out PsiElement>?, parent: StubElement<out PsiElement>?,
private val name: StringRef?, private val name: StringRef?,
private val qualifiedName: StringRef?, private val qualifiedName: StringRef?,
private val classId: ClassId?,
private val isTopLevel: Boolean private val isTopLevel: Boolean
) : KotlinStubBaseImpl<KtTypeAlias>(parent, KtStubElementTypes.TYPEALIAS), KotlinTypeAliasStub { ) : KotlinStubBaseImpl<KtTypeAlias>(parent, KtStubElementTypes.TYPEALIAS), KotlinTypeAliasStub {
override fun getName(): String? = override fun getName(): String? =
StringRef.toString(name) StringRef.toString(name)
override fun getFqName(): FqName? = override fun getFqName(): FqName? =
StringRef.toString(qualifiedName)?.let(::FqName) StringRef.toString(qualifiedName)?.let(::FqName)
override fun getClassId(): ClassId? = classId
override fun isTopLevel(): Boolean = isTopLevel override fun isTopLevel(): Boolean = isTopLevel
} }
@@ -31,7 +31,7 @@ fun createTypeAliasStub(
} }
val typeAlias = KotlinTypeAliasStubImpl( val typeAlias = KotlinTypeAliasStubImpl(
parent, classId.shortClassName.ref(), classId.asSingleFqName().ref(), parent, classId.shortClassName.ref(), classId.asSingleFqName().ref(), classId,
isTopLevel = !classId.isNestedClass isTopLevel = !classId.isNestedClass
) )
@@ -154,7 +154,7 @@ PsiJetFileStubImpl[package=foo.TopLevelMembers]
USER_TYPE USER_TYPE
REFERENCE_EXPRESSION[referencedName=kotlin] REFERENCE_EXPRESSION[referencedName=kotlin]
REFERENCE_EXPRESSION[referencedName=Int] 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] MODIFIER_LIST[private]
TYPE_PARAMETER_LIST TYPE_PARAMETER_LIST
TYPE_PARAMETER[fqName=null, isInVariance=false, isOutVariance=false, name=E] TYPE_PARAMETER[fqName=null, isInVariance=false, isOutVariance=false, name=E]
@@ -107,7 +107,7 @@ PsiJetFileStubImpl[package=test]
MODIFIER_LIST[public] MODIFIER_LIST[public]
VALUE_PARAMETER_LIST VALUE_PARAMETER_LIST
CLASS_BODY 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] MODIFIER_LIST[public]
TYPE_REFERENCE TYPE_REFERENCE
FUNCTION_TYPE FUNCTION_TYPE
@@ -123,7 +123,7 @@ PsiJetFileStubImpl[package=test]
USER_TYPE USER_TYPE
REFERENCE_EXPRESSION[referencedName=kotlin] REFERENCE_EXPRESSION[referencedName=kotlin]
REFERENCE_EXPRESSION[referencedName=Unit] 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] MODIFIER_LIST[private]
ANNOTATION_ENTRY[hasValueArguments=false, shortName=Ann] ANNOTATION_ENTRY[hasValueArguments=false, shortName=Ann]
CONSTRUCTOR_CALLEE CONSTRUCTOR_CALLEE