use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project android by JetBrains.
the class GradleDslParser method getMethodCall.
@NotNull
static GradleDslMethodCall getMethodCall(@NotNull GradleDslElement parentElement, @NotNull GrMethodCallExpression psiElement, @NotNull String propertyName, @NotNull GrArgumentList argumentList) {
GradleDslMethodCall methodCall = new GradleDslMethodCall(parentElement, psiElement, propertyName);
for (GrExpression expression : argumentList.getExpressionArguments()) {
if (expression instanceof GrListOrMap) {
GrListOrMap listOrMap = (GrListOrMap) expression;
if (listOrMap.isMap()) {
methodCall.addParsedExpressionMap(getExpressionMap(methodCall, expression, propertyName, listOrMap.getNamedArguments()));
} else {
for (GrExpression grExpression : listOrMap.getInitializers()) {
GradleDslExpression dslExpression = getExpressionElement(methodCall, expression, propertyName, grExpression);
if (dslExpression != null) {
methodCall.addParsedExpression(dslExpression);
}
}
}
} else if (expression instanceof GrClosableBlock) {
methodCall.setParsedClosureElement(getClosureElement(methodCall, (GrClosableBlock) expression, propertyName));
} else {
GradleDslExpression dslExpression = getExpressionElement(methodCall, expression, propertyName, expression);
if (dslExpression != null) {
methodCall.addParsedExpression(dslExpression);
}
}
}
GrNamedArgument[] namedArguments = argumentList.getNamedArguments();
if (namedArguments.length > 0) {
methodCall.addParsedExpressionMap(getExpressionMap(methodCall, argumentList, propertyName, namedArguments));
}
return methodCall;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project android by JetBrains.
the class GradleDslParser method getExpressionMap.
@NotNull
private static GradleDslExpressionMap getExpressionMap(@NotNull GradleDslElement parentElement, // GrArgumentList or GrListOrMap
@NotNull GroovyPsiElement mapPsiElement, @NotNull String propertyName, @NotNull GrNamedArgument... namedArguments) {
GradleDslExpressionMap expressionMap = new GradleDslExpressionMap(parentElement, mapPsiElement, propertyName);
for (GrNamedArgument namedArgument : namedArguments) {
String argName = namedArgument.getLabelName();
if (isEmpty(argName)) {
continue;
}
GrExpression valueExpression = namedArgument.getExpression();
if (valueExpression == null) {
continue;
}
GradleDslElement valueElement = getExpressionElement(expressionMap, mapPsiElement, argName, valueExpression);
if (valueElement == null && valueExpression instanceof GrListOrMap) {
GrListOrMap listOrMap = (GrListOrMap) valueExpression;
if (listOrMap.isMap()) {
valueElement = getExpressionMap(expressionMap, listOrMap, argName, listOrMap.getNamedArguments());
} else {
// ex: flatDir name: "libs", dirs: ["libs1", "libs2"]
valueElement = getExpressionList(expressionMap, listOrMap, argName, listOrMap.getInitializers());
}
}
if (valueElement != null) {
expressionMap.setParsedElement(argName, valueElement);
}
}
return expressionMap;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project android by JetBrains.
the class GradleDslParser method parse.
private static boolean parse(@NotNull GrAssignmentExpression assignment, @NotNull GradlePropertiesDslElement blockElement) {
PsiElement operationToken = assignment.getOperationToken();
if (!operationToken.getText().equals("=")) {
// TODO: Add support for other operators like +=.
return false;
}
GrExpression left = assignment.getLValue();
String name = left.getText();
if (isEmpty(name)) {
return false;
}
List<String> nameSegments = Splitter.on('.').splitToList(name);
if (nameSegments.size() > 1) {
GradlePropertiesDslElement nestedElement = getBlockElement(nameSegments.subList(0, nameSegments.size() - 1), blockElement);
if (nestedElement != null) {
blockElement = nestedElement;
} else {
return false;
}
}
String propertyName = nameSegments.get(nameSegments.size() - 1).trim();
GrExpression right = assignment.getRValue();
if (right == null) {
return false;
}
GradleDslElement propertyElement;
if (right instanceof GrListOrMap) {
GrListOrMap listOrMap = (GrListOrMap) right;
if (listOrMap.isMap()) {
// ex: manifestPlaceholders = [activityLabel1:"defaultName1", activityLabel2:"defaultName2"]
propertyElement = getExpressionMap(blockElement, listOrMap, propertyName, listOrMap.getNamedArguments());
} else {
// ex: proguardFiles = ['proguard-android.txt', 'proguard-rules.pro']
propertyElement = getExpressionList(blockElement, listOrMap, propertyName, listOrMap.getInitializers());
}
} else {
propertyElement = getExpressionElement(blockElement, assignment, propertyName, right);
}
if (propertyElement == null) {
return false;
}
blockElement.setParsedElement(propertyName, propertyElement);
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project android by JetBrains.
the class GradleDslExpressionList method create.
@Override
@Nullable
public GroovyPsiElement create() {
GroovyPsiElement psiElement = getPsiElement();
if (psiElement == null) {
if (myParent instanceof GradleDslExpressionMap) {
// This is a list in the map element and we need to create a named argument for it.
return createNamedArgumentList();
}
psiElement = super.create();
}
if (psiElement == null) {
return null;
}
if (psiElement instanceof GrListOrMap) {
return psiElement;
}
if (psiElement instanceof GrArgumentList) {
if (!myToBeAddedExpressions.isEmpty() && ((GrArgumentList) psiElement).getAllArguments().length == 1 && !myAppendToArgumentListWithOneElement) {
// Sometimes it's not possible to append to the arguments list with one item. eg. proguardFile "xyz".
// Set the psiElement to null and create a new psiElement of an empty application statement.
setPsiElement(null);
psiElement = super.create();
} else {
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;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project android by JetBrains.
the class GradleDslLiteral method apply.
@Override
protected void apply() {
GroovyPsiElement psiElement = getPsiElement();
if (psiElement == null) {
return;
}
GrLiteral newLiteral = createLiteral();
if (newLiteral == null) {
return;
}
if (myExpression != null) {
PsiElement replace = myExpression.replace(newLiteral);
if (replace instanceof GrLiteral) {
myExpression = (GrLiteral) replace;
}
} else {
PsiElement added;
if (// Entries in [].
psiElement instanceof GrListOrMap || (psiElement instanceof GrArgumentList && !(psiElement instanceof GrCommandArgumentList))) {
// Method call arguments in ().
// add before ) or ]
added = psiElement.addBefore(newLiteral, psiElement.getLastChild());
} else {
added = psiElement.addAfter(newLiteral, psiElement.getLastChild());
}
if (added instanceof GrLiteral) {
myExpression = (GrLiteral) added;
}
if (myUnsavedConfigBlock != null) {
addConfigBlock();
}
}
}
Aggregations