Search in sources :

Example 16 with GrListOrMap

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;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with GrListOrMap

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;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with GrListOrMap

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;
}
Also used : GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 19 with GrListOrMap

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;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with GrListOrMap

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();
        }
    }
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)39 PsiElement (com.intellij.psi.PsiElement)13 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)12 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)12 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 Nullable (org.jetbrains.annotations.Nullable)9 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)9 NotNull (org.jetbrains.annotations.NotNull)8 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)5 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)4 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)4 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)4 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)3 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)3 ASTNode (com.intellij.lang.ASTNode)2 Pair (com.intellij.openapi.util.Pair)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 IElementType (com.intellij.psi.tree.IElementType)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2