From 01099830101e86941ccfb8dc3d165c41ad397365 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 23 Oct 2014 20:18:52 +0200 Subject: [PATCH] provide context for new methods: createMethod(name, returnType, context) --- .../codeInsight/generation/GenerateMembersUtil.java | 2 +- .../src/com/intellij/psi/JVMElementFactory.java | 3 +++ .../src/com/intellij/psi/impl/PsiElementFactoryImpl.java | 6 ++++++ .../plugins/groovy/lang/psi/GroovyPsiElementFactory.java | 4 ++++ .../groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java | 8 +++++++- 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/GenerateMembersUtil.java b/java/java-impl/src/com/intellij/codeInsight/generation/GenerateMembersUtil.java index 4d55138cad1c..9c9034dc7e94 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/GenerateMembersUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/GenerateMembersUtil.java @@ -475,7 +475,7 @@ public class GenerateMembersUtil { if (method.isConstructor()) { return factory.createConstructor(method.getName(), target); } - return factory.createMethodFromText("public void " + method.getName() + "(){}", target); + return factory.createMethod(method.getName(), PsiType.VOID, target); } private static void substituteReturnType(@NotNull PsiManager manager, diff --git a/java/java-psi-api/src/com/intellij/psi/JVMElementFactory.java b/java/java-psi-api/src/com/intellij/psi/JVMElementFactory.java index e6c38aa5a186..7c3999e37a7b 100644 --- a/java/java-psi-api/src/com/intellij/psi/JVMElementFactory.java +++ b/java/java-psi-api/src/com/intellij/psi/JVMElementFactory.java @@ -84,6 +84,9 @@ public interface JVMElementFactory { @NotNull PsiMethod createMethod(@NotNull @NonNls String name, PsiType returnType) throws IncorrectOperationException; + @NotNull + PsiMethod createMethod(@NotNull @NonNls String name, PsiType returnType, PsiElement context) throws IncorrectOperationException; + /** * Creates an empty constructor. * diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java b/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java index 0515b4983ce2..2163057032d8 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java @@ -247,6 +247,12 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps return (PsiMethod)CodeStyleManager.getInstance(myManager.getProject()).reformat(method); } + @NotNull + @Override + public PsiMethod createMethod(@NotNull @NonNls String name, PsiType returnType, PsiElement context) throws IncorrectOperationException { + return createMethodFromText("public " + returnType.getCanonicalText(true) + " " + name + "() {}", context); + } + @NotNull @Override public PsiMethod createConstructor() { diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/GroovyPsiElementFactory.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/GroovyPsiElementFactory.java index ff89fb349ef2..1495abab2a5e 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/GroovyPsiElementFactory.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/GroovyPsiElementFactory.java @@ -265,6 +265,10 @@ public abstract class GroovyPsiElementFactory implements JVMElementFactory { @NotNull public abstract GrMethod createMethod(@NotNull @NonNls String name, @Nullable PsiType returnType) throws IncorrectOperationException; + @Override + @NotNull + public abstract GrMethod createMethod(@NotNull @NonNls String name, @Nullable PsiType returnType, @Nullable PsiElement context) throws IncorrectOperationException; + @NotNull @Override public abstract GrMethod createConstructor(); diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java index 682f55a6d7a2..2a2db5ecaeb9 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java @@ -1016,6 +1016,12 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory { @NotNull @Override public GrMethod createMethod(@NotNull @NonNls String name, @Nullable PsiType returnType) throws IncorrectOperationException { + return createMethod(name, returnType, null); + } + + @NotNull + @Override + public GrMethod createMethod(@NotNull @NonNls String name, PsiType returnType, PsiElement context) throws IncorrectOperationException { StringBuilder builder = StringBuilderSpinAllocator.alloc(); try { builder.append("def "); @@ -1032,7 +1038,7 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory { builder.append('"'); } builder.append("(){}"); - GrMethod method = createMethodFromText(builder); + GrMethod method = createMethodFromText(builder.toString(), context); if (returnType != null) { method.getModifierList().setModifierProperty(GrModifier.DEF, false); }