Search in sources :

Example 71 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory 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 72 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory 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 73 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project freeline by alibaba.

the class GradleUtil method applyPlugin.

/**
     * 插入插件的表达式
     * apply plugin: 'com.antfortune.freeline'
     *
     * @param project
     * @param psiFile
     * @param pluginId
     */
public static void applyPlugin(Project project, GroovyFile psiFile, String pluginId) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    GrStatement grStatement = factory.createExpressionFromText(String.format("apply plugin: \'%s\'", new Object[] { pluginId }), null);
    GrExpression expression = GroovyFileUil.getLastPlugin(psiFile);
    if (expression != null && expression.getParent() != null) {
        psiFile.addAfter(grStatement, expression.getParent());
        // 换行
        psiFile.addAfter(factory.createLineTerminator("\n"), expression.getParent());
    }
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    Document document = documentManager.getDocument(psiFile);
    if (document != null) {
        documentManager.commitDocument(document);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Document(com.intellij.openapi.editor.Document) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 74 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.

the class GradleGroovyFile method createNewValue.

/**
   * Creates a new, blank-valued property at the given path.
   */
@Nullable
static GrMethodCall createNewValue(@NotNull GrStatementOwner root, @NotNull BuildFileKey key, @Nullable Object value, boolean reformatClosure) {
    // First iterate through the components of the path and make sure all of the nested closures are in place.
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(root.getProject());
    String path = key.getPath();
    String[] parts = path.split("/");
    GrStatementOwner parent = root;
    for (int i = 0; i < parts.length - 1; i++) {
        String part = parts[i];
        GrStatementOwner closure = getMethodClosureArgument(parent, part);
        if (closure == null) {
            parent.addStatementBefore(factory.createStatementFromText(part + " {}"), null);
            closure = getMethodClosureArgument(parent, part);
            if (closure == null) {
                return null;
            }
        }
        parent = closure;
    }
    String name = parts[parts.length - 1];
    String text = name + " " + key.getType().convertValueToExpression(value);
    GrStatement statementBefore = null;
    if (key.shouldInsertAtBeginning()) {
        GrStatement[] parentStatements = parent.getStatements();
        if (parentStatements.length > 0) {
            statementBefore = parentStatements[0];
        }
    }
    parent.addStatementBefore(factory.createStatementFromText(text), statementBefore);
    if (reformatClosure) {
        reformatClosure(parent);
    }
    return getMethodCall(parent, name);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) BuildFileKey.escapeLiteralString(com.android.tools.idea.gradle.parser.BuildFileKey.escapeLiteralString) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 75 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.

the class BuildFileStatementFactory method setValues.

@Override
public void setValues(@NotNull GrStatementOwner closure, @NotNull List<BuildFileStatement> statements, @Nullable KeyFilter filter) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(closure.getProject());
    closure = (GrStatementOwner) closure.replace(factory.createClosureFromText("{\n}"));
    for (BuildFileStatement statement : statements) {
        for (PsiElement element : statement.getGroovyElements(factory)) {
            closure.addBefore(element, closure.getLastChild());
        }
    }
    GradleGroovyFile.reformatClosure(closure);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)159 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)42 PsiElement (com.intellij.psi.PsiElement)30 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)23 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)22 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)21 IncorrectOperationException (com.intellij.util.IncorrectOperationException)20 Nullable (org.jetbrains.annotations.Nullable)20 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)18 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)17 NotNull (org.jetbrains.annotations.NotNull)16 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)15 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)14 Project (com.intellij.openapi.project.Project)8 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)8 ASTNode (com.intellij.lang.ASTNode)7 ArrayList (java.util.ArrayList)7 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6