Templates "ifn" and "inn" to use kotlinVariable() instead of suggestVariableName()

This commit is contained in:
Valentin Kipyatkov
2015-11-19 10:47:19 +01:00
parent d4ba15fd90
commit 6bd723da11
3 changed files with 16 additions and 6 deletions
@@ -51,6 +51,7 @@ fun KotlinType.immediateSupertypes(): Collection<KotlinType> = TypeUtils.getImme
fun KotlinType.supertypes(): Collection<KotlinType> = 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)
@@ -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<ExpectedInfo>? {
val ifExpression = (expressionWithType.getParent() as? KtContainerNode)?.getParent() as? KtIfExpression ?: return null
return when (expressionWithType) {
+2 -2
View File
@@ -51,7 +51,7 @@
<template resource-bundle="org.jetbrains.kotlin.idea.KotlinBundle" key="livetemplate.description.ifn"
name="ifn" toReformat="true" toShortenFQNames="true"
value="if ($VAR$ == null) {&#10;$END$&#10;}">
<variable alwaysStopAt="true" defaultValue="&quot;i&quot;" expression="kotlinSuggestVariableName()" name="VAR" />
<variable alwaysStopAt="true" defaultValue="&quot;i&quot;" expression="kotlinVariable()" name="VAR" />
<context>
<option name="KOTLIN_EXPRESSION" value="true" />
</context>
@@ -59,7 +59,7 @@
<template resource-bundle="org.jetbrains.kotlin.idea.KotlinBundle" key="livetemplate.description.inn"
name="inn" toReformat="true" toShortenFQNames="true"
value="if ($VAR$ != null) {&#10;$END$&#10;}">
<variable alwaysStopAt="true" defaultValue="&quot;i&quot;" expression="kotlinSuggestVariableName()" name="VAR" />
<variable alwaysStopAt="true" defaultValue="&quot;i&quot;" expression="kotlinVariable()" name="VAR" />
<context>
<option name="KOTLIN_EXPRESSION" value="true" />
</context>