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:
+15
-2
@@ -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.KotlinNameSuggester
|
||||||
import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
|
import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
|
||||||
import org.jetbrains.kotlin.idea.core.getResolutionScope
|
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.core.refactoring.runRefactoringWithPostprocessing
|
||||||
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle
|
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle
|
||||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.*
|
import org.jetbrains.kotlin.idea.refactoring.changeSignature.*
|
||||||
@@ -268,7 +269,10 @@ public open class KotlinIntroduceParameterHandler(
|
|||||||
null,
|
null,
|
||||||
fun() {
|
fun() {
|
||||||
val isTestMode = ApplicationManager.getApplication().isUnitTestMode()
|
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 originalExpression = JetPsiUtil.safeDeparenthesize(expression)
|
||||||
val psiFactory = JetPsiFactory(project)
|
val psiFactory = JetPsiFactory(project)
|
||||||
@@ -285,7 +289,16 @@ public open class KotlinIntroduceParameterHandler(
|
|||||||
parametersUsages = parametersUsages,
|
parametersUsages = parametersUsages,
|
||||||
occurrencesToReplace = occurrencesToReplace,
|
occurrencesToReplace = occurrencesToReplace,
|
||||||
occurrenceReplacer = {
|
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)
|
||||||
+12
@@ -2749,6 +2749,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
|||||||
doIntroduceSimpleParameterTest(fileName);
|
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")
|
@TestMetadata("localVar.kt")
|
||||||
public void testLocalVar() throws Exception {
|
public void testLocalVar() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceParameter/localVar.kt");
|
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);
|
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")
|
@TestMetadata("lambdaParamInPrimaryConstructor.kt")
|
||||||
public void testLambdaParamInPrimaryConstructor() throws Exception {
|
public void testLambdaParamInPrimaryConstructor() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceLambdaParameter/lambdaParamInPrimaryConstructor.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceLambdaParameter/lambdaParamInPrimaryConstructor.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user