use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class ArgumentListGenerator method generateMultiArg.
private boolean generateMultiArg(GrClosureSignatureUtil.ArgInfo<PsiElement> arg, GrClosureParameter param, PsiSubstitutor substitutor, Project project, GroovyPsiElement context) {
final PsiType type = param.getType();
if (type instanceof PsiEllipsisType) {
for (PsiElement element : arg.args) {
LOG.assertTrue(element instanceof GrExpression);
((GrExpression) element).accept(myExpressionGenerator);
myBuilder.append(", ");
}
if (!arg.args.isEmpty()) {
myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
return true;
} else {
return false;
}
} else if (type instanceof PsiArrayType) {
myBuilder.append("new ");
if (arg.args.isEmpty()) {
TypeWriter.writeType(myBuilder, ((PsiArrayType) type).getComponentType(), context);
myBuilder.append("[0]");
} else {
TypeWriter.writeTypeForNew(myBuilder, type, context);
myBuilder.append("{");
for (PsiElement element : arg.args) {
LOG.assertTrue(element instanceof GrExpression);
((GrExpression) element).accept(myExpressionGenerator);
myBuilder.append(", ");
}
if (!arg.args.isEmpty())
myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
//if (arg.args.size() > 0) myBuilder.removeFromTheEnd(2);
myBuilder.append('}');
}
} else {
final GrExpression listOrMap = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arg.args, type, project);
LOG.assertTrue(listOrMap instanceof GrListOrMap);
listOrMap.accept(myExpressionGenerator);
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project android by JetBrains.
the class GradleDslElement method deleteIfEmpty.
protected static void deleteIfEmpty(@Nullable PsiElement element) {
if (element == null || !element.isValid()) {
return;
}
PsiElement parent = element.getParent();
if (element instanceof GrAssignmentExpression) {
if (((GrAssignmentExpression) element).getRValue() == null) {
element.delete();
}
} else if (element instanceof GrApplicationStatement) {
if (((GrApplicationStatement) element).getArgumentList() == null) {
element.delete();
}
} else if (element instanceof GrClosableBlock) {
final Boolean[] isEmpty = new Boolean[] { true };
((GrClosableBlock) element).acceptChildren(new GroovyElementVisitor() {
@Override
public void visitElement(GroovyPsiElement child) {
if (child instanceof GrParameterList) {
if (((GrParameterList) child).getParameters().length == 0) {
// Ignore the empty parameter list.
return;
}
}
isEmpty[0] = false;
}
});
if (isEmpty[0]) {
element.delete();
}
} else if (element instanceof GrMethodCallExpression) {
GrMethodCallExpression call = ((GrMethodCallExpression) element);
GrArgumentList argumentList;
try {
argumentList = call.getArgumentList();
} catch (AssertionError e) {
// We will get this exception if the argument list is already deleted.
argumentList = null;
}
GrClosableBlock[] closureArguments = call.getClosureArguments();
if ((argumentList == null || argumentList.getAllArguments().length == 0) && closureArguments.length == 0) {
element.delete();
}
} else if (element instanceof GrCommandArgumentList) {
GrCommandArgumentList commandArgumentList = (GrCommandArgumentList) element;
if (commandArgumentList.getAllArguments().length == 0) {
commandArgumentList.delete();
}
} else if (element instanceof GrListOrMap) {
GrListOrMap listOrMap = (GrListOrMap) element;
if ((listOrMap.isMap() && listOrMap.getNamedArguments().length == 0) || (!listOrMap.isMap() && listOrMap.getInitializers().length == 0)) {
listOrMap.delete();
}
}
if (!element.isValid()) {
// If this element is deleted, also delete the parent if it is empty.
deleteIfEmpty(parent);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project android by JetBrains.
the class GradleDslExpressionList method createNamedArgumentList.
@Nullable
private GroovyPsiElement createNamedArgumentList() {
assert myParent instanceof GradleDslExpressionMap;
GroovyPsiElement parentPsiElement = myParent.create();
if (parentPsiElement == null) {
return null;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
GrExpression expressionFromText = factory.createExpressionFromText("[]");
if (expressionFromText instanceof GrListOrMap) {
// Elements need to be added to the list before adding the list to the named argument.
GrListOrMap list = (GrListOrMap) expressionFromText;
for (GradleDslExpression expression : myToBeAddedExpressions) {
expression.setPsiElement(list);
expression.applyChanges();
myExpressions.add(expression);
}
myToBeAddedExpressions.clear();
}
GrNamedArgument namedArgument = factory.createNamedArgument(myName, expressionFromText);
PsiElement added;
if (parentPsiElement instanceof GrArgumentList) {
added = ((GrArgumentList) parentPsiElement).addNamedArgument(namedArgument);
} else {
added = parentPsiElement.addAfter(namedArgument, parentPsiElement.getLastChild());
}
if (added instanceof GrNamedArgument) {
GrNamedArgument addedNameArgument = (GrNamedArgument) added;
setPsiElement(addedNameArgument.getExpression());
return getPsiElement();
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project android by JetBrains.
the class GradleDslExpressionMap method create.
@Override
@Nullable
public GroovyPsiElement create() {
GroovyPsiElement psiElement = super.create();
if (psiElement == null) {
return null;
}
if (psiElement instanceof GrListOrMap || psiElement instanceof GrArgumentList) {
return psiElement;
}
if (psiElement instanceof GrApplicationStatement) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
GrArgumentList argumentList = factory.createArgumentListFromText("xyz");
// Workaround to get an empty argument list.
argumentList.getFirstChild().delete();
PsiElement added = psiElement.addAfter(argumentList, psiElement.getLastChild());
if (added instanceof GrArgumentList) {
GrArgumentList addedArgumentList = (GrArgumentList) added;
setPsiElement(addedArgumentList);
return addedArgumentList;
}
}
return null;
}
Aggregations