[kotlin] k2: introduce engine: ensure that suggested names list is not empty

- move conflict resolution before another name suggestion by type

^KTIJ-29857 fixed

GitOrigin-RevId: 42bc3711099c7af57a94c881a79f2d4ff2a049f3
This commit is contained in:
Anna Kozlova
2024-05-07 15:55:48 +00:00
committed by intellij-monorepo-bot
parent fc0ced8006
commit a4a848cc58
18 changed files with 32 additions and 84 deletions
@@ -3481,6 +3481,11 @@ public abstract class ExtractionTestGenerated extends AbstractExtractionTest {
runTest("testData/refactoring/introduceParameter/classUsedParameter.kt");
}
@TestMetadata("conflictingName.kt")
public void testConflictingName() throws Exception {
runTest("testData/refactoring/introduceParameter/conflictingName.kt");
}
@TestMetadata("defaultParameter.kt")
public void testDefaultParameter() throws Exception {
runTest("testData/refactoring/introduceParameter/defaultParameter.kt");
@@ -1,9 +1,9 @@
fun foo() {
val a = 1
// SIBLING:
fun b(): Boolean = a > 0
fun bool1(): Boolean = a > 0
if (b()) {
if (bool1()) {
fun bool(): Int { return 0 }
println(bool())
}
@@ -1,8 +0,0 @@
// EXTRACTION_TARGET: property with initializer
val i = 1
private const val n = 2
fun foo(): Int {
return n
}
@@ -1,3 +0,0 @@
class Foo(i: Int, n: Int = 1 + 2) {
val x = n + 3
}
@@ -1,3 +0,0 @@
fun foo(i: Int, n: Int = 1 + 2) {
val x = n + 3
}
@@ -0,0 +1,5 @@
class A(val bool: Boolean) {
fun m() {
val p = <selection>false</selection>
}
}
@@ -0,0 +1,5 @@
class A(val bool: Boolean) {
fun m(b: Boolean = false) {
val p = b
}
}
@@ -0,0 +1,5 @@
class A(val bool: Boolean) {
fun m(bool1: Boolean = false) {
val p = bool1
}
}
@@ -3,10 +3,10 @@
fun p2() {
p(get42() + 1)
}
fun p(n: Int) {
println(n)
fun p(i1: Int) {
println(i1)
}
fun get42(): Int = 42
// IGNORE_K1
// IGNORE_K1
@@ -1,3 +0,0 @@
fun foo(i: Int, n: Int = 1) {
val u = n + i
}
@@ -1,3 +0,0 @@
fun foo(i: Int, n: Int = 1) {
val u = n + i
}
@@ -1,10 +0,0 @@
// EXTRACTION_TARGET: property with initializer
open class Base(protected val i: Int)
class Impl(z: Int) : Base(z) {
private val n = 2 + 3 + i
fun foo(): Int {
return n
}
}
@@ -1,11 +0,0 @@
// EXTRACTION_TARGET: property with initializer
class A {
val i = 1
private val n = 1 + 2
fun foo(): Int {
return n
}
}
@@ -1,16 +0,0 @@
// EXTRACTION_TARGET: property with initializer
class A {
val i: Int
init {
i = 1
}
private val n = 1 + 2
fun foo(): Int {
return n
}
}
@@ -1,9 +0,0 @@
// EXTRACTION_TARGET: property with initializer
val i = 1
private val n = 1 + 2
fun foo(): Int {
return n
}
@@ -1,12 +0,0 @@
// EXTRACTION_TARGET: property with getter
open class Base(protected val i: Int)
class Impl(i: Int) : Base(i) {
private val n: Int
get() = 2 + 3 + i
fun foo(): Int {
return n
}
}
@@ -19,8 +19,9 @@ object KotlinNameSuggester : IExtractionNameSuggester<KtType> {
if (kotlinType.isUnit) emptyList()
else KotlinNameSuggester()
.suggestTypeNames(kotlinType)
.filter(validator)
.map { KotlinNameSuggester.suggestNameByName(it, validator) }
.toList()
.takeIf { it.isNotEmpty() } ?: listOf(KotlinNameSuggester.suggestNameByName(defaultName ?: "p", validator))
}
override fun suggestNameByName(
@@ -245,6 +245,11 @@ public abstract class K2IntroduceParameterTestGenerated extends AbstractK2Introd
runTest("../../idea/tests/testData/refactoring/introduceParameter/classUsedParameter.kt");
}
@TestMetadata("conflictingName.kt")
public void testConflictingName() throws Exception {
runTest("../../idea/tests/testData/refactoring/introduceParameter/conflictingName.kt");
}
@TestMetadata("defaultParameter.kt")
public void testDefaultParameter() throws Exception {
runTest("../../idea/tests/testData/refactoring/introduceParameter/defaultParameter.kt");