Fix KNE in intentions

EA-124846 Fixed
This commit is contained in:
Andrey Uskov
2019-03-25 19:52:01 +03:00
parent e33ea24dc7
commit 01a100e2ef
3 changed files with 4 additions and 3 deletions
@@ -79,10 +79,10 @@ fun getLambdaByImplicitItReference(expression: KtNameReferenceExpression) =
resolveToAutoCreatedItDescriptor(expression)?.containingDeclaration?.source?.getPsi() as? KtFunctionLiteral resolveToAutoCreatedItDescriptor(expression)?.containingDeclaration?.source?.getPsi() as? KtFunctionLiteral
// returns assignment which replaces initializer // returns assignment which replaces initializer
fun splitPropertyDeclaration(property: KtProperty): KtBinaryExpression { fun splitPropertyDeclaration(property: KtProperty): KtBinaryExpression? {
val parent = property.parent val parent = property.parent
val initializer = property.initializer!! val initializer = property.initializer ?: return null
val explicitTypeToSet = if (property.typeReference != null) null else initializer.analyze().getType(initializer) val explicitTypeToSet = if (property.typeReference != null) null else initializer.analyze().getType(initializer)
@@ -36,6 +36,7 @@ class UnfoldPropertyToIfIntention : SelfTargetingRangeIntention<KtProperty>(KtPr
override fun applyTo(element: KtProperty, editor: Editor?) { override fun applyTo(element: KtProperty, editor: Editor?) {
val assignment = splitPropertyDeclaration(element) val assignment = splitPropertyDeclaration(element)
?: return // if element initializer is null the apply should not be invoked. If suddenly invoked splitPropertyDeclaration will return null
BranchedUnfoldingUtils.unfoldAssignmentToIf(assignment, editor) BranchedUnfoldingUtils.unfoldAssignmentToIf(assignment, editor)
} }
} }
@@ -38,7 +38,7 @@ class UnfoldPropertyToWhenIntention : SelfTargetingRangeIntention<KtProperty>(Kt
} }
override fun applyTo(element: KtProperty, editor: Editor?) { override fun applyTo(element: KtProperty, editor: Editor?) {
val assignment = splitPropertyDeclaration(element) val assignment = splitPropertyDeclaration(element) ?: return
BranchedUnfoldingUtils.unfoldAssignmentToWhen(assignment, editor) BranchedUnfoldingUtils.unfoldAssignmentToWhen(assignment, editor)
} }
} }