Move utility methods for primitive types into KotlinTypes

This commit is contained in:
Vitaliy.Bibaev
2017-12-20 17:31:54 +03:00
committed by Yan Zhulanow
parent b9ae33b068
commit 6b83e2f49f
2 changed files with 11 additions and 13 deletions
@@ -6,7 +6,6 @@ import com.intellij.debugger.streams.kotlin.psi.KotlinPsiUtil
import com.intellij.debugger.streams.kotlin.trace.dsl.KotlinTypes
import com.intellij.debugger.streams.kotlin.trace.dsl.KotlinTypes.ANY
import com.intellij.debugger.streams.kotlin.trace.dsl.KotlinTypes.NULLABLE_ANY
import com.intellij.debugger.streams.trace.impl.handler.type.ArrayType
import com.intellij.debugger.streams.trace.impl.handler.type.GenericType
import com.intellij.openapi.diagnostic.Logger
import org.jetbrains.kotlin.types.KotlinType
@@ -18,9 +17,6 @@ import org.jetbrains.kotlin.types.typeUtil.supertypes
class KotlinCollectionsTypeExtractor : CallTypeExtractor.Base() {
private companion object {
val LOG = Logger.getInstance(KotlinCollectionsTypeExtractor::class.java)
val primitives: Set<GenericType> = setOf(KotlinTypes.BOOLEAN, KotlinTypes.BYTE, KotlinTypes.INT, KotlinTypes.SHORT,
KotlinTypes.CHAR, KotlinTypes.LONG, KotlinTypes.FLOAT, KotlinTypes.DOUBLE)
val primitiveArrays: Set<ArrayType> = primitives.map(KotlinTypes::array).toSet()
}
override fun extractItemsType(type: KotlinType?): GenericType {
@@ -31,7 +27,7 @@ class KotlinCollectionsTypeExtractor : CallTypeExtractor.Base() {
override fun getResultType(type: KotlinType): GenericType {
val typeName = KotlinPsiUtil.getTypeWithoutTypeParameters(type)
return tryToGetPrimitiveByName(typeName) ?: tryToGetPrimitiveArrayByName(typeName) ?: getAny(type)
return KotlinTypes.primitiveTypeByName(typeName) ?: KotlinTypes.primitiveArrayByName(typeName) ?: getAny(type)
}
private fun tryToFindElementType(type: KotlinType): GenericType? {
@@ -40,13 +36,13 @@ class KotlinCollectionsTypeExtractor : CallTypeExtractor.Base() {
if (type.arguments.isEmpty()) return NULLABLE_ANY
val itemsType = type.arguments.first().type
if (itemsType.isMarkedNullable) return NULLABLE_ANY
val primitiveType = tryToGetPrimitiveByName(KotlinPsiUtil.getTypeWithoutTypeParameters(itemsType))
val primitiveType = KotlinTypes.primitiveTypeByName(KotlinPsiUtil.getTypeWithoutTypeParameters(itemsType))
return primitiveType ?: ANY
}
if (typeName == "kotlin.String" || typeName == "kotlin.CharSequence") return KotlinTypes.CHAR
val primitiveArray = tryToGetPrimitiveArrayByName(typeName)
val primitiveArray = KotlinTypes.primitiveArrayByName(typeName)
if (primitiveArray != null) return primitiveArray.elementType
return type.supertypes().asSequence()
@@ -60,10 +56,4 @@ class KotlinCollectionsTypeExtractor : CallTypeExtractor.Base() {
}
private fun getAny(type: KotlinType): GenericType = if (type.isMarkedNullable) NULLABLE_ANY else ANY
private fun tryToGetPrimitiveByName(name: String): GenericType? =
primitives.firstOrNull { x -> x.variableTypeName == name }
private fun tryToGetPrimitiveArrayByName(name: String): ArrayType? =
primitiveArrays.firstOrNull { it.variableTypeName == name }
}
@@ -64,5 +64,13 @@ object KotlinTypes : Types {
else -> ClassTypeImpl(type.genericTypeName + '?', type.defaultValue)
}
}
private val primitiveTypesIndex: Map<String, GenericType> = listOf(KotlinTypes.BOOLEAN, KotlinTypes.BYTE, KotlinTypes.INT, KotlinTypes.SHORT,
KotlinTypes.CHAR, KotlinTypes.LONG, KotlinTypes.FLOAT, KotlinTypes.DOUBLE).associate { it.genericTypeName to it }
private val primitiveArraysIndex: Map<String, ArrayType> = primitiveTypesIndex.asSequence().associate { it.key to array(it.value) }
fun primitiveTypeByName(typeName: String): GenericType? = primitiveTypesIndex[typeName]
fun primitiveArrayByName(typeName: String): ArrayType? = primitiveArraysIndex[typeName]
}