use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project android by JetBrains.
the class GradleDslExpressionList method apply.
@Override
protected void apply() {
GroovyPsiElement psiElement = create();
if (psiElement != null) {
for (GradleDslExpression expression : myToBeAddedExpressions) {
expression.setPsiElement(psiElement);
expression.applyChanges();
myExpressions.add(expression);
}
}
myToBeAddedExpressions.clear();
for (GradleDslExpression expression : myToBeRemovedExpressions) {
if (myExpressions.remove(expression)) {
expression.delete();
}
}
myToBeRemovedExpressions.clear();
for (GradleDslExpression expression : myExpressions) {
if (expression.isModified()) {
expression.applyChanges();
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project android by JetBrains.
the class GradleDslMethodCall method create.
@Override
@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;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
String statementText = (myStatementName != null ? myStatementName + " " : "") + myName + "()";
GrStatement statement = factory.createStatementFromText(statementText);
PsiElement addedElement = parentPsiElement.addBefore(statement, parentPsiElement.getLastChild());
if (addedElement instanceof GrApplicationStatement) {
GrExpression[] expressionArguments = ((GrApplicationStatement) addedElement).getArgumentList().getExpressionArguments();
if (expressionArguments.length == 1 && expressionArguments[0] instanceof GrMethodCallExpression) {
setPsiElement(expressionArguments[0]);
return getPsiElement();
}
}
if (addedElement instanceof GrMethodCallExpression) {
setPsiElement((GrMethodCallExpression) addedElement);
return getPsiElement();
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement 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.GroovyPsiElement 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.GroovyPsiElement 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;
}
Aggregations