Unwrap: function parameter (KT-14788)
#KT-14788 Fixed
This commit is contained in:
committed by
Nikolay Krasko
parent
6838873a93
commit
c1f43558b9
@@ -374,6 +374,7 @@ fun main(args: Array<String>) {
|
||||
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<AbstractExpressionTypeTest> {
|
||||
|
||||
+30
@@ -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<KtCallExpression>() == 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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun test() {
|
||||
val i = 1
|
||||
foo(baz<caret>(i, 2))
|
||||
}
|
||||
|
||||
fun foo(i: Int) {}
|
||||
|
||||
fun baz(i: Int, j: Int) = 1
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// OPTION: 0
|
||||
fun test() {
|
||||
val i = 1
|
||||
foo(bar<caret>(1))
|
||||
}
|
||||
|
||||
fun foo(i: Int) {}
|
||||
|
||||
fun bar(i: Int) = 1
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// OPTION: 0
|
||||
fun test() {
|
||||
val i = 1
|
||||
foo(1<caret>)
|
||||
}
|
||||
|
||||
fun foo(i: Int) {}
|
||||
|
||||
fun bar(i: Int) = 1
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun test() {
|
||||
val i = 1
|
||||
val test = Test()
|
||||
foo(test.qux<caret>(i))
|
||||
}
|
||||
|
||||
fun foo(i: Int) {}
|
||||
|
||||
class Test {
|
||||
fun qux(i: Int) = 1
|
||||
}
|
||||
@@ -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<? extends Unwrapper> unwrapperClass) throws Exception {
|
||||
configureByFile(path);
|
||||
|
||||
|
||||
+27
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user