diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index b0bf1bf5966..195473926c2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -277,6 +277,7 @@ class QuickFixRegistrar : QuickFixContributor { NO_VALUE_FOR_PARAMETER.registerFactory(ChangeFunctionSignatureFix) UNUSED_PARAMETER.registerFactory(RemoveUnusedFunctionParameterFix) UNUSED_PARAMETER.registerFactory(RenameToUnderscoreFix.Factory) + UNUSED_PARAMETER.registerFactory(RemoveSingleLambdaParameterFix) EXPECTED_PARAMETERS_NUMBER_MISMATCH.registerFactory(ChangeFunctionLiteralSignatureFix) EXPECTED_PARAMETER_TYPE_MISMATCH.registerFactory(ChangeTypeFix) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveSingleLambdaParameterFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveSingleLambdaParameterFix.kt new file mode 100644 index 00000000000..974754b980d --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveSingleLambdaParameterFix.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2010-2016 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.codeInsight.intention.IntentionAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtLambdaExpression +import org.jetbrains.kotlin.psi.KtParameter +import org.jetbrains.kotlin.psi.KtParameterList + +class RemoveSingleLambdaParameterFix(element: KtParameter) : KotlinQuickFixAction(element) { + override fun getFamilyName() = "Remove single lambda parameter declaration" + override fun getText() = familyName + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + val parameterList = element.parent as? KtParameterList ?: return + val ownerFunction = parameterList.ownerFunction ?: return + val arrow = ownerFunction.node.findChildByType(KtTokens.ARROW) ?: return + + parameterList.delete() + ownerFunction.node.removeChild(arrow) + } + + companion object : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val parameter = diagnostic.psiElement as? KtParameter ?: return null + val parameterList = parameter.parent as? KtParameterList ?: return null + + if (parameterList.parameters.size != 1) return null + + if (parameterList.parent?.parent !is KtLambdaExpression) return null + + return RemoveSingleLambdaParameterFix(parameter) + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeSingleLambdaParameter/multiple.kt b/idea/testData/quickfix/removeSingleLambdaParameter/multiple.kt new file mode 100644 index 00000000000..76d32ab8dda --- /dev/null +++ b/idea/testData/quickfix/removeSingleLambdaParameter/multiple.kt @@ -0,0 +1,10 @@ +// "Remove single lambda parameter declaration" "false" +// ACTION: Move lambda argument into parentheses +// ACTION: Rename to _ +// ACTION: Specify explicit lambda signature +// ACTION: Specify type explicitly +// WITH_RUNTIME + +fun main() { + mapOf(1 to 2).forEach { t, u -> println(t) } +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeSingleLambdaParameter/simple.kt b/idea/testData/quickfix/removeSingleLambdaParameter/simple.kt new file mode 100644 index 00000000000..51bf0613e19 --- /dev/null +++ b/idea/testData/quickfix/removeSingleLambdaParameter/simple.kt @@ -0,0 +1,6 @@ +// "Remove single lambda parameter declaration" "true" +// WITH_RUNTIME + +fun main() { + listOf(1).forEach { x -> println() } +} \ No newline at end of file diff --git a/idea/testData/quickfix/removeSingleLambdaParameter/simple.kt.after b/idea/testData/quickfix/removeSingleLambdaParameter/simple.kt.after new file mode 100644 index 00000000000..c96c5607adf --- /dev/null +++ b/idea/testData/quickfix/removeSingleLambdaParameter/simple.kt.after @@ -0,0 +1,6 @@ +// "Remove single lambda parameter declaration" "true" +// WITH_RUNTIME + +fun main() { + listOf(1).forEach { println() } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index c11eb62d347..f88a87fec20 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -7272,6 +7272,27 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/removeSingleLambdaParameter") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveSingleLambdaParameter extends AbstractQuickFixTest { + public void testAllFilesPresentInRemoveSingleLambdaParameter() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeSingleLambdaParameter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("multiple.kt") + public void testMultiple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeSingleLambdaParameter/multiple.kt"); + doTest(fileName); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeSingleLambdaParameter/simple.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/removeToStringInStringTemplate") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)