Skip to content

Commit 1bc2af6

Browse files
committed
Add intention+inspection to remove empty parentheses from method call with single lambda parameter
Fixes #KT-13519
1 parent a683c2b commit 1bc2af6

File tree

10 files changed

+96
-0
lines changed

10 files changed

+96
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
This inspection reports unnecessary parentheses of function calls where the only parameter is a lambda that's outside the parentheses.
4+
</body>
5+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
forEach { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
forEach<spot>()</spot> { }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
This intention removes unnecessary parentheses from function calls where the only parameter is a lambda that's outside the parentheses.
4+
</body>
5+
</html>

idea/src/META-INF/plugin.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,11 @@
13221322
<category>Kotlin</category>
13231323
</intentionAction>
13241324

1325+
<intentionAction>
1326+
<className>org.jetbrains.kotlin.idea.intentions.RemoveEmptyParenthesesFromLambdaCallIntention</className>
1327+
<category>Kotlin</category>
1328+
</intentionAction>
1329+
13251330
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
13261331
displayName="Object literal can be converted to lambda"
13271332
groupName="Kotlin"
@@ -1731,6 +1736,14 @@
17311736
language="kotlin"
17321737
/>
17331738

1739+
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveEmptyParenthesesFromLambdaCallInspection"
1740+
displayName="Remove unnecessary parentheses from function call with lambda"
1741+
groupName="Kotlin"
1742+
enabledByDefault="true"
1743+
level="INFO"
1744+
language="kotlin"
1745+
/>
1746+
17341747
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
17351748

17361749
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2010-2016 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.kotlin.idea.intentions
18+
19+
import com.intellij.codeInspection.CleanupLocalInspectionTool
20+
import com.intellij.codeInspection.ProblemHighlightType
21+
import com.intellij.openapi.editor.Editor
22+
import com.intellij.openapi.util.TextRange
23+
import org.jetbrains.kotlin.idea.conversion.copy.range
24+
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
25+
import org.jetbrains.kotlin.psi.KtCallExpression
26+
import org.jetbrains.kotlin.psi.KtValueArgumentList
27+
28+
class RemoveEmptyParenthesesFromLambdaCallInspection : IntentionBasedInspection<KtValueArgumentList>(
29+
RemoveEmptyParenthesesFromLambdaCallIntention::class), CleanupLocalInspectionTool{
30+
override val problemHighlightType: ProblemHighlightType get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL
31+
}
32+
33+
class RemoveEmptyParenthesesFromLambdaCallIntention : SelfTargetingRangeIntention<KtValueArgumentList>(
34+
KtValueArgumentList::class.java, "Remove unnecessary parentheses from function call with lambda") {
35+
36+
override fun applicabilityRange(element: KtValueArgumentList): TextRange? {
37+
if (element.arguments.isNotEmpty()) return null
38+
val parent = element.parent as? KtCallExpression ?: return null
39+
return if (parent.lambdaArguments.count() == 1) element.range else null
40+
}
41+
42+
override fun applyTo(element: KtValueArgumentList, editor: Editor?) {
43+
element.delete()
44+
}
45+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.jetbrains.kotlin.idea.intentions.RemoveEmptyParenthesesFromLambdaCallIntention
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// WITH_RUNTIME
2+
3+
fun foo() {
4+
listOf(1).forEach()<caret> { }
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// WITH_RUNTIME
2+
3+
fun foo() {
4+
listOf(1).forEach<caret> { }
5+
}

idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9366,6 +9366,21 @@ public void testUnnecessaryBrackets6() throws Exception {
93669366
}
93679367
}
93689368

9369+
@TestMetadata("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall")
9370+
@TestDataPath("$PROJECT_ROOT")
9371+
@RunWith(JUnit3RunnerWithInners.class)
9372+
public static class RemoveEmptyParenthesesFromLambdaCall extends AbstractIntentionTest {
9373+
public void testAllFilesPresentInRemoveEmptyParenthesesFromLambdaCall() throws Exception {
9374+
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
9375+
}
9376+
9377+
@TestMetadata("simple.kt")
9378+
public void testSimple() throws Exception {
9379+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeEmptyParenthesesFromLambdaCall/simple.kt");
9380+
doTest(fileName);
9381+
}
9382+
}
9383+
93699384
@TestMetadata("idea/testData/intentions/removeExplicitLambdaParameterTypes")
93709385
@TestDataPath("$PROJECT_ROOT")
93719386
@RunWith(JUnit3RunnerWithInners.class)

0 commit comments

Comments
 (0)