Add quick fix to remove single lambda parameter if it's unused #KT-14593 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
2fb7b01320
commit
e10d10c49e
@@ -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)
|
||||
|
||||
@@ -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<KtParameter>(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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, <caret>u -> println(t) }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Remove single lambda parameter declaration" "true"
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun main() {
|
||||
listOf(1).forEach { <caret>x -> println() }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Remove single lambda parameter declaration" "true"
|
||||
// WITH_RUNTIME
|
||||
|
||||
fun main() {
|
||||
listOf(1).forEach { <caret>println() }
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user