Add intention+inspection to remove empty parentheses from method call with single lambda parameter

Fixes #KT-13519
This commit is contained in:
Kirill Rakhman
2016-08-19 22:24:04 +02:00
parent a683c2b68d
commit 1bc2af6436
10 changed files with 96 additions and 0 deletions
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports unnecessary parentheses of function calls where the only parameter is a lambda that's outside the parentheses.
</body>
</html>
@@ -0,0 +1 @@
forEach<spot>()</spot> { }
@@ -0,0 +1,5 @@
<html>
<body>
This intention removes unnecessary parentheses from function calls where the only parameter is a lambda that's outside the parentheses.
</body>
</html>
+13
View File
@@ -1322,6 +1322,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveEmptyParenthesesFromLambdaCallIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -1731,6 +1736,14 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveEmptyParenthesesFromLambdaCallInspection"
displayName="Remove unnecessary parentheses from function call with lambda"
groupName="Kotlin"
enabledByDefault="true"
level="INFO"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -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<KtValueArgumentList>(
RemoveEmptyParenthesesFromLambdaCallIntention::class), CleanupLocalInspectionTool{
override val problemHighlightType: ProblemHighlightType get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL
}
class RemoveEmptyParenthesesFromLambdaCallIntention : SelfTargetingRangeIntention<KtValueArgumentList>(
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()
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.RemoveEmptyParenthesesFromLambdaCallIntention
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
listOf(1).forEach()<caret> { }
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
fun foo() {
listOf(1).forEach<caret> { }
}
@@ -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)