mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
KT-11980 JvmCommonIntentionActionsFactory now has a createAddCallableMemberActions method to create constructors and methods
This commit is contained in:
+45
-8
@@ -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<IntentionAction> = emptyList()
|
||||
|
||||
open fun createAddBeanPropertyActions(uClass: UClass,
|
||||
propertyName: String,
|
||||
@@ -54,11 +53,49 @@ abstract class JvmCommonIntentionActionsFactory {
|
||||
setterRequired: Boolean,
|
||||
getterRequired: Boolean): Array<IntentionAction> = emptyArray()
|
||||
|
||||
|
||||
companion object : LanguageExtension<JvmCommonIntentionActionsFactory>(
|
||||
"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<String> = emptyList(),
|
||||
val typeParams: List<PsiTypeParameter> = emptyList(),
|
||||
val returnType: PsiType? = null,
|
||||
val parameters: List<UParameter> = 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<UParameter>) =
|
||||
NewCallableMemberInfo(kind = CallableKind.CONSTRUCTOR, containingClass = uClass, parameters = parameters)
|
||||
|
||||
@JvmStatic
|
||||
fun simpleMethodInfo(uClass: UClass, methodName: String, modifier: String, returnType: PsiType, parameters: List<UParameter>) =
|
||||
NewCallableMemberInfo(kind = CallableKind.FUNCTION,
|
||||
name = methodName,
|
||||
modifiers = listOf(modifier),
|
||||
containingClass = uClass,
|
||||
returnType = returnType,
|
||||
parameters = parameters)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+101
@@ -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<PsiClass> myBeanClass;
|
||||
private final List<PsiParameter> myParameters;
|
||||
private final String name;
|
||||
|
||||
public AddConstructorFix(PsiClass beanClass, List<PsiParameter> 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();
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
+23
-6
@@ -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<IntentionAction> {
|
||||
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<UParameter>): 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<IntentionAction>(
|
||||
CreateJavaBeanPropertyFix(uClass.psi, propertyName, propertyType, getterRequired, setterRequired, true))
|
||||
}
|
||||
|
||||
}
|
||||
+2
-6
@@ -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,
|
||||
|
||||
@@ -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<LocalQuickFix> wrapToQuickFixes(@NotNull List<IntentionAction> actions, @NotNull PsiFile file) {
|
||||
if (actions.isEmpty()) return Collections.emptyList();
|
||||
List<LocalQuickFix> fixes = new ArrayList<>(actions.size());
|
||||
for (IntentionAction action : actions) {
|
||||
fixes.add(wrapToQuickFix(action, file));
|
||||
}
|
||||
return fixes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
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
|
||||
Reference in New Issue
Block a user