KT-12260 ISE while replacing an operator with safe call
#KT-12260 Fixed
This commit is contained in:
@@ -20,10 +20,9 @@ import com.intellij.codeInsight.intention.IntentionAction
|
|||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
import org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||||
|
|
||||||
class ReplaceInfixCallFix(element: KtBinaryExpression) : KotlinQuickFixAction<KtBinaryExpression>(element) {
|
class ReplaceInfixCallFix(element: KtBinaryExpression) : KotlinQuickFixAction<KtBinaryExpression>(element) {
|
||||||
@@ -33,9 +32,18 @@ class ReplaceInfixCallFix(element: KtBinaryExpression) : KotlinQuickFixAction<Kt
|
|||||||
override fun getFamilyName() = text
|
override fun getFamilyName() = text
|
||||||
|
|
||||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||||
val newExpression = KtPsiFactory(file).createExpressionByPattern(
|
val psiFactory = KtPsiFactory(file)
|
||||||
"$0?.$1($2)", element.left!!, element.operationReference, element.right!!)
|
if (element.operationToken == KtTokens.IDENTIFIER) {
|
||||||
element.replace(newExpression)
|
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
|
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);
|
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")
|
@TestMetadata("unsafeInfixCall.kt")
|
||||||
public void testUnsafeInfixCall() throws Exception {
|
public void testUnsafeInfixCall() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInfixCall.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeInfixCall.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user