Call expression changes in property initializer are OCB

#KT-38443 Fixed
This commit is contained in:
Vladimir Dolzhenko
2020-04-22 21:41:38 +02:00
committed by Vladimir Dolzhenko
parent 1cc58518c0
commit 1565fc0211
5 changed files with 63 additions and 6 deletions
@@ -222,13 +222,23 @@ abstract class KotlinCodeBlockModificationListenerCompat(protected val project:
// }
if (blockDeclaration.typeReference != null) {
val accessors =
blockDeclaration.accessors.map { it.initializer ?: it.bodyExpression } + blockDeclaration.initializer
for (accessor in accessors) {
blockDeclaration.accessors.map { it.initializer ?: it.bodyExpression }
val accessorList = if (blockDeclaration.initializer.isAncestor(element) &&
// call expression changes in property initializer are OCB, see KT-38443
KtPsiUtil.getTopmostParentOfTypes(element, KtCallExpression::class.java) == null
) {
accessors + blockDeclaration.initializer
} else {
accessors
}
for (accessor in accessorList) {
accessor?.takeIf {
it.isAncestor(element) &&
// adding annotations to accessor is the same as change contract of property
(element !is KtAnnotated || element.annotationEntries.isEmpty())
}
it.isAncestor(element) &&
// adding annotations to accessor is the same as change contract of property
(element !is KtAnnotated || element.annotationEntries.isEmpty())
}
?.let { expression ->
val declaration =
KtPsiUtil.getTopmostParentOfTypes(blockDeclaration, KtClassOrObject::class.java) as? KtElement ?:
@@ -0,0 +1,10 @@
// OUT_OF_CODE_BLOCK: TRUE
// ERROR: No value passed for parameter 'i2'
// TYPE: '\b\b'
class Test {
val foo = bar(1, 2<caret>)
fun bar(i: Int, i2: Int) = ""
}
// TODO: it has to be non OCB !
@@ -0,0 +1,11 @@
// TODO: it has to be non OCB !
// OUT_OF_CODE_BLOCK: TRUE
// ERROR: No value passed for parameter 'i2'
// TYPE: '\b\b'
class Test {
val foo: String? = "a" + bar(1, 2<caret>)
fun bar(i: Int, i2: Int) = ""
}
// SKIP_ANALYZE_CHECK
@@ -0,0 +1,11 @@
// TODO: it has to be non OCB !
// OUT_OF_CODE_BLOCK: TRUE
// ERROR: The integer literal does not conform to the expected type String?
// TYPE: '\b\b\b\b'
class Test {
val foo: String? = "a"+<caret>1
fun bar(i: Int, i2: Int) = ""
}
// SKIP_ANALYZE_CHECK
@@ -113,6 +113,21 @@ public class OutOfBlockModificationTestGenerated extends AbstractOutOfBlockModif
runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyAccessor.kt");
}
@TestMetadata("InClassPropertyInitializer.kt")
public void testInClassPropertyInitializer() throws Exception {
runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializer.kt");
}
@TestMetadata("InClassPropertyInitializerWithoutInference.kt")
public void testInClassPropertyInitializerWithoutInference() throws Exception {
runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference.kt");
}
@TestMetadata("InClassPropertyInitializerWithoutInference2.kt")
public void testInClassPropertyInitializerWithoutInference2() throws Exception {
runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference2.kt");
}
@TestMetadata("InComment.kt")
public void testInComment() throws Exception {
runTest("idea/testData/codeInsight/outOfBlock/InComment.kt");