idiomatic groovy constructors don't start with def (IDEA-27066)

This commit is contained in:
peter
2010-08-04 20:11:28 +01:00
parent b4cd62c1f3
commit dff00a0e7e
6 changed files with 24 additions and 21 deletions
@@ -354,13 +354,13 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
String[] paramNames,
String body,
PsiElement context) {
final GrMethod method = createMethodFromText(null, constructorName, null, paramTypes, paramNames, body, context);
final String text = generateMethodText(null, constructorName, null, paramTypes, paramNames, body, true);
GroovyFileImpl file = createDummyFile("class " + constructorName + "{" + method.getText() + "}");
GroovyFileImpl file = createDummyFile("class " + constructorName + "{" + text + "}");
file.setContext(context);
GrTopLevelDefintion defintion = file.getTopLevelDefinitions()[0];
assert defintion != null && defintion instanceof GrClassDefinition;
final PsiMethod constructor = ((GrClassDefinition) defintion).getMethods()[0];
GrTopLevelDefintion definition = file.getTopLevelDefinitions()[0];
assert definition != null && definition instanceof GrClassDefinition;
final PsiMethod constructor = ((GrClassDefinition) definition).getMethods()[0];
assert constructor instanceof GrConstructorImpl;
return ((GrConstructorImpl) constructor);
}
@@ -502,13 +502,13 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
}
private GrMethod createMethodFromText(String modifier,
String name,
String type,
@Nullable String[] paramTypes,
@NotNull String[] paramNames,
String body,
PsiElement context) {
private static String generateMethodText(String modifier,
String name,
String type,
String[] paramTypes,
String[] paramNames,
String body,
boolean isConstructor) {
StringBuilder builder = new StringBuilder();
if (modifier != null){
@@ -516,7 +516,9 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
builder.append(" ");
}
builder.append("def ");
if (!isConstructor) {
builder.append("def ");
}
//This is for constructor creation
if (type != null) {
@@ -547,7 +549,7 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
builder.append("}");
}
return createMethodFromText(builder.toString(), context);
return builder.toString();
}
public GrMethod createMethodFromText(String modifier, String name, @Nullable String type, String[] paramTypes, PsiElement context) {
@@ -565,8 +567,9 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
res.add(psiType);
}
return createMethodFromText(modifier, name, type, paramTypes,
QuickfixUtil.getMethodArgumentsNames(myProject, res.toArray(new PsiType[res.size()])), null, context);
String[] paramNames = QuickfixUtil.getMethodArgumentsNames(myProject, res.toArray(new PsiType[res.size()]));
final String text = generateMethodText(modifier, name, type, paramTypes, paramNames, null, false);
return createMethodFromText(text, context);
}
public GrDocComment createDocCommentFromText(String text) {
@@ -59,7 +59,7 @@ class Foo extends Super {
Object d
final e
def Foo(a, int b, int c, Object d, e) {
Foo(a, int b, int c, Object d, e) {
super(a, b)
this.c = c
this.d = d
@@ -4,6 +4,6 @@ class ConstructorAtEnd {
}
<caret>def ConstructorAtEnd() {
<caret>ConstructorAtEnd() {
}
}
@@ -4,7 +4,7 @@ class ConstructorAtOffset {
}
<caret>def ConstructorAtOffset() {
<caret>ConstructorAtOffset() {
}
def bar() {
@@ -1,5 +1,5 @@
class LonelyConstructor {
<caret>def LonelyConstructor() {
<caret>LonelyConstructor() {
}
}
@@ -1,5 +1,5 @@
class B extends A {
def B() {
B() {
super(27)
}
}