Implement SequenceTypeExtractor

This commit is contained in:
Vitaliy.Bibaev
2017-12-20 17:40:50 +03:00
committed by Yan Zhulanow
parent 6b83e2f49f
commit 5f9481b273
@@ -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
}
}