[kotlin] k2 extract function: escape names when needed

^KTIJ-30489 fixed

GitOrigin-RevId: 92113d3b7b5810cee7919ebf941fe0700601e4ce
This commit is contained in:
Anna Kozlova
2024-07-04 07:40:14 +00:00
committed by intellij-monorepo-bot
parent c082f1364a
commit ba28bc2b5e
5 changed files with 51 additions and 1 deletions
@@ -1177,6 +1177,11 @@ public abstract class ExtractionTestGenerated extends AbstractExtractionTest {
runTest("testData/refactoring/extractFunction/basic/privateSetter.kt");
}
@TestMetadata("quotedName.kt")
public void testQuotedName() throws Exception {
runTest("testData/refactoring/extractFunction/basic/quotedName.kt");
}
@TestMetadata("refInReturn.kt")
public void testRefInReturn() throws Exception {
runTest("testData/refactoring/extractFunction/basic/refInReturn.kt");
@@ -0,0 +1,18 @@
data class Person(val name: String, val pets: List<Pet>)
data class Pet(val name: String)
fun main() {
val <caret>`⍼` = listOf(
Person("Alice", listOf(Pet("Rex"), Pet("Fluffy"))),
Person("Bob", listOf(Pet("Max")))
)
`⍼`.forEach {
println("Person: ${it.name}")
it.pets.forEach {
println("Pet: ${it.name}")
}
}
}
// IGNORE_K1
@@ -0,0 +1,22 @@
data class Person(val name: String, val pets: List<Pet>)
data class Pet(val name: String)
fun main() {
val `⍼` = people()
`⍼`.forEach {
println("Person: ${it.name}")
it.pets.forEach {
println("Pet: ${it.name}")
}
}
}
private fun people(): List<Person> {
val `⍼` = listOf(
Person("Alice", listOf(Pet("Rex"), Pet("Fluffy"))),
Person("Bob", listOf(Pet("Max")))
)
return `⍼`
}
// IGNORE_K1
@@ -150,7 +150,7 @@ abstract class ExtractFunctionGenerator<KotlinType, ExtractionResult : IExtracti
is ExpressionValue -> resultExpression
is Jump -> if (it.conditional) psiFactory.createExpression("false") else null
is ParameterUpdate -> psiFactory.createExpression(it.parameter.nameForRef)
is Initializer -> psiFactory.createExpression(it.initializedDeclaration.name!!)
is Initializer -> psiFactory.createExpression(it.initializedDeclaration.name!!.quoteIfNeeded())
else -> throw IllegalArgumentException("Unknown output value: $it")
}
}
@@ -293,6 +293,11 @@ public abstract class K2IntroduceFunctionTestGenerated extends AbstractK2Introdu
runTest("../../idea/tests/testData/refactoring/extractFunction/basic/privateSetter.kt");
}
@TestMetadata("quotedName.kt")
public void testQuotedName() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunction/basic/quotedName.kt");
}
@TestMetadata("refInReturn.kt")
public void testRefInReturn() throws Exception {
runTest("../../idea/tests/testData/refactoring/extractFunction/basic/refInReturn.kt");