From 9f87720a505edc118751b87b23ceeeda810486c9 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Tue, 16 May 2017 18:38:15 +0200 Subject: [PATCH] KT-11980 `JvmCommonIntentionActionsFactory` now has a `createAddCallableMemberActions` method to create constructors and methods --- .../JvmCommonIntentionActionsFactory.kt | 53 +++++++-- .../impl/quickfix/AddConstructorFix.java | 101 ++++++++++++++++++ .../impl/quickfix/UastCreateMethodFix.kt | 5 +- .../impl/JavaCommonIntentionActionsFactory.kt | 29 +++-- .../CreateBeanPropertyFixes.java | 8 +- .../codeInspection/IntentionWrapper.java | 26 ++++- .../src/messages/QuickFixBundle.properties | 4 +- 7 files changed, 203 insertions(+), 23 deletions(-) create mode 100644 java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddConstructorFix.java diff --git a/java/java-analysis-api/src/com/intellij/codeInsight/intention/JvmCommonIntentionActionsFactory.kt b/java/java-analysis-api/src/com/intellij/codeInsight/intention/JvmCommonIntentionActionsFactory.kt index 3b74bef789f2..c2e13987e786 100644 --- a/java/java-analysis-api/src/com/intellij/codeInsight/intention/JvmCommonIntentionActionsFactory.kt +++ b/java/java-analysis-api/src/com/intellij/codeInsight/intention/JvmCommonIntentionActionsFactory.kt @@ -19,18 +19,21 @@ import com.intellij.lang.Language import com.intellij.lang.LanguageExtension import com.intellij.psi.PsiModifier import com.intellij.psi.PsiType +import com.intellij.psi.PsiTypeParameter import org.jetbrains.annotations.ApiStatus import org.jetbrains.annotations.NonNls import org.jetbrains.uast.UClass import org.jetbrains.uast.UDeclaration +import org.jetbrains.uast.UElement +import org.jetbrains.uast.UParameter /** * Extension Point provides language-abstracted code modifications for JVM-based languages. * - * Each method should return nullable code modification ([IntentionAction]). - * If method returns `null` this means that operation on given elements is not supported or not yet implemented for a language. + * Each method should return nullable code modification ([IntentionAction]) or list of code modifications which could be empty. + * If method returns `null` or empty list this means that operation on given elements is not supported or not yet implemented for a language. * - * Every new added method should return `null` by default and then be overridden in implementations for each language if it is possible. + * Every new added method should return `null` or empty list by default and then be overridden in implementations for each language if it is possible. * * @since 2017.2 */ @@ -41,11 +44,7 @@ abstract class JvmCommonIntentionActionsFactory { @PsiModifier.ModifierConstant @NonNls modifier: String, shouldPresent: Boolean): IntentionAction? = null - open fun createAddMethodAction(uClass: UClass, - methodName: String, - @PsiModifier.ModifierConstant visibilityModifier: String, - returnType: PsiType, - vararg parameters: PsiType): IntentionAction? = null + open fun createAddCallableMemberActions(info: NewCallableMemberInfo): List = emptyList() open fun createAddBeanPropertyActions(uClass: UClass, propertyName: String, @@ -54,11 +53,49 @@ abstract class JvmCommonIntentionActionsFactory { setterRequired: Boolean, getterRequired: Boolean): Array = emptyArray() + companion object : LanguageExtension( "com.intellij.codeInsight.intention.jvmCommonIntentionActionsFactory") { @JvmStatic override fun forLanguage(l: Language): JvmCommonIntentionActionsFactory? = super.forLanguage(l) } + +} + +data class NewCallableMemberInfo( + val kind: CallableKind, + val containingClass: UClass, + val name: String? = null, + val modifiers: List = emptyList(), + val typeParams: List = emptyList(), + val returnType: PsiType? = null, + val parameters: List = emptyList(), + val caller: UElement? = null, + val isAbstract: Boolean = false, + val focusAfterInserting: Boolean = false +) { + + enum class CallableKind { + FUNCTION, + CONSTRUCTOR + } + + companion object { + + @JvmStatic + fun constructorInfo(uClass: UClass, parameters: List) = + NewCallableMemberInfo(kind = CallableKind.CONSTRUCTOR, containingClass = uClass, parameters = parameters) + + @JvmStatic + fun simpleMethodInfo(uClass: UClass, methodName: String, modifier: String, returnType: PsiType, parameters: List) = + NewCallableMemberInfo(kind = CallableKind.FUNCTION, + name = methodName, + modifiers = listOf(modifier), + containingClass = uClass, + returnType = returnType, + parameters = parameters) + + } } diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddConstructorFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddConstructorFix.java new file mode 100644 index 000000000000..6b2702ce56b9 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/AddConstructorFix.java @@ -0,0 +1,101 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.daemon.impl.quickfix; + +import com.intellij.codeInsight.FileModificationService; +import com.intellij.codeInsight.daemon.QuickFixBundle; +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.codeInspection.LocalQuickFix; +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.stream.Collectors; + +public class AddConstructorFix implements LocalQuickFix, IntentionAction { + + private final SmartPsiElementPointer myBeanClass; + private final List myParameters; + private final String name; + + public AddConstructorFix(PsiClass beanClass, List parameters) { + myBeanClass = SmartPointerManager.getInstance(beanClass.getProject()).createSmartPsiElementPointer(beanClass); + myParameters = parameters; + final String params = myParameters.stream().map(p -> p.getText()).collect(Collectors.joining(", ")); + final String signature = beanClass.getName() + "(" + params + ")"; + name = QuickFixBundle.message("model.create.constructor.quickfix.message", signature); + } + + @NotNull + public String getName() { + return name; + } + + @Nls + @NotNull + @Override + public String getText() { + return name; + } + + @NotNull + public String getFamilyName() { + return QuickFixBundle.message("model.create.constructor.quickfix.message.family.name"); + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + return true; + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + applyFix(); + } + + private void applyFix() { + try { + if (!FileModificationService.getInstance().preparePsiElementForWrite(myBeanClass.getContainingFile())) return; + PsiClass psiClass = myBeanClass.getElement(); + if (psiClass == null) return; + final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(myBeanClass.getProject()).getElementFactory(); + + final PsiMethod constructor = elementFactory.createConstructor(); + + for (PsiParameter parameter : myParameters) { + constructor.getParameterList().add(parameter); + } + psiClass.add(constructor); + } + catch (IncorrectOperationException e) { + throw new RuntimeException(e); + } + } + + @Override + public boolean startInWriteAction() { + return true; + } + + public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) { + applyFix(); + } +} diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/UastCreateMethodFix.kt b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/UastCreateMethodFix.kt index 5b2eb9ccb9aa..e890fffc5d18 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/UastCreateMethodFix.kt +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/UastCreateMethodFix.kt @@ -17,6 +17,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix import com.intellij.codeInsight.intention.IntentionAction import com.intellij.codeInsight.intention.JvmCommonIntentionActionsFactory +import com.intellij.codeInsight.intention.NewCallableMemberInfo import com.intellij.codeInspection.LocalQuickFixBase import com.intellij.codeInspection.ProblemDescriptor import com.intellij.openapi.module.ModuleUtilCore @@ -55,7 +56,9 @@ class UastCreateMethodFix(containingClass: UClass, private val createMethodActio @PsiModifier.ModifierConstant modifier: String): UastCreateMethodFix? { if (!ModuleUtilCore.projectContainsFile(uClass.project, uClass.containingFile.virtualFile, false)) return null val actionsFactory = JvmCommonIntentionActionsFactory.forLanguage(uClass.language) ?: return null - val action = actionsFactory.createAddMethodAction(uClass, methodName, modifier, PsiType.VOID) ?: return null + val action = actionsFactory.createAddCallableMemberActions( + NewCallableMemberInfo.simpleMethodInfo(uClass, methodName, modifier, PsiType.VOID, emptyList()) + ).firstOrNull() ?: return null return UastCreateMethodFix(uClass, action) } } diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/JavaCommonIntentionActionsFactory.kt b/java/java-impl/src/com/intellij/codeInsight/intention/impl/JavaCommonIntentionActionsFactory.kt index 92036d1384ae..3f581f7f8d39 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/JavaCommonIntentionActionsFactory.kt +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/JavaCommonIntentionActionsFactory.kt @@ -16,10 +16,12 @@ package com.intellij.codeInsight.intention.impl import com.intellij.codeInsight.daemon.QuickFixBundle +import com.intellij.codeInsight.daemon.impl.quickfix.AddConstructorFix import com.intellij.codeInsight.daemon.impl.quickfix.ModifierFix import com.intellij.codeInsight.intention.AbstractIntentionAction import com.intellij.codeInsight.intention.IntentionAction import com.intellij.codeInsight.intention.JvmCommonIntentionActionsFactory +import com.intellij.codeInsight.intention.NewCallableMemberInfo import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.psi.* @@ -28,6 +30,7 @@ import com.intellij.util.VisibilityUtil import org.jetbrains.annotations.NotNull import org.jetbrains.uast.UClass import org.jetbrains.uast.UDeclaration +import org.jetbrains.uast.UParameter class JavaCommonIntentionActionsFactory : JvmCommonIntentionActionsFactory() { @@ -36,12 +39,25 @@ class JavaCommonIntentionActionsFactory : JvmCommonIntentionActionsFactory() { return ModifierFix(declaration.modifierList, modifier, shouldPresent, false) } - override fun createAddMethodAction(uClass: UClass, - methodName: String, - @PsiModifier.ModifierConstant @NotNull visibilityModifier: String, - returnType: PsiType, - vararg parameters: PsiType): IntentionAction? { - val paramsString = parameters.mapIndexed { i, t -> "${t.presentableText} arg$i" }.joinToString() + override fun createAddCallableMemberActions(info: NewCallableMemberInfo): List { + return when (info.kind) { + NewCallableMemberInfo.CallableKind.FUNCTION -> + with(info) { + createAddMethodAction(containingClass, name!!, modifiers.joinToString(" "), returnType!!, parameters) + ?.let { listOf(it) } ?: emptyList() + } + + NewCallableMemberInfo.CallableKind.CONSTRUCTOR -> + listOf(AddConstructorFix(info.containingClass.psi, info.parameters.map { it.psi })) + } + } + + private fun createAddMethodAction(uClass: UClass, + methodName: String, + @PsiModifier.ModifierConstant @NotNull visibilityModifier: String, + returnType: PsiType, + parameters: List): IntentionAction? { + val paramsString = parameters.mapIndexed { i, t -> "${t.type.presentableText} ${t.name ?: "arg$i"}" }.joinToString() val signatureString = "${VisibilityUtil.getVisibilityString(visibilityModifier)} ${returnType.presentableText} $methodName($paramsString){}" val smartPsi = SmartPointerManager.getInstance(uClass.project).createSmartPsiElementPointer(uClass.psi) @@ -83,4 +99,5 @@ class JavaCommonIntentionActionsFactory : JvmCommonIntentionActionsFactory() { return arrayOf( CreateJavaBeanPropertyFix(uClass.psi, propertyName, propertyType, getterRequired, setterRequired, true)) } + } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/psi/impl/beanProperties/CreateBeanPropertyFixes.java b/java/java-impl/src/com/intellij/psi/impl/beanProperties/CreateBeanPropertyFixes.java index ce3318c77631..ea61ebaa455a 100644 --- a/java/java-impl/src/com/intellij/psi/impl/beanProperties/CreateBeanPropertyFixes.java +++ b/java/java-impl/src/com/intellij/psi/impl/beanProperties/CreateBeanPropertyFixes.java @@ -17,6 +17,7 @@ package com.intellij.psi.impl.beanProperties; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInsight.intention.JvmCommonIntentionActionsFactory; +import com.intellij.codeInspection.IntentionWrapper; import com.intellij.codeInspection.LocalQuickFix; import com.intellij.openapi.project.Project; import com.intellij.psi.*; @@ -27,9 +28,6 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.uast.UClass; import org.jetbrains.uast.UastContextKt; -import java.util.Arrays; - -import static com.intellij.codeInspection.IntentionWrapper.wrapToQuickFix; import static com.intellij.psi.CommonClassNames.JAVA_LANG_STRING; @ApiStatus.Experimental @@ -39,9 +37,7 @@ public class CreateBeanPropertyFixes { @NotNull PsiClass psiClass, @Nullable PsiType type, final boolean createSetter) { - return Arrays.stream(createActions(propertyName, psiClass, type, createSetter)) - .map(ia -> wrapToQuickFix(ia, psiClass.getContainingFile())) - .toArray(LocalQuickFix[]::new); + return IntentionWrapper.wrapToQuickFixes(createActions(propertyName, psiClass, type, createSetter), psiClass.getContainingFile()); } public static IntentionAction[] createActions(String propertyName, diff --git a/platform/lang-api/src/com/intellij/codeInspection/IntentionWrapper.java b/platform/lang-api/src/com/intellij/codeInspection/IntentionWrapper.java index 81834e6982be..8b7e8863e0e3 100644 --- a/platform/lang-api/src/com/intellij/codeInspection/IntentionWrapper.java +++ b/platform/lang-api/src/com/intellij/codeInspection/IntentionWrapper.java @@ -30,6 +30,10 @@ import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + public class IntentionWrapper implements LocalQuickFix, IntentionAction, ActionClassHolder, IntentionActionDelegate { private final IntentionAction myAction; private final PsiFile myFile; @@ -66,6 +70,7 @@ public class IntentionWrapper implements LocalQuickFix, IntentionAction, ActionC public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { myAction.invoke(project, editor, file); } + @Nullable @Override public PsiElement getElementToMakeWritable(@NotNull PsiFile file) { @@ -88,7 +93,7 @@ public class IntentionWrapper implements LocalQuickFix, IntentionAction, ActionC if (virtualFile != null) { FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(virtualFile); - myAction.invoke(project, editor instanceof TextEditor ? ((TextEditor) editor).getEditor() : null, myFile); + myAction.invoke(project, editor instanceof TextEditor ? ((TextEditor)editor).getEditor() : null, myFile); } } @@ -111,4 +116,23 @@ public class IntentionWrapper implements LocalQuickFix, IntentionAction, ActionC return new IntentionWrapper(action, file); } + @NotNull + public static LocalQuickFix[] wrapToQuickFixes(@NotNull IntentionAction[] actions, @NotNull PsiFile file) { + if (actions.length == 0) return LocalQuickFix.EMPTY_ARRAY; + LocalQuickFix[] fixes = new LocalQuickFix[actions.length]; + for (int i = 0; i < actions.length; i++) { + fixes[i] = wrapToQuickFix(actions[i], file); + } + return fixes; + } + + @NotNull + public static List wrapToQuickFixes(@NotNull List actions, @NotNull PsiFile file) { + if (actions.isEmpty()) return Collections.emptyList(); + List fixes = new ArrayList<>(actions.size()); + for (IntentionAction action : actions) { + fixes.add(wrapToQuickFix(action, file)); + } + return fixes; + } } diff --git a/resources-en/src/messages/QuickFixBundle.properties b/resources-en/src/messages/QuickFixBundle.properties index 4750691fd035..0319e9507262 100644 --- a/resources-en/src/messages/QuickFixBundle.properties +++ b/resources-en/src/messages/QuickFixBundle.properties @@ -313,4 +313,6 @@ wrap.with.java.io.file.parameter.single.text=Wrap parameter using 'new File()' wrap.with.java.io.file.parameter.multiple.text=Wrap {0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} parameter using ''new File()'' java.9.merge.module.statements.fix.family.name=Merge with other ''{0}'' statement -java.9.merge.module.statements.fix.name=Merge with other ''{0} {1}'' statement \ No newline at end of file +java.9.merge.module.statements.fix.name=Merge with other ''{0} {1}'' statement +model.create.constructor.quickfix.message=Create constructor ''{0}'' +model.create.constructor.quickfix.message.family.name=Create constructor \ No newline at end of file