From 5f9481b273f16391a0fc7ce7f3d94ceb83ffc2be Mon Sep 17 00:00:00 2001 From: "Vitaliy.Bibaev" Date: Wed, 20 Dec 2017 17:40:50 +0300 Subject: [PATCH] Implement SequenceTypeExtractor --- .../psi/sequence/SequenceTypeExtractor.kt | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/psi/sequence/SequenceTypeExtractor.kt b/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/psi/sequence/SequenceTypeExtractor.kt index e8fbf9cc571..718aceeeba6 100644 --- a/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/psi/sequence/SequenceTypeExtractor.kt +++ b/idea/idea-jvm/src/com/intellij/debugger/streams/kotlin/psi/sequence/SequenceTypeExtractor.kt @@ -2,18 +2,52 @@ package com.intellij.debugger.streams.kotlin.psi.sequence import com.intellij.debugger.streams.kotlin.psi.CallTypeExtractor +import com.intellij.debugger.streams.kotlin.psi.KotlinPsiUtil +import com.intellij.debugger.streams.kotlin.trace.dsl.KotlinTypes +import com.intellij.debugger.streams.trace.impl.handler.type.ClassTypeImpl import com.intellij.debugger.streams.trace.impl.handler.type.GenericType +import com.intellij.openapi.diagnostic.Logger import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.typeUtil.supertypes /** * @author Vitaliy.Bibaev */ class SequenceTypeExtractor : CallTypeExtractor.Base() { + private companion object { + val LOG = Logger.getInstance(SequenceTypeExtractor::class.java) + } + override fun extractItemsType(type: KotlinType?): GenericType { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + if (type == null) return KotlinTypes.NULLABLE_ANY + + return tryToFindElementType(type) ?: defaultType(type) } override fun getResultType(type: KotlinType): GenericType { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + val typeName = KotlinPsiUtil.getTypeWithoutTypeParameters(type) + return KotlinTypes.primitiveTypeByName(typeName) + ?: KotlinTypes.primitiveArrayByName(typeName) + ?: ClassTypeImpl(typeName) + } + + private fun tryToFindElementType(type: KotlinType): GenericType? { + val typeName = KotlinPsiUtil.getTypeWithoutTypeParameters(type) + if (typeName == "kotlin.collections.Sequence") { + if (type.arguments.isEmpty()) return KotlinTypes.NULLABLE_ANY + val itemsType = type.arguments.single().type + if (itemsType.isMarkedNullable) return KotlinTypes.NULLABLE_ANY + val primitiveType = KotlinTypes.primitiveTypeByName(KotlinPsiUtil.getTypeWithoutTypeParameters(itemsType)) + return primitiveType ?: KotlinTypes.ANY + } + + return type.supertypes().asSequence() + .map(this::tryToFindElementType) + .firstOrNull() + } + + private fun defaultType(type: KotlinType): GenericType { + LOG.warn("Could not find type of items for type ${KotlinPsiUtil.getTypeName(type)}") + return if (type.isMarkedNullable) KotlinTypes.NULLABLE_ANY else KotlinTypes.ANY } } \ No newline at end of file