"Convert to primary constructor": do not move init section down
So #KT-19629 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
9a1253c5c7
commit
a41064295b
+13
-3
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.intentions
|
|||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import com.intellij.psi.search.searches.ReferencesSearch
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithContent
|
import org.jetbrains.kotlin.idea.caches.resolve.analyzeWithContent
|
||||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||||
@@ -181,10 +182,19 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingRangeIntentio
|
|||||||
with (constructorInClass.replace(constructor)) {
|
with (constructorInClass.replace(constructor)) {
|
||||||
constructorCommentSaver.restore(this)
|
constructorCommentSaver.restore(this)
|
||||||
}
|
}
|
||||||
element.delete()
|
|
||||||
|
|
||||||
if ((initializer.body as? KtBlockExpression)?.statements?.isNotEmpty() ?: false) {
|
if ((initializer.body as? KtBlockExpression)?.statements?.isNotEmpty() == true) {
|
||||||
klass.addDeclaration(initializer)
|
val hasPropertyAfterInitializer = element.parent.children.takeLastWhile { it !== element }.any {
|
||||||
|
it is KtProperty && ReferencesSearch.search(it, initializer.useScope).findFirst() != null
|
||||||
|
}
|
||||||
|
if (hasPropertyAfterInitializer) {
|
||||||
|
// In this case we must move init {} down, because it uses a property declared below
|
||||||
|
klass.addDeclaration(initializer)
|
||||||
|
} else {
|
||||||
|
klass.addDeclarationAfter(initializer, element)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
element.delete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,4 +7,6 @@ class ConvertToInit {
|
|||||||
foo()
|
foo()
|
||||||
bar()
|
bar()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun baz() {}
|
||||||
}
|
}
|
||||||
@@ -8,4 +8,5 @@ class ConvertToInit() {
|
|||||||
bar()
|
bar()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun baz() {}
|
||||||
}
|
}
|
||||||
@@ -7,4 +7,6 @@ class WithVarArg {
|
|||||||
constructor(<caret>vararg zz: String) {
|
constructor(<caret>vararg zz: String) {
|
||||||
x = listOf(*zz)
|
x = listOf(*zz)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun foo() {}
|
||||||
}
|
}
|
||||||
@@ -8,4 +8,5 @@ class WithVarArg(vararg zz: String) {
|
|||||||
x = listOf(*zz)
|
x = listOf(*zz)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun foo() {}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user