Create parameter: Forbid inplace refactoring if occurrences contain out-of-parentheses lambda arguments. Fix lambda argument replacement

#KT-9307 Fixed
This commit is contained in:
Alexey Sedunov
2015-09-28 18:27:41 +03:00
parent b805645489
commit 007c7c17f0
6 changed files with 35 additions and 2 deletions
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
import org.jetbrains.kotlin.idea.core.getResolutionScope
import org.jetbrains.kotlin.idea.core.moveInsideParenthesesAndReplaceWith
import org.jetbrains.kotlin.idea.core.refactoring.runRefactoringWithPostprocessing
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle
import org.jetbrains.kotlin.idea.refactoring.changeSignature.*
@@ -268,7 +269,10 @@ public open class KotlinIntroduceParameterHandler(
null,
fun() {
val isTestMode = ApplicationManager.getApplication().isUnitTestMode()
val inplaceIsAvailable = editor.getSettings().isVariableInplaceRenameEnabled() && !isTestMode
val haveLambdaArgumentsToReplace = occurrencesToReplace.any {
it.elements.any { it is JetFunctionLiteralExpression && it.parent is JetFunctionLiteralArgument }
}
val inplaceIsAvailable = editor.settings.isVariableInplaceRenameEnabled && !isTestMode && !haveLambdaArgumentsToReplace
val originalExpression = JetPsiUtil.safeDeparenthesize(expression)
val psiFactory = JetPsiFactory(project)
@@ -285,7 +289,16 @@ public open class KotlinIntroduceParameterHandler(
parametersUsages = parametersUsages,
occurrencesToReplace = occurrencesToReplace,
occurrenceReplacer = {
it.elements.single().replace(psiFactory.createExpression(newParameterName))
val expressionToReplace = it.elements.single() as JetExpression
val replacingExpression = psiFactory.createExpression(newParameterName)
if (expressionToReplace.isFunctionLiteralOutsideParentheses()) {
expressionToReplace
.getStrictParentOfType<JetFunctionLiteralArgument>()!!
.moveInsideParenthesesAndReplaceWith(replacingExpression, context)
}
else {
expressionToReplace.replace(replacingExpression)
}
}
)
)
@@ -0,0 +1,2 @@
// WITH_RUNTIME
fun foo(list: List<String>) = list.filter <selection>{ it.length() > 6 }</selection>
@@ -0,0 +1,2 @@
// WITH_RUNTIME
fun foo(list: List<String>, function: () -> (String) -> Boolean = { { it.length() > 6 } }) = list.filter(function())
@@ -0,0 +1,2 @@
// WITH_RUNTIME
fun foo(list: List<String>) = list.filter <selection>{ it.length() > 6 }</selection>
@@ -0,0 +1,2 @@
// WITH_RUNTIME
fun foo(list: List<String>, function: (String) -> Boolean = { it.length() > 6 }) = list.filter(function)
@@ -2749,6 +2749,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doIntroduceSimpleParameterTest(fileName);
}
@TestMetadata("lambdaArgument.kt")
public void testLambdaArgument() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceParameter/lambdaArgument.kt");
doIntroduceSimpleParameterTest(fileName);
}
@TestMetadata("localVar.kt")
public void testLocalVar() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceParameter/localVar.kt");
@@ -2890,6 +2896,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/refactoring/introduceLambdaParameter"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("lambdaArgument.kt")
public void testLambdaArgument() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceLambdaParameter/lambdaArgument.kt");
doIntroduceLambdaParameterTest(fileName);
}
@TestMetadata("lambdaParamInPrimaryConstructor.kt")
public void testLambdaParamInPrimaryConstructor() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceLambdaParameter/lambdaParamInPrimaryConstructor.kt");