use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList in project intellij-community by JetBrains.
the class ApplicationStatementUtil method convertAppInternal.
private static GrMethodCallExpression convertAppInternal(GroovyPsiElementFactory factory, GrApplicationStatement app) {
final GrCommandArgumentList list = app.getArgumentList();
final GrMethodCallExpression prototype = (GrMethodCallExpression) factory.createExpressionFromText("foo()");
prototype.getInvokedExpression().replace(app.getInvokedExpression());
final GrArgumentList pList = prototype.getArgumentList();
final PsiElement anchor = pList.getRightParen();
for (GroovyPsiElement arg : list.getAllArguments()) {
pList.addBefore(arg, anchor);
}
return prototype;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList in project intellij-community by JetBrains.
the class RemoveParenthesesFromMethodPredicate method satisfiedBy.
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrMethodCallExpression))
return false;
if (!PsiUtil.isExpressionStatement(element))
return false;
final GrMethodCallExpression expression = (GrMethodCallExpression) element;
if (PsiImplUtil.hasClosureArguments(expression))
return false;
final StringBuilder newStatementText = new StringBuilder();
newStatementText.append(expression.getInvokedExpression().getText()).append(' ');
GrArgumentList argumentList = expression.getArgumentList();
final GroovyPsiElement[] allArguments = argumentList.getAllArguments();
argumentList = (GrArgumentList) argumentList.copy();
final PsiElement leftParen = argumentList.getLeftParen();
final PsiElement rightParen = argumentList.getRightParen();
if (leftParen != null)
leftParen.delete();
if (rightParen != null)
rightParen.delete();
newStatementText.append(argumentList.getText());
final GrStatement newStatement;
try {
newStatement = GroovyPsiElementFactory.getInstance(element.getProject()).createStatementFromText(newStatementText.toString());
} catch (IncorrectOperationException e) {
return false;
}
if (newStatement instanceof GrApplicationStatement) {
final GrCommandArgumentList newArgList = ((GrApplicationStatement) newStatement).getArgumentList();
if (newArgList.getAllArguments().length == allArguments.length) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList 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);
}
}
Aggregations