[groovy] Fix CreateConstructorAction

GitOrigin-RevId: e123ec5445c6650b7516432181efe117b129f601
This commit is contained in:
Konstantin Nisht
2022-07-21 10:54:42 +03:00
committed by intellij-monorepo-bot
parent c5ff500bfd
commit dd3b123c83
2 changed files with 16 additions and 3 deletions

View File

@@ -751,7 +751,7 @@ intention.name.activate.back=Activate back
intention.name.cast.to.0=Cast to {0}
intention.name.replace.eq.with.0.eq=Replace ''='' with ''{0}=''
intention.name.cast.operand.to.0=Cast operand to {0}
intention.name.add.required.attributes.to.map.constructor=Add required attributes to @MapConstructor
intention.name.add.required.attributes.to.map.constructor=Add required attributes to '@MapConstructor'
intention.name.convert.to.single.quoted.string=Convert to single-quoted string
intention.name.change.quotes.to.triple.single.quotes=Change quotes to '''
intention.name.convert.to.double.quoted.string=Convert to double-quoted string

View File

@@ -1,13 +1,15 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.plugins.groovy.annotator.intentions.elements
import com.intellij.codeInsight.CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement
import com.intellij.codeInsight.daemon.QuickFixBundle.message
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
import com.intellij.lang.jvm.JvmModifier
import com.intellij.lang.jvm.actions.CreateConstructorRequest
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import org.jetbrains.plugins.groovy.GroovyFileType
import org.jetbrains.plugins.groovy.intentions.base.IntentionUtils.createTemplateForMethod
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition
@@ -18,6 +20,12 @@ internal class CreateConstructorAction(
override val request: CreateConstructorRequest
) : CreateMemberAction(targetClass, request) {
override fun generatePreview(project: Project, editor: Editor, file: PsiFile): IntentionPreviewInfo {
val targetClass = myTargetPointer.element ?: return IntentionPreviewInfo.EMPTY
val constructor = ConstructorMethodRenderer(project, target, request).renderConstructor()
val className = targetClass.name
return IntentionPreviewInfo.CustomDiff(GroovyFileType.GROOVY_FILE_TYPE, className, "", constructor.text)
}
override fun getFamilyName(): String = message("create.constructor.family")
@@ -50,9 +58,14 @@ private class ConstructorMethodRenderer(
createTemplateForMethod(typeExpressions, nameExpressions, method, targetClass, null, true, null)
}
private fun renderConstructor(): GrMethod {
fun renderConstructor(): GrMethod {
val constructor = factory.createConstructor()
val name = targetClass.name
if (name != null) {
constructor.name = name
}
val modifiersToRender = request.modifiers.toMutableList()
modifiersToRender -= JvmModifier.PUBLIC //public by default