Search in sources :

Example 46 with GroovyPsiElement

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();
        }
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 47 with GroovyPsiElement

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

Example 48 with GroovyPsiElement

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;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) 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) Nullable(org.jetbrains.annotations.Nullable)

Example 49 with GroovyPsiElement

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();
        }
    }
}
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)

Example 50 with GroovyPsiElement

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

Aggregations

GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)98 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)17 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)8 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)8 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6