diff --git a/idea/resources/inspectionDescriptions/RemoveEmptyParenthesesFromLambdaCall.html b/idea/resources/inspectionDescriptions/RemoveEmptyParenthesesFromLambdaCall.html new file mode 100644 index 00000000000..880c59d61c6 --- /dev/null +++ b/idea/resources/inspectionDescriptions/RemoveEmptyParenthesesFromLambdaCall.html @@ -0,0 +1,5 @@ + + +This inspection reports unnecessary parentheses of function calls where the only parameter is a lambda that's outside the parentheses. + + diff --git a/idea/resources/intentionDescriptions/RemoveEmptyParenthesesFromLambdaCallIntention/after.kt.template b/idea/resources/intentionDescriptions/RemoveEmptyParenthesesFromLambdaCallIntention/after.kt.template new file mode 100644 index 00000000000..cc5ec594b0f --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveEmptyParenthesesFromLambdaCallIntention/after.kt.template @@ -0,0 +1 @@ +forEach { } \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/RemoveEmptyParenthesesFromLambdaCallIntention/before.kt.template b/idea/resources/intentionDescriptions/RemoveEmptyParenthesesFromLambdaCallIntention/before.kt.template new file mode 100644 index 00000000000..9c6c9d0c06b --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveEmptyParenthesesFromLambdaCallIntention/before.kt.template @@ -0,0 +1 @@ +forEach() { } \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/RemoveEmptyParenthesesFromLambdaCallIntention/description.html b/idea/resources/intentionDescriptions/RemoveEmptyParenthesesFromLambdaCallIntention/description.html new file mode 100644 index 00000000000..996b1aea873 --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveEmptyParenthesesFromLambdaCallIntention/description.html @@ -0,0 +1,5 @@ + + +This intention removes unnecessary parentheses from function calls where the only parameter is a lambda that's outside the parentheses. + + \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 4fc43eb4805..a2b55ca662c 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1322,6 +1322,11 @@ Kotlin + + org.jetbrains.kotlin.idea.intentions.RemoveEmptyParenthesesFromLambdaCallIntention + Kotlin + + + + diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyParenthesesFromLambdaCallIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyParenthesesFromLambdaCallIntention.kt new file mode 100644 index 00000000000..40184f58ac4 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveEmptyParenthesesFromLambdaCallIntention.kt @@ -0,0 +1,45 @@ +/* + * 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.intentions + +import com.intellij.codeInspection.CleanupLocalInspectionTool +import com.intellij.codeInspection.ProblemHighlightType +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.idea.conversion.copy.range +import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection +import org.jetbrains.kotlin.psi.KtCallExpression +import org.jetbrains.kotlin.psi.KtValueArgumentList + +class RemoveEmptyParenthesesFromLambdaCallInspection : IntentionBasedInspection( + RemoveEmptyParenthesesFromLambdaCallIntention::class), CleanupLocalInspectionTool{ + override val problemHighlightType: ProblemHighlightType get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL +} + +class RemoveEmptyParenthesesFromLambdaCallIntention : SelfTargetingRangeIntention( + KtValueArgumentList::class.java, "Remove unnecessary parentheses from function call with lambda") { + + override fun applicabilityRange(element: KtValueArgumentList): TextRange? { + if (element.arguments.isNotEmpty()) return null + val parent = element.parent as? KtCallExpression ?: return null + return if (parent.lambdaArguments.count() == 1) element.range else null + } + + override fun applyTo(element: KtValueArgumentList, editor: Editor?) { + element.delete() + } +} diff --git a/idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/.intention b/idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/.intention new file mode 100644 index 00000000000..f43bfe48763 --- /dev/null +++ b/idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/.intention @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.intentions.RemoveEmptyParenthesesFromLambdaCallIntention \ No newline at end of file diff --git a/idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/simple.kt b/idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/simple.kt new file mode 100644 index 00000000000..7951e6718ae --- /dev/null +++ b/idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/simple.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun foo() { + listOf(1).forEach() { } +} diff --git a/idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/simple.kt.after b/idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/simple.kt.after new file mode 100644 index 00000000000..683d78a6bf9 --- /dev/null +++ b/idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/simple.kt.after @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +fun foo() { + listOf(1).forEach { } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 270fd38ab15..2aab44e3a87 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -9366,6 +9366,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } } + @TestMetadata("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class RemoveEmptyParenthesesFromLambdaCall extends AbstractIntentionTest { + public void testAllFilesPresentInRemoveEmptyParenthesesFromLambdaCall() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/simple.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/intentions/removeExplicitLambdaParameterTypes") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)