KT-17497 related: introduce fix for REDUNDANT_ELSE_IN_WHEN

This commit is contained in:
Mikhail Glukhikh
2017-04-28 15:15:39 +03:00
parent 14886827a2
commit 35754a98e7
5 changed files with 63 additions and 0 deletions
@@ -238,6 +238,7 @@ 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)
NON_EXHAUSTIVE_WHEN.registerFactory(AddWhenElseBranchFix)
NON_EXHAUSTIVE_WHEN.registerFactory(AddWhenRemainingBranchesFix)
BREAK_OR_CONTINUE_IN_WHEN.registerFactory(AddLoopLabelFix)
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.psi.KtFile
import org.jetbrains.kotlin.psi.KtWhenEntry
class RemoveWhenElseBranchFix(element: KtWhenEntry) : KotlinQuickFixAction<KtWhenEntry>(element) {
override fun getFamilyName() = "Remove else branch"
override fun getText() = familyName
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
element?.delete()
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): RemoveWhenElseBranchFix? {
return (diagnostic.psiElement as? KtWhenEntry)?.let { RemoveWhenElseBranchFix(it) }
}
}
}
+9
View File
@@ -0,0 +1,9 @@
// "Remove else branch" "true"
fun foo(b: Boolean) {
when (b) {
true -> return
false -> {}
<caret>else -> error()
}
}
@@ -0,0 +1,8 @@
// "Remove else branch" "true"
fun foo(b: Boolean) {
when (b) {
true -> return
false -> {}
}
}
@@ -10886,6 +10886,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("removeRedundantElse.kt")
public void testRemoveRedundantElse() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/when/removeRedundantElse.kt");
doTest(fileName);
}
@TestMetadata("twoElseBranchesInWhen.kt")
public void testTwoElseBranchesInWhen() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/when/twoElseBranchesInWhen.kt");