Make property initializer modification is OutOfBlock change
Fixed #KT-38762
This commit is contained in:
+18
-22
@@ -29,6 +29,7 @@ import com.intellij.psi.util.PsiModificationTracker
|
|||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
|
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getTopmostParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getTopmostParentOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
|
||||||
|
|
||||||
@@ -220,32 +221,27 @@ abstract class KotlinCodeBlockModificationListenerCompat(protected val project:
|
|||||||
// return BlockModificationScopeElement(it, it)
|
// return BlockModificationScopeElement(it, it)
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (blockDeclaration.typeReference != null) {
|
if (blockDeclaration.typeReference != null) {
|
||||||
val accessors =
|
|
||||||
blockDeclaration.accessors.map { it.initializer ?: it.bodyExpression }
|
|
||||||
|
|
||||||
val accessorList = if (blockDeclaration.initializer.isAncestor(element) &&
|
// adding annotations to accessor is the same as change contract of property
|
||||||
// call expression changes in property initializer are OCB, see KT-38443
|
if (element !is KtAnnotated || element.annotationEntries.isEmpty()) {
|
||||||
KtPsiUtil.getTopmostParentOfTypes(element, KtCallExpression::class.java) == null
|
|
||||||
) {
|
|
||||||
accessors + blockDeclaration.initializer
|
|
||||||
} else {
|
|
||||||
accessors
|
|
||||||
}
|
|
||||||
|
|
||||||
for (accessor in accessorList) {
|
val properExpression = blockDeclaration.accessors
|
||||||
accessor?.takeIf {
|
.firstOrNull { (it.initializer ?: it.bodyExpression).isAncestor(element) }
|
||||||
it.isAncestor(element) &&
|
?: blockDeclaration.initializer?.takeIf {
|
||||||
// adding annotations to accessor is the same as change contract of property
|
// name references changes in property initializer are OCB, see KT-38443, KT-38762
|
||||||
(element !is KtAnnotated || element.annotationEntries.isEmpty())
|
it.isAncestor(element) && !it.anyDescendantOfType<KtNameReferenceExpression>()
|
||||||
}
|
|
||||||
?.let { expression ->
|
|
||||||
val declaration =
|
|
||||||
KtPsiUtil.getTopmostParentOfTypes(blockDeclaration, KtClassOrObject::class.java) as? KtElement ?:
|
|
||||||
// ktFile to check top level property declarations
|
|
||||||
return null
|
|
||||||
return BlockModificationScopeElement(declaration, expression)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (properExpression != null) {
|
||||||
|
val declaration =
|
||||||
|
KtPsiUtil.getTopmostParentOfTypes(blockDeclaration, KtClassOrObject::class.java) as? KtElement
|
||||||
|
|
||||||
|
if (declaration != null) {
|
||||||
|
return BlockModificationScopeElement(declaration, properExpression)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// OUT_OF_CODE_BLOCK: TRUE
|
||||||
|
// TYPE: 'A'
|
||||||
|
// SKIP_ANALYZE_CHECK
|
||||||
|
|
||||||
|
class SomeClass(val paramA: Int) {
|
||||||
|
val someProperty: Int = param<caret>
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: it has to be non OCB !
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
// OUT_OF_CODE_BLOCK: FALSE
|
// OUT_OF_CODE_BLOCK: TRUE
|
||||||
// ERROR: Unresolved reference: apri
|
// ERROR: Unresolved reference: apri
|
||||||
|
// SKIP_ANALYZE_CHECK
|
||||||
|
|
||||||
class Test {
|
class Test {
|
||||||
val a : () -> Int = { <caret>pri }
|
val a : () -> Int = { <caret>pri }
|
||||||
|
|||||||
+5
@@ -118,6 +118,11 @@ public class OutOfBlockModificationTestGenerated extends AbstractOutOfBlockModif
|
|||||||
runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializer.kt");
|
runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializer.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("InClassPropertyInitializer2.kt")
|
||||||
|
public void testInClassPropertyInitializer2() throws Exception {
|
||||||
|
runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializer2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("InClassPropertyInitializerWithoutInference.kt")
|
@TestMetadata("InClassPropertyInitializerWithoutInference.kt")
|
||||||
public void testInClassPropertyInitializerWithoutInference() throws Exception {
|
public void testInClassPropertyInitializerWithoutInference() throws Exception {
|
||||||
runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference.kt");
|
runTest("idea/testData/codeInsight/outOfBlock/InClassPropertyInitializerWithoutInference.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user