use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList 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.statements.arguments.GrArgumentList in project android by JetBrains.
the class GradleDslLiteral method create.
@Override
@Nullable
public GroovyPsiElement create() {
if (!(myParent instanceof GradleDslExpressionMap)) {
return super.create();
}
// This is a value in the map element we need to create a named argument for it.
GroovyPsiElement parentPsiElement = myParent.create();
if (parentPsiElement == null) {
return null;
}
setPsiElement(parentPsiElement);
GrLiteral newLiteral = createLiteral();
if (newLiteral == null) {
return null;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(newLiteral.getProject());
GrNamedArgument namedArgument = factory.createNamedArgument(myName, newLiteral);
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;
GrLiteral literal = getChildOfType(addedNameArgument, GrLiteral.class);
if (literal != null) {
myExpression = literal;
setModified(false);
return getPsiElement();
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList 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();
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.
the class GradleFilePsiMerger method pullDependenciesIntoMap.
/**
* Looks for statements adding dependencies to different configurations (which look like 'configurationName "dependencyCoordinate"')
* and tries to parse them into Gradle coordinates. If successful, adds the new coordinate to the map and removes the corresponding
* PsiElement from the tree.
*
* @return true if new items were added to the map
*/
private static boolean pullDependenciesIntoMap(@NotNull PsiElement root, @NotNull Map<String, Multimap<String, GradleCoordinate>> allConfigurations, @Nullable List<String> unparsedDependencies) {
boolean wasMapUpdated = false;
for (PsiElement existingElem : root.getChildren()) {
if (existingElem instanceof GrCall) {
PsiElement reference = existingElem.getFirstChild();
if (reference instanceof GrReferenceExpression) {
final String configurationName = reference.getText();
boolean parsed = false;
GrCall call = (GrCall) existingElem;
GrArgumentList arguments = call.getArgumentList();
// Don't try merging dependencies if one of them has a closure block attached.
if (arguments != null && call.getClosureArguments().length == 0) {
GrExpression[] expressionArguments = arguments.getExpressionArguments();
if (expressionArguments.length == 1 && expressionArguments[0] instanceof GrLiteral) {
Object value = ((GrLiteral) expressionArguments[0]).getValue();
if (value instanceof String) {
String coordinateText = (String) value;
GradleCoordinate coordinate = GradleCoordinate.parseCoordinateString(coordinateText);
if (coordinate != null) {
parsed = true;
Multimap<String, GradleCoordinate> map = allConfigurations.get(configurationName);
if (map == null) {
map = LinkedListMultimap.create();
allConfigurations.put(configurationName, map);
}
if (!map.get(coordinate.getId()).contains(coordinate)) {
map.put(coordinate.getId(), coordinate);
existingElem.delete();
wasMapUpdated = true;
}
}
}
}
if (!parsed && unparsedDependencies != null) {
unparsedDependencies.add(existingElem.getText());
}
}
}
}
}
return wasMapUpdated;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.
the class GradleGroovyFile method getNamedArgumentValues.
/**
* If the given method takes named arguments, returns those arguments as a name:value map. Returns an empty map otherwise.
*/
@NotNull
protected static Map<String, Object> getNamedArgumentValues(@NotNull GrMethodCall gmc) {
GrArgumentList argumentList = gmc.getArgumentList();
Map<String, Object> values = Maps.newHashMap();
for (GrNamedArgument grNamedArgument : getTypedArguments(argumentList, GrNamedArgument.class)) {
values.put(grNamedArgument.getLabelName(), parseValueExpression(grNamedArgument.getExpression()));
}
return values;
}
Aggregations