Add quickfix for SENSELESS_NULL_IN_WHEN to remove redundant when branch
#KT-24556 Fixed
This commit is contained in:
committed by
Yan Zhulanow
parent
668473c337
commit
5abb6bc7a5
@@ -1138,6 +1138,7 @@ remove.useless.is.check=Remove useless is check
|
||||
remove.val.var.from.parameter=Remove 'val/var' from parameter
|
||||
remove.0.from.parameter=Remove ''{0}'' from parameter
|
||||
remove.else.branch=Remove else branch
|
||||
remove.branch=Remove branch
|
||||
rename.identifier.fix.text=Rename
|
||||
rename.to.0=Rename to ''{0}''
|
||||
rename.parameter.to.match.overridden.method=Rename parameter to match overridden method
|
||||
|
||||
@@ -276,7 +276,8 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
ELSE_MISPLACED_IN_WHEN.registerFactory(MoveWhenElseBranchFix)
|
||||
NO_ELSE_IN_WHEN.registerFactory(AddWhenElseBranchFix)
|
||||
NO_ELSE_IN_WHEN.registerFactory(AddWhenRemainingBranchesFix)
|
||||
REDUNDANT_ELSE_IN_WHEN.registerFactory(RemoveWhenElseBranchFix)
|
||||
REDUNDANT_ELSE_IN_WHEN.registerFactory(RemoveWhenBranchFix)
|
||||
SENSELESS_NULL_IN_WHEN.registerFactory(RemoveWhenBranchFix)
|
||||
NON_EXHAUSTIVE_WHEN.registerFactory(AddWhenElseBranchFix)
|
||||
NON_EXHAUSTIVE_WHEN.registerFactory(AddWhenRemainingBranchesFix)
|
||||
NON_EXHAUSTIVE_WHEN_ON_SEALED_CLASS.registerFactory(AddWhenElseBranchFix)
|
||||
|
||||
+17
-4
@@ -19,12 +19,18 @@ package org.jetbrains.kotlin.idea.quickfix
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtWhenEntry
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
|
||||
class RemoveWhenElseBranchFix(element: KtWhenEntry) : KotlinQuickFixAction<KtWhenEntry>(element) {
|
||||
override fun getFamilyName() = KotlinBundle.message("remove.else.branch")
|
||||
class RemoveWhenBranchFix(element: KtWhenEntry) : KotlinQuickFixAction<KtWhenEntry>(element) {
|
||||
override fun getFamilyName() = if (element?.isElse == true) {
|
||||
KotlinBundle.message("remove.else.branch")
|
||||
} else {
|
||||
KotlinBundle.message("remove.branch")
|
||||
}
|
||||
|
||||
override fun getText() = familyName
|
||||
|
||||
@@ -33,8 +39,15 @@ class RemoveWhenElseBranchFix(element: KtWhenEntry) : KotlinQuickFixAction<KtWhe
|
||||
}
|
||||
|
||||
companion object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): RemoveWhenElseBranchFix? {
|
||||
return (diagnostic.psiElement as? KtWhenEntry)?.let { RemoveWhenElseBranchFix(it) }
|
||||
override fun createAction(diagnostic: Diagnostic): RemoveWhenBranchFix? {
|
||||
return when (diagnostic.factory) {
|
||||
Errors.REDUNDANT_ELSE_IN_WHEN ->
|
||||
(diagnostic.psiElement as? KtWhenEntry)?.let { RemoveWhenBranchFix(it) }
|
||||
Errors.SENSELESS_NULL_IN_WHEN ->
|
||||
diagnostic.psiElement.getStrictParentOfType<KtWhenEntry>()?.let { RemoveWhenBranchFix(it) }
|
||||
else ->
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Remove branch" "true"
|
||||
fun test(x: Int): String {
|
||||
return when (x) {
|
||||
1 -> "1"
|
||||
2 -> "2"
|
||||
<caret>null -> "null"
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Remove branch" "true"
|
||||
fun test(x: Int): String {
|
||||
return when (x) {
|
||||
1 -> "1"
|
||||
2 -> "2"
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
@@ -15517,6 +15517,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
runTest("idea/testData/quickfix/when/noElseInWhenWithoutBranches.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("removeRedundantBranch.kt")
|
||||
public void testRemoveRedundantBranch() throws Exception {
|
||||
runTest("idea/testData/quickfix/when/removeRedundantBranch.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("removeRedundantElse.kt")
|
||||
public void testRemoveRedundantElse() throws Exception {
|
||||
runTest("idea/testData/quickfix/when/removeRedundantElse.kt");
|
||||
|
||||
Reference in New Issue
Block a user