Add "Remove redundant label" quick fix for REDUNDANT_LABEL_WARNING

#KT-26431 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-09-11 00:35:43 +09:00
committed by Dmitry Gridin
parent 18df5d9db0
commit b2d2165342
8 changed files with 96 additions and 0 deletions
@@ -613,5 +613,7 @@ class QuickFixRegistrar : QuickFixContributor {
NO_VALUE_FOR_PARAMETER.registerFactory(AddConstructorParameterFromSuperTypeCallFix)
UNEXPECTED_TRAILING_LAMBDA_ON_A_NEW_LINE.registerFactory(AddSemicolonBeforeLambdaExpressionFix.Factory)
REDUNDANT_LABEL_WARNING.registerFactory(RemoveRedundantLabelFix)
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
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.KtLabelReferenceExpression
import org.jetbrains.kotlin.psi.KtLabeledExpression
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class RemoveRedundantLabelFix(element: KtLabeledExpression) : KotlinQuickFixAction<KtLabeledExpression>(element) {
override fun getText(): String = "Remove redundant label"
override fun getFamilyName(): String = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val labeledExpression = element ?: return
val baseExpression = labeledExpression.baseExpression ?: return
labeledExpression.replace(baseExpression)
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtLabeledExpression>? {
val labelReference = diagnostic.psiElement as? KtLabelReferenceExpression ?: return null
val labeledExpression = labelReference.getStrictParentOfType<KtLabeledExpression>() ?: return null
return RemoveRedundantLabelFix(labeledExpression)
}
}
}
+6
View File
@@ -0,0 +1,6 @@
// "Remove redundant label" "true"
fun foo() {
<caret>L1@ val x = L2@ bar()
}
fun bar() {}
@@ -0,0 +1,6 @@
// "Remove redundant label" "true"
fun foo() {
val x = L2@ bar()
}
fun bar() {}
@@ -0,0 +1,6 @@
// "Remove redundant label" "true"
fun foo() {
L1@ val x = L2@<caret> bar()
}
fun bar() {}
@@ -0,0 +1,6 @@
// "Remove redundant label" "true"
fun foo() {
L1@ val x = bar()
}
fun bar() {}
@@ -3673,6 +3673,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
}
}
@TestMetadata("idea/testData/quickfix/removeRedundantLabel")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveRedundantLabel extends AbstractQuickFixMultiFileTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInRemoveRedundantLabel() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeRedundantLabel"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
}
}
@TestMetadata("idea/testData/quickfix/removeSingleLambdaParameter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -10303,6 +10303,29 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/removeRedundantLabel")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveRedundantLabel extends AbstractQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInRemoveRedundantLabel() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeRedundantLabel"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/quickfix/removeRedundantLabel/simple.kt");
}
@TestMetadata("simple2.kt")
public void testSimple2() throws Exception {
runTest("idea/testData/quickfix/removeRedundantLabel/simple2.kt");
}
}
@TestMetadata("idea/testData/quickfix/removeSingleLambdaParameter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)