isVarArg for GrParameterOwner, some new factory methods

This commit is contained in:
Maxim Medvedev
2011-06-28 10:28:16 +04:00
parent 81a54d9c6e
commit 139a25eb5d
11 changed files with 79 additions and 8 deletions
@@ -127,7 +127,11 @@ public abstract class GroovyPsiElementFactory implements JVMElementFactory {
public abstract GrTopStatement createTopElementFromText(String text);
public abstract GrClosableBlock createClosureFromText(String s) throws IncorrectOperationException;
public abstract GrClosableBlock createClosureFromText(String text, @Nullable PsiElement context);
public GrClosableBlock createClosureFromText(String s) throws IncorrectOperationException {
return createClosureFromText(s, null);
}
public GrParameter createParameter(String name, @Nullable String typeText, @Nullable GroovyPsiElement context) throws IncorrectOperationException {
return createParameter(name, typeText, null, context);
@@ -29,4 +29,6 @@ public interface GrParametersOwner extends GroovyPsiElement {
@Nullable
GrParameterList getParameterList();
boolean isVarArgs();
}
@@ -48,4 +48,6 @@ public interface GrClosableBlock extends GrExpression, GrCodeBlock, GrParameters
@Nullable
PsiElement getArrow();
boolean isVarArgs();
}
@@ -253,8 +253,8 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
return topStatements[0];
}
public GrClosableBlock createClosureFromText(String closureText) throws IncorrectOperationException {
GroovyFile psiFile = createDummyFile("def foo = " + closureText);
public GrClosableBlock createClosureFromText(String closureText, PsiElement context) throws IncorrectOperationException {
GroovyFile psiFile = createGroovyFile("def foo = " + closureText, false, context);
final GrStatement st = psiFile.getStatements()[0];
LOG.assertTrue(st instanceof GrVariableDeclaration, closureText);
final GrExpression initializer = ((GrVariableDeclaration)st).getVariables()[0].getInitializerGroovy();
@@ -55,6 +55,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.*;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.arithmetic.GrAdditiveExpressionImpl;
@@ -472,4 +473,8 @@ public class PsiImplUtil {
node.replaceChild(oldBodyNode, newBody.getNode());
return newBody;
}
public static boolean isVarArgs(GrParameter[] parameters) {
return parameters.length > 0 && parameters[parameters.length - 1].isVarArgs();
}
}
@@ -19,6 +19,7 @@ import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.ResolveState;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
@@ -67,4 +68,9 @@ public class GrCatchClauseImpl extends GroovyPsiElementImpl implements GrCatchCl
public GrParameterList getParameterList() {
return null;
}
@Override
public boolean isVarArgs() {
throw new IncorrectOperationException("Catch clause cannot have varargs");
}
}
@@ -136,6 +136,11 @@ public class GrClosableBlockImpl extends GrBlockImpl implements GrClosableBlock
return findPsiChildByType(GroovyTokenTypes.mCLOSABLE_BLOCK_OP);
}
@Override
public boolean isVarArgs() {
return PsiImplUtil.isVarArgs(getParameters());
}
@NotNull
public GrParameterListImpl getParameterList() {
@@ -17,6 +17,7 @@
package org.jetbrains.plugins.groovy.lang.psi.impl.statements.clauses;
import com.intellij.lang.ASTNode;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
@@ -57,6 +58,11 @@ public class GrForInClauseImpl extends GroovyPsiElementImpl implements GrForInCl
return null;
}
@Override
public boolean isVarArgs() {
throw new IncorrectOperationException("For in clause cannot have varargs");
}
@Nullable
public GrExpression getIteratedExpression() {
return findChildByClass(GrExpression.class);
@@ -17,6 +17,7 @@
package org.jetbrains.plugins.groovy.lang.psi.impl.statements.clauses;
import com.intellij.lang.ASTNode;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
@@ -118,4 +119,9 @@ public class GrTraditionalForClauseImpl extends GroovyPsiElementImpl implements
public GrParameterList getParameterList() {
return null;
}
@Override
public boolean isVarArgs() {
throw new IncorrectOperationException("For clause cannot have varargs");
}
}
@@ -303,8 +303,7 @@ public abstract class GrMethodBaseImpl extends GrStubElementBase<GrMethodStub> i
}
public boolean isVarArgs() {
GrParameter[] parameters = getParameters();
return parameters.length > 0 && parameters[parameters.length - 1].isVarArgs();
return PsiImplUtil.isVarArgs(getParameters());
}
@NotNull
@@ -15,15 +15,18 @@
*/
package org.jetbrains.plugins.groovy.lang.psi.impl.synthetic;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiType;
import com.intellij.psi.impl.light.LightParameter;
import com.intellij.psi.impl.light.LightVariableBuilder;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.GroovyFileType;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
@@ -31,12 +34,33 @@ import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
/**
* @author ven
*/
public class GrLightParameter extends LightParameter implements GrParameter {
public class GrLightParameter extends LightVariableBuilder<GrLightParameter> implements GrParameter {
public static final GrLightParameter[] EMPTY_ARRAY = new GrLightParameter[0];
private volatile boolean myOptional;
private volatile GrModifierList myModifierList;
private PsiElement myScope;
public GrLightParameter(@NotNull String name, @NotNull PsiType type, @NotNull PsiElement scope) {
super(name, type, scope, GroovyFileType.GROOVY_LANGUAGE);
super(scope.getManager(), name, type, GroovyFileType.GROOVY_LANGUAGE);
myScope = scope;
myModifierList = new GrLightModifierList(scope, ArrayUtil.EMPTY_STRING_ARRAY);
}
@NotNull
@Override
public PsiElement getDeclarationScope() {
return myScope;
}
@Override
public boolean isVarArgs() {
return false;
}
@NotNull
@Override
public PsiAnnotation[] getAnnotations() {
return PsiAnnotation.EMPTY_ARRAY;
}
public GrTypeElement getTypeElementGroovy() {
@@ -97,4 +121,16 @@ public class GrLightParameter extends LightParameter implements GrParameter {
public boolean isValid() {
return getDeclarationScope().isValid();
}
@NotNull
@Override
public GrModifierList getModifierList() {
return myModifierList;
}
@Override
public GrLightParameter setModifiers(String... modifiers) {
myModifierList = new GrLightModifierList(getContext(), modifiers);
return this;
}
}