Search in sources :

Example 16 with GrStatementOwner

use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner in project android by JetBrains.

the class GradleBuildFile method removeValue.

/**
   * If the given key has a value at the given root, removes it and returns true. Returns false if there is no value for that key.
   */
public boolean removeValue(@Nullable GrStatementOwner root, @NotNull BuildFileKey key) {
    checkInitialized();
    commitDocumentChanges();
    if (root == null) {
        root = myGroovyFile;
    }
    GrMethodCall method = getMethodCallByPath(root, key.getPath());
    if (method != null) {
        GrStatementOwner parent = (GrStatementOwner) method.getParent();
        parent.removeElements(new PsiElement[] { method });
        reformatClosure(parent);
        return true;
    }
    return false;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)

Example 17 with GrStatementOwner

use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner in project intellij-community by JetBrains.

the class GroovyFix method replaceStatement.

/**
   * unwraps surrounding blocks from newStatement.
   */
protected static void replaceStatement(GrStatement oldStatement, GrStatement newStatement) throws IncorrectOperationException {
    if (newStatement instanceof GrBlockStatement) {
        GrBlockStatement blockStatement = (GrBlockStatement) newStatement;
        final GrOpenBlock openBlock = blockStatement.getBlock();
        final GrStatement[] statements = openBlock.getStatements();
        if (statements.length == 0) {
            oldStatement.removeStatement();
        } else {
            final PsiElement parent = oldStatement.getParent();
            if (parent instanceof GrStatementOwner) {
                GrStatementOwner statementOwner = (GrStatementOwner) parent;
                for (GrStatement statement : statements) {
                    statementOwner.addStatementBefore(statement, oldStatement);
                }
                oldStatement.removeStatement();
            } else if (parent instanceof GrControlStatement) {
                oldStatement.replace(newStatement);
            }
        }
    } else {
        oldStatement.replaceWithStatement(newStatement);
    }
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrBlockStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 18 with GrStatementOwner

use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner in project android by JetBrains.

the class GradleBuildFileTest method testSetIntegerOrStringAsInteger.

public void testSetIntegerOrStringAsInteger() throws Exception {
    final GradleBuildFile file = getTestFile("android {\n" + "    defaultConfig {\n" + "    }\n" + "}\n");
    final GrStatementOwner closure = file.getClosure("android/defaultConfig");
    assertNotNull(closure);
    WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {

        @Override
        public void run() {
            file.setValue(closure, BuildFileKey.TARGET_SDK_VERSION, 5);
        }
    });
    String expected = "android {\n" + "    defaultConfig {\n" + "        targetSdkVersion 5\n" + "    }\n" + "}\n";
    assertContents(expected);
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)

Example 19 with GrStatementOwner

use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner in project android by JetBrains.

the class GradleBuildFileTest method testSetFileStringValue.

public void testSetFileStringValue() throws Exception {
    final GradleBuildFile file = getTestFile(getSimpleTestFile());
    final GrStatementOwner closure = file.getClosure("android/productFlavors/flavor1");
    final File replacementFile = new File("abc/def/foo.txt");
    WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {

        @Override
        public void run() {
            file.setValue(closure, BuildFileKey.PROGUARD_FILE, replacementFile);
        }
    });
    // We always expect system independent paths in build.gradle files.
    String expected = getSimpleTestFile().replaceAll("proguard-flavor1\\.txt", "abc/def/foo.txt");
    assertContents(expected);
    assertEquals(replacementFile, file.getValue(closure, BuildFileKey.PROGUARD_FILE));
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 20 with GrStatementOwner

use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner in project android by JetBrains.

the class GradleBuildFileTest method testGetIntegerOrStringAsInteger.

public void testGetIntegerOrStringAsInteger() throws Exception {
    final GradleBuildFile file = getTestFile("android {\n" + "    defaultConfig {\n" + "        targetSdkVersion 5\n" + "    }\n" + "}\n");
    GrStatementOwner closure = file.getClosure("android/defaultConfig");
    assertNotNull(closure);
    assertEquals("5", file.getValue(closure, BuildFileKey.TARGET_SDK_VERSION));
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)

Aggregations

GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)38 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)11 PsiElement (com.intellij.psi.PsiElement)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 File (java.io.File)4 List (java.util.List)4 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)4 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 ImmutableList (com.google.common.collect.ImmutableList)3 GrControlStatement (org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement)3 GrBlockStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement)3 BuildFileKey (com.android.tools.idea.gradle.parser.BuildFileKey)2 NamedObject (com.android.tools.idea.gradle.parser.NamedObject)2 ValueFactory (com.android.tools.idea.gradle.parser.ValueFactory)2 ASTNode (com.intellij.lang.ASTNode)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 IElementType (com.intellij.psi.tree.IElementType)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 NotNull (org.jetbrains.annotations.NotNull)2