FIR IDE: RemoveNulalbleFix quickfix

This commit is contained in:
Tianyu Geng
2021-06-11 16:30:40 -07:00
committed by Ilya Kirillov
parent b10de3dd2d
commit 4bedf41f9c
10 changed files with 99 additions and 59 deletions
@@ -1171,7 +1171,7 @@ fun main(args: Array<String>) {
model("quickfix/expressions", pattern = pattern, filenameStartsLowerCase = true) model("quickfix/expressions", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/lateinit", pattern = pattern, filenameStartsLowerCase = true) model("quickfix/lateinit", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/modifiers", pattern = pattern, filenameStartsLowerCase = true, recursive = false) model("quickfix/modifiers", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
model("quickfix/nullables/unsafeInfixCall", pattern = pattern, filenameStartsLowerCase = true) model("quickfix/nullables", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/override", pattern = pattern, filenameStartsLowerCase = true, recursive = false) model("quickfix/override", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
model("quickfix/override/typeMismatchOnOverride", pattern = pattern, filenameStartsLowerCase = true, recursive = false) model("quickfix/override/typeMismatchOnOverride", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
model("quickfix/replaceInfixOrOperatorCall", pattern = pattern, filenameStartsLowerCase = true) model("quickfix/replaceInfixOrOperatorCall", pattern = pattern, filenameStartsLowerCase = true)
@@ -132,6 +132,9 @@ class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
registerApplicator(WrapWithSafeLetCallFixFactories.forUnsafeInfixCall) registerApplicator(WrapWithSafeLetCallFixFactories.forUnsafeInfixCall)
registerApplicator(WrapWithSafeLetCallFixFactories.forUnsafeOperatorCall) registerApplicator(WrapWithSafeLetCallFixFactories.forUnsafeOperatorCall)
registerApplicator(WrapWithSafeLetCallFixFactories.forArgumentTypeMismatch) registerApplicator(WrapWithSafeLetCallFixFactories.forArgumentTypeMismatch)
registerPsiQuickFixes(KtFirDiagnostic.NullableSupertype::class, RemoveNullableFix.removeForSuperType)
registerPsiQuickFixes(KtFirDiagnostic.InapplicableLateinitModifier::class, RemoveNullableFix.removeForLateInitProperty)
} }
private val returnTypes = KtQuickFixesListBuilder.registerPsiQuickFix { private val returnTypes = KtQuickFixesListBuilder.registerPsiQuickFix {
@@ -1133,6 +1133,38 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
} }
} }
@TestMetadata("idea/testData/quickfix/nullables")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Nullables extends AbstractHighLevelQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInNullables() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/nullables"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
}
@TestMetadata("removeRedundantNullable.kt")
public void testRemoveRedundantNullable() throws Exception {
runTest("idea/testData/quickfix/nullables/removeRedundantNullable.kt");
}
@TestMetadata("removeSupertypeNullable1.kt")
public void testRemoveSupertypeNullable1() throws Exception {
runTest("idea/testData/quickfix/nullables/removeSupertypeNullable1.kt");
}
@TestMetadata("removeSupertypeNullable2.kt")
public void testRemoveSupertypeNullable2() throws Exception {
runTest("idea/testData/quickfix/nullables/removeSupertypeNullable2.kt");
}
@TestMetadata("removeUselessNullable.kt")
public void testRemoveUselessNullable() throws Exception {
runTest("idea/testData/quickfix/nullables/removeUselessNullable.kt");
}
@TestMetadata("idea/testData/quickfix/nullables/unsafeInfixCall") @TestMetadata("idea/testData/quickfix/nullables/unsafeInfixCall")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)
@@ -1205,6 +1237,7 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeSet.kt"); runTest("idea/testData/quickfix/nullables/unsafeInfixCall/unsafeSet.kt");
} }
} }
}
@TestMetadata("idea/testData/quickfix/override") @TestMetadata("idea/testData/quickfix/override")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -8,10 +8,7 @@ package org.jetbrains.kotlin.idea.quickfix
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.idea.KotlinBundle import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNullableType
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class RemoveNullableFix(element: KtNullableType, private val typeOfError: NullableKind) : class RemoveNullableFix(element: KtNullableType, private val typeOfError: NullableKind) :
@@ -43,7 +40,10 @@ class RemoveNullableFix(element: KtNullableType, private val typeOfError: Nullab
return quickFixesPsiBasedFactory { e -> return quickFixesPsiBasedFactory { e ->
when (typeOfError) { when (typeOfError) {
NullableKind.REDUNDANT, NullableKind.SUPERTYPE, NullableKind.USELESS -> { NullableKind.REDUNDANT, NullableKind.SUPERTYPE, NullableKind.USELESS -> {
val nullType = e.getNonStrictParentOfType<KtNullableType>() val nullType: KtNullableType? = when (e) {
is KtTypeReference -> e.typeElement as? KtNullableType
else -> e.getNonStrictParentOfType()
}
if (nullType?.innerType == null) return@quickFixesPsiBasedFactory emptyList() if (nullType?.innerType == null) return@quickFixesPsiBasedFactory emptyList()
listOf(RemoveNullableFix(nullType, typeOfError)) listOf(RemoveNullableFix(nullType, typeOfError))
} }
-1
View File
@@ -3,4 +3,3 @@
class A() { class A() {
<caret>lateinit var foo: String? <caret>lateinit var foo: String?
} }
/* IGNORE_FIR */
-1
View File
@@ -3,4 +3,3 @@
class A() { class A() {
<caret>lateinit var foo: String <caret>lateinit var foo: String
} }
/* IGNORE_FIR */
@@ -2,3 +2,5 @@
fun main(args : Array<String>) { fun main(args : Array<String>) {
val x : Int??<caret> = 15 val x : Int??<caret> = 15
} }
/* IGNORE_FIR */
@@ -2,3 +2,5 @@
fun main(args : Array<String>) { fun main(args : Array<String>) {
val x : Int? = 15 val x : Int? = 15
} }
/* IGNORE_FIR */
@@ -2,3 +2,4 @@
fun f(a: Int) : Boolean { fun f(a: Int) : Boolean {
return a is Int?<caret> return a is Int?<caret>
} }
/* IGNORE_FIR */
@@ -2,3 +2,4 @@
fun f(a: Int) : Boolean { fun f(a: Int) : Boolean {
return a is Int<caret> return a is Int<caret>
} }
/* IGNORE_FIR */