Search in sources :

Example 51 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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();
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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 53 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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 54 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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 55 with GrStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project android by JetBrains.

the class GradleSettingsFile method addModule.

/**
   * Adds a reference to the module to the settings file, if there is not already one. The module path must be colon separated, with a
   * leading colon, e.g. ":project:subproject". Must be run inside a write action.
   *
   * If the file does not match the default module location, this method will override the location.
   */
public void addModule(@NotNull String modulePath, @NotNull File location) {
    checkInitialized();
    commitDocumentChanges();
    for (GrMethodCall includeStatement : getMethodCalls(myGroovyFile, INCLUDE_METHOD)) {
        for (GrLiteral lit : getLiteralArguments(includeStatement)) {
            if (modulePath.equals(lit.getValue())) {
                return;
            }
        }
    }
    GrMethodCall includeStatement = getMethodCall(myGroovyFile, INCLUDE_METHOD);
    if (includeStatement != null) {
        GrArgumentList argList = includeStatement.getArgumentList();
        GrLiteral literal = GroovyPsiElementFactory.getInstance(myProject).createLiteralFromValue(modulePath);
        argList.addAfter(literal, argList.getLastChild());
    } else {
        GrStatement statement = GroovyPsiElementFactory.getInstance(myProject).createStatementFromText(INCLUDE_METHOD + " '" + modulePath + "'");
        myGroovyFile.add(statement);
    }
    // We get location relative to this file parent
    VirtualFile parent = getFile().getParent();
    File defaultLocation = GradleUtil.getModuleDefaultPath(parent, modulePath);
    if (!FileUtil.filesEqual(defaultLocation, location)) {
        final String path;
        File parentFile = VfsUtilCore.virtualToIoFile(parent);
        if (FileUtil.isAncestor(parentFile, location, true)) {
            path = PathUtil.toSystemIndependentName(FileUtil.getRelativePath(parentFile, location));
        } else {
            path = PathUtil.toSystemIndependentName(location.getAbsolutePath());
        }
        String locationAssignment = String.format(CUSTOM_LOCATION_FORMAT, modulePath, path);
        GrStatement locationStatement = GroovyPsiElementFactory.getInstance(myProject).createStatementFromText(locationAssignment);
        myGroovyFile.add(locationStatement);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)113 PsiElement (com.intellij.psi.PsiElement)36 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)22 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)21 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)17 TextRange (com.intellij.openapi.util.TextRange)14 Nullable (org.jetbrains.annotations.Nullable)14 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)13 NotNull (org.jetbrains.annotations.NotNull)12 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)12 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 Document (com.intellij.openapi.editor.Document)6