diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 2ca10a53ab2..2aafdadea70 100755 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -374,6 +374,7 @@ fun main(args: Array) { model("codeInsight/unwrapAndRemove/unwrapFinally", testMethod = "doTestFinallyUnwrapper") model("codeInsight/unwrapAndRemove/removeFinally", testMethod = "doTestFinallyRemover") model("codeInsight/unwrapAndRemove/unwrapLambda", testMethod = "doTestLambdaUnwrapper") + model("codeInsight/unwrapAndRemove/unwrapFunctionParameter", testMethod = "doTestFunctionParameterUnwrapper") } testClass { diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/unwrap/KotlinFunctionParameterUnwrapper.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/unwrap/KotlinFunctionParameterUnwrapper.kt new file mode 100644 index 00000000000..f1155f99a5a --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/unwrap/KotlinFunctionParameterUnwrapper.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.codeInsight.unwrap + +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.psi.KtCallExpression +import org.jetbrains.kotlin.psi.KtValueArgument +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType + +class KotlinFunctionParameterUnwrapper(key: String) : KotlinUnwrapRemoveBase(key) { + + override fun isApplicableTo(element: PsiElement): Boolean { + if (element !is KtCallExpression) return false + if (element.valueArguments.size != 1) return false + val argument = element.parent as? KtValueArgument ?: return false + if (argument.getStrictParentOfType() == null) return false + return true + } + + override fun doUnwrap(element: PsiElement?, context: Context?) { + val function = element as? KtCallExpression ?: return + val argument = element.valueArguments.firstOrNull()?.getArgumentExpression() ?: return + context?.extractFromExpression(argument, function) + context?.delete(function) + } + +} diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/unwrap/KotlinUnwrapDescriptor.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/unwrap/KotlinUnwrapDescriptor.java index 035951b0360..9e262f7e67f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/unwrap/KotlinUnwrapDescriptor.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/unwrap/KotlinUnwrapDescriptor.java @@ -34,6 +34,7 @@ public class KotlinUnwrapDescriptor extends UnwrapDescriptorBase { new KotlinUnwrappers.KotlinFinallyUnwrapper("unwrap.expression"), new KotlinUnwrappers.KotlinFinallyRemover("remove.expression"), new KotlinLambdaUnwrapper("unwrap.expression"), + new KotlinFunctionParameterUnwrapper("unwrap.expression") }; } } diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasMultiParam.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasMultiParam.kt new file mode 100644 index 00000000000..3b95f3d5f50 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasMultiParam.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false +fun test() { + val i = 1 + foo(baz(i, 2)) +} + +fun foo(i: Int) {} + +fun baz(i: Int, j: Int) = 1 diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasSingleParam.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasSingleParam.kt new file mode 100644 index 00000000000..e6b3435007a --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasSingleParam.kt @@ -0,0 +1,9 @@ +// OPTION: 0 +fun test() { + val i = 1 + foo(bar(1)) +} + +fun foo(i: Int) {} + +fun bar(i: Int) = 1 diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasSingleParam.kt.after b/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasSingleParam.kt.after new file mode 100644 index 00000000000..06a34719a93 --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasSingleParam.kt.after @@ -0,0 +1,9 @@ +// OPTION: 0 +fun test() { + val i = 1 + foo(1) +} + +fun foo(i: Int) {} + +fun bar(i: Int) = 1 diff --git a/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionWithReceiver.kt b/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionWithReceiver.kt new file mode 100644 index 00000000000..dc76e5031ac --- /dev/null +++ b/idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionWithReceiver.kt @@ -0,0 +1,12 @@ +// IS_APPLICABLE: false +fun test() { + val i = 1 + val test = Test() + foo(test.qux(i)) +} + +fun foo(i: Int) {} + +class Test { + fun qux(i: Int) = 1 +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/unwrap/AbstractUnwrapRemoveTest.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/unwrap/AbstractUnwrapRemoveTest.java index e8679a21883..d176fec6959 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/unwrap/AbstractUnwrapRemoveTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/unwrap/AbstractUnwrapRemoveTest.java @@ -75,6 +75,10 @@ public abstract class AbstractUnwrapRemoveTest extends LightCodeInsightTestCase doTest(path, KotlinLambdaUnwrapper.class); } + public void doTestFunctionParameterUnwrapper(@NotNull String path) throws Exception { + doTest(path, KotlinFunctionParameterUnwrapper.class); + } + private void doTest(@NotNull String path, final Class unwrapperClass) throws Exception { configureByFile(path); diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/unwrap/UnwrapRemoveTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/unwrap/UnwrapRemoveTestGenerated.java index e2dd370c696..e7756e86051 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/unwrap/UnwrapRemoveTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/unwrap/UnwrapRemoveTestGenerated.java @@ -339,4 +339,31 @@ public class UnwrapRemoveTestGenerated extends AbstractUnwrapRemoveTest { doTestLambdaUnwrapper(fileName); } } + + @TestMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class UnwrapFunctionParameter extends AbstractUnwrapRemoveTest { + public void testAllFilesPresentInUnwrapFunctionParameter() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("functionHasMultiParam.kt") + public void testFunctionHasMultiParam() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasMultiParam.kt"); + doTestFunctionParameterUnwrapper(fileName); + } + + @TestMetadata("functionHasSingleParam.kt") + public void testFunctionHasSingleParam() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionHasSingleParam.kt"); + doTestFunctionParameterUnwrapper(fileName); + } + + @TestMetadata("functionWithReceiver.kt") + public void testFunctionWithReceiver() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/unwrapAndRemove/unwrapFunctionParameter/functionWithReceiver.kt"); + doTestFunctionParameterUnwrapper(fileName); + } + } }