mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-02-04 15:06:56 +07:00
[spring] IDEA-357318 Spring: inject on completion in constructor produces broken code
GitOrigin-RevId: 79fa4eeac4be5e1520963e1813be22c94db52b69
This commit is contained in:
committed by
intellij-monorepo-bot
parent
086eca34c7
commit
26f4517a0e
@@ -56,6 +56,14 @@ public interface CreateFieldRequest extends ActionRequest {
|
||||
*/
|
||||
boolean isConstant();
|
||||
|
||||
/**
|
||||
* Only for Kotlin
|
||||
* Determines whether an empty initializer should be created for the field.
|
||||
*
|
||||
* @return true if an empty initializer should be created, false otherwise.
|
||||
*/
|
||||
default boolean isCreateEmptyInitializer() { return true; }
|
||||
|
||||
/**
|
||||
* @return should start live template after a new field was created.
|
||||
*/
|
||||
|
||||
@@ -36,7 +36,11 @@ class AddFieldActionCreateCallableFromUsageFix(
|
||||
val initializer = if (requestInitializer is JvmLong) {
|
||||
psiFactory.createExpression("${requestInitializer.longValue}L")
|
||||
} else if (!lateinit) {
|
||||
psiFactory.createExpression("TODO(\"initialize me\")")
|
||||
if (request.isCreateEmptyInitializer) {
|
||||
psiFactory.createExpression("TODO(\"initialize me\")")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} else null
|
||||
if (!request.isValid) null else
|
||||
PropertyInfo(
|
||||
|
||||
@@ -85,7 +85,7 @@ abstract class KotlinUastBaseCodeGenerationPlugin : UastCodeGenerationPlugin {
|
||||
TODO("Not implemented")
|
||||
}
|
||||
|
||||
override fun initializeField(uField: UField, uParameter: UParameter): UExpression? {
|
||||
override fun initializeField(uField: UField, uParameter: UParameter, anchor: PsiElement?, addBefore: Boolean): UExpression? {
|
||||
val uMethod = uParameter.getParentOfType(UMethod::class.java, false) ?: return null
|
||||
val sourcePsi = uMethod.sourcePsi ?: return null
|
||||
if (sourcePsi is KtPrimaryConstructor) {
|
||||
@@ -125,8 +125,18 @@ abstract class KotlinUastBaseCodeGenerationPlugin : UastCodeGenerationPlugin {
|
||||
appendFixedText(" = ")
|
||||
appendName(Name.identifier(uParameter.name))
|
||||
}
|
||||
|
||||
body.addBefore(assignmentExpression, body.rBrace)
|
||||
if (anchor != null) {
|
||||
val newLine = ktPsiFactory.createWhiteSpace("\n")
|
||||
if (addBefore) {
|
||||
val anchor = body.addBefore(newLine, anchor)
|
||||
body.addBefore(assignmentExpression, anchor)
|
||||
} else {
|
||||
val anchor = body.addAfter(newLine, anchor)
|
||||
body.addAfter(assignmentExpression, anchor)
|
||||
}
|
||||
} else {
|
||||
body.addBefore(assignmentExpression, body.rBrace)
|
||||
}
|
||||
return assignmentExpression.toUElementOfType()
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ interface UastCodeGenerationPlugin {
|
||||
* If the parameter is from Kotlin primary constructor and the field and the parameter have different names,
|
||||
* Kotlin property is initialized with the parameter.
|
||||
*/
|
||||
fun initializeField(uField: UField, uParameter: UParameter): UExpression?
|
||||
fun initializeField(uField: UField, uParameter: UParameter, anchor: PsiElement? = null, addBefore: Boolean = false): UExpression?
|
||||
|
||||
/**
|
||||
* Creates new return expression with changed return label for Explicit return expression (for Kotlin)
|
||||
|
||||
@@ -117,7 +117,7 @@ internal class JavaUastCodeGenerationPlugin : UastCodeGenerationPlugin {
|
||||
return ptr.element?.parent.toUElementOfType()
|
||||
}
|
||||
|
||||
override fun initializeField(uField: UField, uParameter: UParameter): UExpression? {
|
||||
override fun initializeField(uField: UField, uParameter: UParameter, anchor: PsiElement?, addBefore: Boolean): UExpression? {
|
||||
val uMethod = uParameter.getParentOfType(UMethod::class.java, false) ?: return null
|
||||
val psiMethod = uMethod.sourcePsi as? PsiMethod ?: return null
|
||||
val body = psiMethod.body ?: return null
|
||||
@@ -125,12 +125,16 @@ internal class JavaUastCodeGenerationPlugin : UastCodeGenerationPlugin {
|
||||
val elementFactory = JavaPsiFacade.getInstance(psiMethod.project).elementFactory
|
||||
val prefix = if (uField.name == uParameter.name) "this." else ""
|
||||
val statement = elementFactory.createStatementFromText("$prefix${uField.name} = ${uParameter.name};", psiMethod)
|
||||
val lastBodyElement = body.lastBodyElement
|
||||
if (lastBodyElement is PsiWhiteSpace) {
|
||||
lastBodyElement.replace(statement)
|
||||
}
|
||||
else {
|
||||
body.add(statement)
|
||||
if (anchor != null) {
|
||||
if (addBefore) body.addBefore(statement, anchor) else body.addAfter(statement, anchor)
|
||||
} else {
|
||||
val lastBodyElement = body.lastBodyElement
|
||||
if (lastBodyElement is PsiWhiteSpace) {
|
||||
lastBodyElement.replace(statement)
|
||||
}
|
||||
else {
|
||||
body.add(statement)
|
||||
}
|
||||
}
|
||||
return statement.toUElementOfType()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user