Skip to content

Commit

Permalink
Merge pull request #113 from suusan2go/issue_112
Browse files Browse the repository at this point in the history
use primary constructor to fill non-primitive value
  • Loading branch information
oboenikui authored Jul 19, 2023
2 parents 334d4cf + ce53255 commit f1bbee3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ open class FillClassFix(
}

val fqName = descriptor?.importableFqName?.asString()
val valueParameters =
descriptor?.constructors?.firstOrNull { it is ClassConstructorDescriptor }?.valueParameters
val valueParameters = descriptor?.constructors
?.sortedByDescending { it.isPrimary } // primary constructor first
?.firstOrNull { it is ClassConstructorDescriptor }
?.valueParameters
val argumentExpression = if (fqName != null && valueParameters != null) {
(factory.createExpression("$fqName()")).also {
val callExpression = it as? KtCallExpression ?: (it as? KtQualifiedExpression)?.callExpression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ class FillEmptyValueInspectionTest : BasePlatformTestCase() {
)
}

fun `test fill for non primitive types that has secondary constructor`() {
doAvailableTest(
"""
class A(a1: String, a2: Int) {
constructor(a1: String, a2: Int, a3: Boolean) : this(a1, a2)
}
class B(a: A)
fun test() {
B(<caret>)
}
""",
"""
class A(a1: String, a2: Int) {
constructor(a1: String, a2: Int, a3: Boolean) : this(a1, a2)
}
class B(a: A)
fun test() {
B(a = A(a1 = "", a2 = 0))
}
""",
)
}

fun `test fill for non primitive types and put argument on separate lines enabled`() {
doAvailableTest(
before = """
Expand Down

0 comments on commit f1bbee3

Please # to comment.