From 6bd723da1156ec1a46b7a40c8c8cfe769770ab99 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 19 Nov 2015 10:47:19 +0100 Subject: [PATCH] Templates "ifn" and "inn" to use kotlinVariable() instead of suggestVariableName() --- .../src/org/jetbrains/kotlin/types/TypeUtils.kt | 1 + .../jetbrains/kotlin/idea/core/ExpectedInfos.kt | 17 +++++++++++++---- idea/resources/liveTemplates/Kotlin.xml | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt index 05087c6316f..a46ff9138a9 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeUtils.kt @@ -51,6 +51,7 @@ fun KotlinType.immediateSupertypes(): Collection = TypeUtils.getImme fun KotlinType.supertypes(): Collection = TypeUtils.getAllSupertypes(this) fun KotlinType.isNothing(): Boolean = KotlinBuiltIns.isNothing(this) +fun KotlinType.isNullableNothing(): Boolean = KotlinBuiltIns.isNullableNothing(this) fun KotlinType.isUnit(): Boolean = KotlinBuiltIns.isUnit(this) fun KotlinType.isAnyOrNullableAny(): Boolean = KotlinBuiltIns.isAnyOrNullableAny(this) fun KotlinType.isBoolean(): Boolean = KotlinBuiltIns.isBoolean(this) diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ExpectedInfos.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ExpectedInfos.kt index 31a5019c7ab..fae4db1cf39 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ExpectedInfos.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ExpectedInfos.kt @@ -38,9 +38,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeSubstitutor import org.jetbrains.kotlin.types.TypeUtils -import org.jetbrains.kotlin.types.typeUtil.containsError -import org.jetbrains.kotlin.types.typeUtil.makeNotNullable -import org.jetbrains.kotlin.types.typeUtil.makeNullable +import org.jetbrains.kotlin.types.typeUtil.* import org.jetbrains.kotlin.utils.addToStdlib.check import org.jetbrains.kotlin.utils.addToStdlib.singletonOrEmptyList import java.util.* @@ -399,18 +397,29 @@ class ExpectedInfos( if (otherOperand != null) { var expectedType = bindingContext.getType(otherOperand) ?: return null + val expectedName = expectedNameFromExpression(otherOperand) + + if (expectedType.isNullableNothing()) { // other operand is 'null' + return listOf(ExpectedInfo(NullableTypesFilter, expectedName, null)) + } + // if we complete argument of == or !=, make types in expected info's nullable to allow nullable items too if (operationToken in COMPARISON_TOKENS) { expectedType = expectedType.makeNullable() } - return listOf(ExpectedInfo(expectedType, expectedNameFromExpression(otherOperand), null)) + return listOf(ExpectedInfo(expectedType, expectedName, null)) } } } return null } + private object NullableTypesFilter : ByTypeFilter { + override fun matchingSubstitutor(descriptorType: FuzzyType) + = if (descriptorType.type.nullability() != TypeNullability.NOT_NULL) TypeSubstitutor.EMPTY else null + } + private fun calculateForIf(expressionWithType: KtExpression): Collection? { val ifExpression = (expressionWithType.getParent() as? KtContainerNode)?.getParent() as? KtIfExpression ?: return null return when (expressionWithType) { diff --git a/idea/resources/liveTemplates/Kotlin.xml b/idea/resources/liveTemplates/Kotlin.xml index cf5bc18248e..41acbddffba 100644 --- a/idea/resources/liveTemplates/Kotlin.xml +++ b/idea/resources/liveTemplates/Kotlin.xml @@ -51,7 +51,7 @@