From ddf1d376d743217816b1505ca3f53901cbabb4c7 Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Thu, 11 Jul 2013 08:46:50 +0400 Subject: [PATCH] create method with a literal name if the name is not a identifier --- .../lang/psi/GroovyPsiElementFactory.java | 3 +- .../psi/impl/GroovyPsiElementFactoryImpl.java | 28 +++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/GroovyPsiElementFactory.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/GroovyPsiElementFactory.java index 2dc38225e770..3495e07fda7c 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/GroovyPsiElementFactory.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/GroovyPsiElementFactory.java @@ -228,7 +228,8 @@ public abstract class GroovyPsiElementFactory implements JVMElementFactory { public abstract GrMethod createConstructorFromText(String constructorName, CharSequence constructorText, @Nullable PsiElement context); - public abstract GrDocComment createDocCommentFromText(String text) ; + @NotNull + public abstract GrDocComment createDocCommentFromText(@NotNull String text) ; public abstract GrDocTag createDocTagFromText(String text) ; diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java index 4e59ed1e1995..ea34a7286c6c 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java @@ -41,6 +41,7 @@ import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference; import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement; import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag; import org.jetbrains.plugins.groovy.lang.psi.*; +import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifier; import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList; import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation; import org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature; @@ -833,7 +834,8 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory { return createMethodFromText(text.toString(), context); } - public GrDocComment createDocCommentFromText(String text) { + @NotNull + public GrDocComment createDocCommentFromText(@NotNull String text) { return (GrDocComment)createGroovyFileChecked(text).getFirstChild(); } @@ -962,14 +964,30 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory { public GrMethod createMethod(@NotNull @NonNls String name, @Nullable PsiType returnType) throws IncorrectOperationException { StringBuilder builder = StringBuilderSpinAllocator.alloc(); try { + builder.append("def "); if (returnType != null) { builder.append(returnType.getCanonicalText()); } - else { - builder.append("def"); + builder.append(' '); + if (GroovyNamesUtil.isIdentifier(name)) { + builder.append(name); } - builder.append(' ').append(name).append("(){}"); - return createMethodFromText(builder); + else { + builder.append('"'); + builder.append(GrStringUtil.escapeSymbolsForGString(name, true, false)); + builder.append('"'); + } + builder.append("(){}"); + GrMethod method = createMethodFromText(builder); + if (returnType != null) { + method.getModifierList().setModifierProperty(GrModifier.DEF, false); + } + PsiTypeParameterList typeParameterList = method.getTypeParameterList(); + assert typeParameterList != null; + typeParameterList.getFirstChild().delete(); + typeParameterList.getFirstChild().delete(); + typeParameterList.getFirstChild().delete(); + return method; } finally { StringBuilderSpinAllocator.dispose(builder);