Skip to content

Commit e10d10c

Browse files
cypressiousmglukhikh
authored andcommitted
Add quick fix to remove single lambda parameter if it's unused #KT-14593 Fixed
1 parent 2fb7b01 commit e10d10c

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ class QuickFixRegistrar : QuickFixContributor {
277277
NO_VALUE_FOR_PARAMETER.registerFactory(ChangeFunctionSignatureFix)
278278
UNUSED_PARAMETER.registerFactory(RemoveUnusedFunctionParameterFix)
279279
UNUSED_PARAMETER.registerFactory(RenameToUnderscoreFix.Factory)
280+
UNUSED_PARAMETER.registerFactory(RemoveSingleLambdaParameterFix)
280281
EXPECTED_PARAMETERS_NUMBER_MISMATCH.registerFactory(ChangeFunctionLiteralSignatureFix)
281282

282283
EXPECTED_PARAMETER_TYPE_MISMATCH.registerFactory(ChangeTypeFix)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.quickfix
18+
19+
import com.intellij.codeInsight.intention.IntentionAction
20+
import com.intellij.openapi.editor.Editor
21+
import com.intellij.openapi.project.Project
22+
import org.jetbrains.kotlin.diagnostics.Diagnostic
23+
import org.jetbrains.kotlin.lexer.KtTokens
24+
import org.jetbrains.kotlin.psi.KtFile
25+
import org.jetbrains.kotlin.psi.KtLambdaExpression
26+
import org.jetbrains.kotlin.psi.KtParameter
27+
import org.jetbrains.kotlin.psi.KtParameterList
28+
29+
class RemoveSingleLambdaParameterFix(element: KtParameter) : KotlinQuickFixAction<KtParameter>(element) {
30+
override fun getFamilyName() = "Remove single lambda parameter declaration"
31+
override fun getText() = familyName
32+
33+
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
34+
val parameterList = element.parent as? KtParameterList ?: return
35+
val ownerFunction = parameterList.ownerFunction ?: return
36+
val arrow = ownerFunction.node.findChildByType(KtTokens.ARROW) ?: return
37+
38+
parameterList.delete()
39+
ownerFunction.node.removeChild(arrow)
40+
}
41+
42+
companion object : KotlinSingleIntentionActionFactory() {
43+
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
44+
val parameter = diagnostic.psiElement as? KtParameter ?: return null
45+
val parameterList = parameter.parent as? KtParameterList ?: return null
46+
47+
if (parameterList.parameters.size != 1) return null
48+
49+
if (parameterList.parent?.parent !is KtLambdaExpression) return null
50+
51+
return RemoveSingleLambdaParameterFix(parameter)
52+
}
53+
}
54+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// "Remove single lambda parameter declaration" "false"
2+
// ACTION: Move lambda argument into parentheses
3+
// ACTION: Rename to _
4+
// ACTION: Specify explicit lambda signature
5+
// ACTION: Specify type explicitly
6+
// WITH_RUNTIME
7+
8+
fun main() {
9+
mapOf(1 to 2).forEach { t, <caret>u -> println(t) }
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// "Remove single lambda parameter declaration" "true"
2+
// WITH_RUNTIME
3+
4+
fun main() {
5+
listOf(1).forEach { <caret>x -> println() }
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// "Remove single lambda parameter declaration" "true"
2+
// WITH_RUNTIME
3+
4+
fun main() {
5+
listOf(1).forEach { <caret>println() }
6+
}

idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7272,6 +7272,27 @@ public void testSimpleRef() throws Exception {
72727272
}
72737273
}
72747274

7275+
@TestMetadata("idea/testData/quickfix/removeSingleLambdaParameter")
7276+
@TestDataPath("$PROJECT_ROOT")
7277+
@RunWith(JUnit3RunnerWithInners.class)
7278+
public static class RemoveSingleLambdaParameter extends AbstractQuickFixTest {
7279+
public void testAllFilesPresentInRemoveSingleLambdaParameter() throws Exception {
7280+
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeSingleLambdaParameter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
7281+
}
7282+
7283+
@TestMetadata("multiple.kt")
7284+
public void testMultiple() throws Exception {
7285+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeSingleLambdaParameter/multiple.kt");
7286+
doTest(fileName);
7287+
}
7288+
7289+
@TestMetadata("simple.kt")
7290+
public void testSimple() throws Exception {
7291+
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/removeSingleLambdaParameter/simple.kt");
7292+
doTest(fileName);
7293+
}
7294+
}
7295+
72757296
@TestMetadata("idea/testData/quickfix/removeToStringInStringTemplate")
72767297
@TestDataPath("$PROJECT_ROOT")
72777298
@RunWith(JUnit3RunnerWithInners.class)

0 commit comments

Comments
 (0)