KT-12260 ISE while replacing an operator with safe call

#KT-12260 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-05-10 19:21:55 +03:00
parent 6e28c381c4
commit 3f82cc9b39
4 changed files with 35 additions and 7 deletions
@@ -20,10 +20,9 @@ import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class ReplaceInfixCallFix(element: KtBinaryExpression) : KotlinQuickFixAction<KtBinaryExpression>(element) {
@@ -33,9 +32,18 @@ class ReplaceInfixCallFix(element: KtBinaryExpression) : KotlinQuickFixAction<Kt
override fun getFamilyName() = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val newExpression = KtPsiFactory(file).createExpressionByPattern(
"$0?.$1($2)", element.left!!, element.operationReference, element.right!!)
element.replace(newExpression)
val psiFactory = KtPsiFactory(file)
if (element.operationToken == KtTokens.IDENTIFIER) {
val newExpression = psiFactory.createExpressionByPattern("$0?.$1($2)", element.left!!, element.operationReference, element.right!!)
element.replace(newExpression)
}
else {
val nameExpression = OperatorToFunctionIntention.convert(element).second
val callExpression = nameExpression.parent as KtCallExpression
val qualifiedExpression = callExpression.parent as KtDotQualifiedExpression
val safeExpression = psiFactory.createExpressionByPattern("$0?.$1", qualifiedExpression.receiverExpression, callExpression)
qualifiedExpression.replace(safeExpression)
}
}
override fun startInWriteAction() = true
@@ -0,0 +1,7 @@
// "Replace with safe (?.) call" "true"
// ERROR: Infix call corresponds to a dot-qualified call 'value?.compareTo(1).compareTo(0)' which is not allowed on a nullable receiver 'value?.compareTo(1)'. Use '?.'-qualified call instead
var foo: Int? = null
set(value) {
if(value <caret>< 1) {
}
}
@@ -0,0 +1,7 @@
// "Replace with safe (?.) call" "true"
// ERROR: Infix call corresponds to a dot-qualified call 'value?.compareTo(1).compareTo(0)' which is not allowed on a nullable receiver 'value?.compareTo(1)'. Use '?.'-qualified call instead
var foo: Int? = null
set(value) {
if(value?.compareTo(1) < 0) {
}
}
@@ -5835,6 +5835,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/nullables/unsafeInfixCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("operator.kt")
public void testOperator() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/nullables/unsafeInfixCall/operator.kt");
doTest(fileName);
}
@TestMetadata("unsafeInfixCall.kt")
public void testUnsafeInfixCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInfixCall.kt");