Extract Function: Generate named argument for extracted lambda (when necessary)
This commit is contained in:
+1
@@ -305,6 +305,7 @@ fun ControlFlow.toDefault(): ControlFlow =
|
|||||||
|
|
||||||
data class ExtractableCodeDescriptor(
|
data class ExtractableCodeDescriptor(
|
||||||
val extractionData: ExtractionData,
|
val extractionData: ExtractionData,
|
||||||
|
val originalContext: BindingContext,
|
||||||
val name: String,
|
val name: String,
|
||||||
val visibility: String,
|
val visibility: String,
|
||||||
val parameters: List<Parameter>,
|
val parameters: List<Parameter>,
|
||||||
|
|||||||
+1
@@ -729,6 +729,7 @@ fun ExtractionData.performAnalysis(): AnalysisResult {
|
|||||||
return AnalysisResult(
|
return AnalysisResult(
|
||||||
ExtractableCodeDescriptor(
|
ExtractableCodeDescriptor(
|
||||||
this,
|
this,
|
||||||
|
bindingContext,
|
||||||
functionName,
|
functionName,
|
||||||
if (isVisibilityApplicable()) "private" else "",
|
if (isVisibilityApplicable()) "private" else "",
|
||||||
adjustedParameters.sortBy { it.name },
|
adjustedParameters.sortBy { it.name },
|
||||||
|
|||||||
@@ -193,7 +193,7 @@ fun ExtractableCodeDescriptor.findDuplicates(): List<DuplicateInfo> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun makeCall(
|
private fun makeCall(
|
||||||
name: String,
|
extractableDescriptor: ExtractableCodeDescriptor,
|
||||||
declaration: JetNamedDeclaration,
|
declaration: JetNamedDeclaration,
|
||||||
controlFlow: ControlFlow,
|
controlFlow: ControlFlow,
|
||||||
rangeToReplace: JetPsiRange,
|
rangeToReplace: JetPsiRange,
|
||||||
@@ -202,8 +202,7 @@ private fun makeCall(
|
|||||||
val firstExpression = rangeToReplace.elements.firstOrNull { it is JetExpression } as? JetExpression
|
val firstExpression = rangeToReplace.elements.firstOrNull { it is JetExpression } as? JetExpression
|
||||||
if (firstExpression?.isFunctionLiteralOutsideParentheses() ?: false) {
|
if (firstExpression?.isFunctionLiteralOutsideParentheses() ?: false) {
|
||||||
val functionLiteralArgument = PsiTreeUtil.getParentOfType(firstExpression, javaClass<JetFunctionLiteralArgument>())!!
|
val functionLiteralArgument = PsiTreeUtil.getParentOfType(firstExpression, javaClass<JetFunctionLiteralArgument>())!!
|
||||||
//todo use the right binding context
|
functionLiteralArgument.moveInsideParenthesesAndReplaceWith(wrappedCall, extractableDescriptor.originalContext)
|
||||||
functionLiteralArgument.moveInsideParenthesesAndReplaceWith(wrappedCall, BindingContext.EMPTY)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
anchor.replace(wrappedCall)
|
anchor.replace(wrappedCall)
|
||||||
@@ -223,8 +222,8 @@ private fun makeCall(
|
|||||||
|
|
||||||
val callText = when (declaration) {
|
val callText = when (declaration) {
|
||||||
is JetNamedFunction ->
|
is JetNamedFunction ->
|
||||||
arguments.joinToString(separator = ", ", prefix = "${name}(", postfix = ")")
|
arguments.joinToString(separator = ", ", prefix = "${extractableDescriptor.name}(", postfix = ")")
|
||||||
else -> name
|
else -> extractableDescriptor.name
|
||||||
}
|
}
|
||||||
|
|
||||||
val anchorInBlock = stream(anchor) { it.getParent() }.firstOrNull { it.getParent() is JetBlockExpression }
|
val anchorInBlock = stream(anchor) { it.getParent() }.firstOrNull { it.getParent() is JetBlockExpression }
|
||||||
@@ -512,9 +511,9 @@ fun ExtractableCodeDescriptor.generateDeclaration(options: ExtractionGeneratorOp
|
|||||||
|
|
||||||
if (options.inTempFile) return ExtractionResult(declaration, Collections.emptyMap(), nameByOffset)
|
if (options.inTempFile) return ExtractionResult(declaration, Collections.emptyMap(), nameByOffset)
|
||||||
|
|
||||||
makeCall(name, declaration, controlFlow, extractionData.originalRange, parameters.map { it.argumentText })
|
makeCall(this, declaration, controlFlow, extractionData.originalRange, parameters.map { it.argumentText })
|
||||||
ShortenReferences.process(declaration)
|
ShortenReferences.process(declaration)
|
||||||
|
|
||||||
val duplicateReplacers = duplicates.map { it.range to { makeCall(name, declaration, it.controlFlow, it.range, it.arguments) } }.toMap()
|
val duplicateReplacers = duplicates.map { it.range to { makeCall(this, declaration, it.controlFlow, it.range, it.arguments) } }.toMap()
|
||||||
return ExtractionResult(declaration, duplicateReplacers, nameByOffset)
|
return ExtractionResult(declaration, duplicateReplacers, nameByOffset)
|
||||||
}
|
}
|
||||||
+1
@@ -258,6 +258,7 @@ public class KotlinExtractFunctionDialog extends DialogWrapper {
|
|||||||
|
|
||||||
return new ExtractableCodeDescriptor(
|
return new ExtractableCodeDescriptor(
|
||||||
descriptor.getExtractionData(),
|
descriptor.getExtractionData(),
|
||||||
|
descriptor.getOriginalContext(),
|
||||||
getFunctionName(),
|
getFunctionName(),
|
||||||
getVisibility(),
|
getVisibility(),
|
||||||
ContainerUtil.newArrayList(oldToNewParameters.values()),
|
ContainerUtil.newArrayList(oldToNewParameters.values()),
|
||||||
|
|||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
fun <T> Array<T>.check(a: Int, b: Int, f: (T) -> Boolean): Boolean = false
|
||||||
|
|
||||||
|
// SIBLING:
|
||||||
|
fun foo(t: Array<Int>) {
|
||||||
|
t.check(a = 1, b = 2) <selection>{ it + 1 > 1 }</selection>
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
fun <T> Array<T>.check(a: Int, b: Int, f: (T) -> Boolean): Boolean = false
|
||||||
|
|
||||||
|
// SIBLING:
|
||||||
|
fun foo(t: Array<Int>) {
|
||||||
|
t.check(a = 1, b = 2, f = function())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun function(): (Int) -> Boolean {
|
||||||
|
return { it + 1 > 1 }
|
||||||
|
}
|
||||||
+6
@@ -665,6 +665,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
|||||||
doExtractFunctionTest(fileName);
|
doExtractFunctionTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("trailingLambdaNonEmptyArgListWithNamedArgs.kt")
|
||||||
|
public void testTrailingLambdaNonEmptyArgListWithNamedArgs() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/controlFlow/evaluateExpression/trailingLambdaNonEmptyArgListWithNamedArgs.kt");
|
||||||
|
doExtractFunctionTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/refactoring/extractFunction/controlFlow/initializer")
|
@TestMetadata("idea/testData/refactoring/extractFunction/controlFlow/initializer")
|
||||||
|
|||||||
Reference in New Issue
Block a user