Fix invalidated element access exception in ObjectLiteralToLambdaIntention

#KT-36149 fixed
#KT-36152 fixed
This commit is contained in:
Ilya Kirillov
2020-02-14 10:02:36 +03:00
parent 6b913da698
commit 85be0450ba
7 changed files with 117 additions and 4 deletions
@@ -150,14 +150,14 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
}
val replaced = runWriteAction { element.replaced(newExpression) }
val callee = replaced.getCalleeExpressionIfAny()!! as KtNameReferenceExpression
val pointerToReplaced = replaced.createSmartPointer()
val callee = replaced.callee
val callExpression = callee.parent as KtCallExpression
val functionLiteral = callExpression.lambdaArguments.single().getLambdaExpression()!!
val returnLabel = callee.getReferencedNameAsName()
runWriteAction {
returnSaver.restore(functionLiteral, returnLabel)
commentSaver.restore(replaced, forceAdjustIndent = true/* by some reason lambda body is sometimes not properly indented */)
}
val parentCall = ((replaced.parent as? KtValueArgument)
?.parent as? KtValueArgumentList)
@@ -165,15 +165,27 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
if (parentCall != null && RedundantSamConstructorInspection.samConstructorCallsToBeConverted(parentCall)
.singleOrNull() == callExpression
) {
runWriteAction {
commentSaver.restore(replaced, forceAdjustIndent = true/* by some reason lambda body is sometimes not properly indented */)
}
RedundantSamConstructorInspection.replaceSamConstructorCall(callExpression)
if (parentCall.canMoveLambdaOutsideParentheses()) runWriteAction {
parentCall.moveFunctionLiteralOutsideParentheses()
}
} else {
val endOffset = (callee.parent as? KtCallExpression)?.typeArgumentList?.endOffset ?: callee.endOffset
ShortenReferences.DEFAULT.process(replaced.containingKtFile, replaced.startOffset, endOffset)
runWriteAction {
commentSaver.restore(replaced, forceAdjustIndent = true/* by some reason lambda body is sometimes not properly indented */)
}
pointerToReplaced.element?.let { replacedByPointer ->
val endOffset = (replacedByPointer.callee.parent as? KtCallExpression)?.typeArgumentList?.endOffset
?: replacedByPointer.callee.endOffset
ShortenReferences.DEFAULT.process(replacedByPointer.containingKtFile, replacedByPointer.startOffset, endOffset)
}
}
}
private val KtExpression.callee
get() = getCalleeExpressionIfAny() as KtNameReferenceExpression
}
private data class Data(