diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 5676a02090a..d719d238bfe 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -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) } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantLabelFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantLabelFix.kt new file mode 100644 index 00000000000..774e87189ac --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRedundantLabelFix.kt @@ -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(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? { + val labelReference = diagnostic.psiElement as? KtLabelReferenceExpression ?: return null + val labeledExpression = labelReference.getStrictParentOfType() ?: return null + return RemoveRedundantLabelFix(labeledExpression) + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeRedundantLabel/simple.kt b/idea/testData/quickfix/removeRedundantLabel/simple.kt new file mode 100644 index 00000000000..b7e98d531f4 --- /dev/null +++ b/idea/testData/quickfix/removeRedundantLabel/simple.kt @@ -0,0 +1,6 @@ +// "Remove redundant label" "true" +fun foo() { + L1@ val x = L2@ bar() +} + +fun bar() {} \ No newline at end of file diff --git a/idea/testData/quickfix/removeRedundantLabel/simple.kt.after b/idea/testData/quickfix/removeRedundantLabel/simple.kt.after new file mode 100644 index 00000000000..d0727310928 --- /dev/null +++ b/idea/testData/quickfix/removeRedundantLabel/simple.kt.after @@ -0,0 +1,6 @@ +// "Remove redundant label" "true" +fun foo() { + val x = L2@ bar() +} + +fun bar() {} \ No newline at end of file diff --git a/idea/testData/quickfix/removeRedundantLabel/simple2.kt b/idea/testData/quickfix/removeRedundantLabel/simple2.kt new file mode 100644 index 00000000000..a2ad86c81dc --- /dev/null +++ b/idea/testData/quickfix/removeRedundantLabel/simple2.kt @@ -0,0 +1,6 @@ +// "Remove redundant label" "true" +fun foo() { + L1@ val x = L2@ bar() +} + +fun bar() {} \ No newline at end of file diff --git a/idea/testData/quickfix/removeRedundantLabel/simple2.kt.after b/idea/testData/quickfix/removeRedundantLabel/simple2.kt.after new file mode 100644 index 00000000000..fb490244f07 --- /dev/null +++ b/idea/testData/quickfix/removeRedundantLabel/simple2.kt.after @@ -0,0 +1,6 @@ +// "Remove redundant label" "true" +fun foo() { + L1@ val x = bar() +} + +fun bar() {} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java index 8c07814bb94..ebbcc3e1d3b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java @@ -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) diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index e55ba25f76b..8ab259a3b98 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -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)