use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project android by JetBrains.
the class GradleDslParser method parse.
private static boolean parse(@NotNull GrMethodCallExpression expression, @NotNull GradlePropertiesDslElement dslElement) {
GrReferenceExpression referenceExpression = findChildOfType(expression, GrReferenceExpression.class);
if (referenceExpression == null) {
return false;
}
String name = referenceExpression.getText();
if (isEmpty(name)) {
return false;
}
List<String> nameSegments = Splitter.on('.').splitToList(name);
if (nameSegments.size() > 1) {
dslElement = getBlockElement(nameSegments.subList(0, (nameSegments.size() - 1)), dslElement);
name = nameSegments.get(nameSegments.size() - 1);
}
if (dslElement == null) {
return false;
}
GrClosableBlock[] closureArguments = expression.getClosureArguments();
GrArgumentList argumentList = expression.getArgumentList();
if (argumentList.getAllArguments().length > 0) {
// This element is a method call with arguments and an optional closure associated with it.
// ex: compile("dependency") {}
GradleDslExpression methodCall = getMethodCall(dslElement, expression, name, argumentList);
if (closureArguments.length > 0) {
methodCall.setParsedClosureElement(getClosureElement(methodCall, closureArguments[0], name));
}
dslElement.addParsedElement(name, methodCall);
return true;
}
if (argumentList.getAllArguments().length == 0 && closureArguments.length == 0) {
// This element is a pure method call, i.e a method call with no arguments and no closure arguments.
// ex: jcenter()
dslElement.addParsedElement(name, new GradleDslMethodCall(dslElement, expression, name));
return true;
}
// Now this element is pure block element, i.e a method call with no argument but just a closure argument. So, here just process the
// closure and treat it as a block element.
// ex: android {}
GrClosableBlock closableBlock = closureArguments[0];
// The block elements this closure needs to be applied.
List<GradlePropertiesDslElement> blockElements = Lists.newArrayList();
if (dslElement instanceof GradleDslFile && name.equals("allprojects")) {
// The "allprojects" closure needs to be applied to this project and all it's sub projects.
blockElements.add(dslElement);
// After applying the allprojects closure to this project, process it as subprojects section to also pass the same properties to
// subprojects.
name = "subprojects";
}
GradlePropertiesDslElement blockElement = getBlockElement(ImmutableList.of(name), dslElement);
if (blockElement != null) {
blockElement.setPsiElement(closableBlock);
blockElements.add(blockElement);
}
if (blockElements.isEmpty()) {
return false;
}
for (GradlePropertiesDslElement element : blockElements) {
parse(closableBlock, element);
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project android by JetBrains.
the class GradleDslElement method create.
/**
* Creates the {@link GroovyPsiElement} by adding this element to the .gradle file.
*
* <p>It creates a new {@link GroovyPsiElement} only when {@link #getPsiElement()} return {@code null}.
*
* <p>Returns the final {@link GroovyPsiElement} corresponds to this element or {@code null} when failed to create the
* {@link GroovyPsiElement}.
*/
@Nullable
public GroovyPsiElement create() {
GroovyPsiElement psiElement = getPsiElement();
if (psiElement != null) {
return psiElement;
}
if (myParent == null) {
return null;
}
GroovyPsiElement parentPsiElement = myParent.create();
if (parentPsiElement == null) {
return null;
}
Project project = parentPsiElement.getProject();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
if (isNewEmptyBlockElement()) {
// Avoid creation of an empty block statement.
return null;
}
String statementText = myName + (isBlockElement() ? " {\n}\n" : " \"abc\", \"xyz\"");
GrStatement statement = factory.createStatementFromText(statementText);
if (statement instanceof GrApplicationStatement) {
// Workaround to create an application statement.
((GrApplicationStatement) statement).getArgumentList().delete();
}
PsiElement lineTerminator = factory.createLineTerminator(1);
PsiElement addedElement;
if (parentPsiElement instanceof GroovyFile) {
addedElement = parentPsiElement.addAfter(statement, parentPsiElement.getLastChild());
parentPsiElement.addBefore(lineTerminator, addedElement);
} else {
addedElement = parentPsiElement.addBefore(statement, parentPsiElement.getLastChild());
parentPsiElement.addAfter(lineTerminator, addedElement);
}
if (isBlockElement()) {
GrClosableBlock closableBlock = getClosableBlock(addedElement);
if (closableBlock != null) {
setPsiElement(closableBlock);
}
} else {
if (addedElement instanceof GrApplicationStatement) {
setPsiElement((GrApplicationStatement) addedElement);
}
}
return getPsiElement();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project android by JetBrains.
the class ArtifactDependencyModel method buildExcludesBlock.
private static GrClosableBlock buildExcludesBlock(@NotNull List<ArtifactDependencySpec> excludes, @NotNull Project project) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrClosableBlock block = factory.createClosureFromText("{\n}");
for (ArtifactDependencySpec spec : excludes) {
String text = String.format("exclude group: '%s', module: '%s'", spec.group, spec.name);
block.addBefore(factory.createStatementFromText(text), block.getLastChild());
PsiElement lineTerminator = factory.createLineTerminator(1);
block.addBefore(lineTerminator, block.getLastChild());
}
return block;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project android by JetBrains.
the class GradleDslParser method parse.
private static boolean parse(@NotNull GrApplicationStatement statement, @NotNull GradlePropertiesDslElement blockElement) {
GrReferenceExpression referenceExpression = getChildOfType(statement, GrReferenceExpression.class);
if (referenceExpression == null) {
return false;
}
GrCommandArgumentList argumentList = getNextSiblingOfType(referenceExpression, GrCommandArgumentList.class);
if (argumentList == null) {
return false;
}
GroovyPsiElement[] arguments = argumentList.getAllArguments();
if (arguments.length == 0) {
return false;
}
String name = referenceExpression.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);
GradleDslElement propertyElement = null;
if (arguments.length == 1) {
GroovyPsiElement element = arguments[0];
if (element instanceof GrExpression) {
propertyElement = getExpressionElement(blockElement, argumentList, propertyName, (GrExpression) element);
} else if (element instanceof GrNamedArgument) {
// ex: manifestPlaceholders activityLabel:"defaultName"
propertyElement = getExpressionMap(blockElement, argumentList, propertyName, (GrNamedArgument) element);
}
} else {
if (arguments[0] instanceof GrExpression) {
// ex: proguardFiles 'proguard-android.txt', 'proguard-rules.pro'
GrExpression[] expressions = new GrExpression[arguments.length];
for (int i = 0; i < expressions.length; i++) {
expressions[i] = (GrExpression) arguments[i];
}
propertyElement = getExpressionList(blockElement, argumentList, propertyName, expressions);
} else if (arguments[0] instanceof GrNamedArgument) {
// ex: manifestPlaceholders activityLabel1:"defaultName1", activityLabel2:"defaultName2"
GrNamedArgument[] namedArguments = new GrNamedArgument[arguments.length];
for (int i = 0; i < arguments.length; i++) {
namedArguments[i] = (GrNamedArgument) arguments[i];
}
propertyElement = getExpressionMap(blockElement, argumentList, propertyName, namedArguments);
}
}
if (propertyElement == null) {
return false;
}
GroovyPsiElement lastArgument = arguments[arguments.length - 1];
if (lastArgument instanceof GrClosableBlock) {
propertyElement.setParsedClosureElement(getClosureElement(propertyElement, (GrClosableBlock) lastArgument, name));
}
blockElement.addParsedElement(propertyName, propertyElement);
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project android by JetBrains.
the class ArtifactDependencyModel method createAndAddToList.
static void createAndAddToList(@NotNull GradleDslElementList list, @NotNull String configurationName, @NotNull ArtifactDependencySpec dependency, @NotNull List<ArtifactDependencySpec> excludes) {
GradleDslLiteral literal = new GradleDslLiteral(list, configurationName);
literal.setValue(dependency.compactNotation());
if (!excludes.isEmpty()) {
GrClosableBlock configBlock = buildExcludesBlock(excludes, list.getDslFile().getProject());
literal.setConfigBlock(configBlock);
}
list.addNewElement(literal);
}
Aggregations