Search in sources :

Example 11 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 12 with GrStatementOwner

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

the class NamedObjectPanel method init.

@Override
public void init() {
    super.init();
    myPanelGroup.myPanes.add(myDetailsPane);
    if (myGradleBuildFile == null) {
        return;
    }
    List<NamedObject> namedObjects = (List<NamedObject>) myGradleBuildFile.getValue(myBuildFileKey);
    if (namedObjects != null) {
        for (NamedObject object : namedObjects) {
            // consistent between the case where you've customized it and you haven't.
            if (myBuildFileKey == BuildFileKey.BUILD_TYPES && HARDCODED_BUILD_TYPES.contains(object.getName())) {
                object = new UndeletableNamedObject(object);
            }
            addElement(object);
        }
    }
    // If this is a flavor panel, add a synthetic flavor entry for defaultConfig.
    if (myBuildFileKey == BuildFileKey.FLAVORS) {
        GrStatementOwner defaultConfig = myGradleBuildFile.getClosure(BuildFileKey.DEFAULT_CONFIG.getPath());
        NamedObject obj = new UndeletableNamedObject(DEFAULT_CONFIG);
        if (defaultConfig != null) {
            for (BuildFileKey key : DEFAULT_CONFIG_KEYS) {
                Object value = myGradleBuildFile.getValue(defaultConfig, key);
                obj.setValue(key, value);
            }
        }
        addElement(obj);
    }
    NamedObject.Factory objectFactory = (NamedObject.Factory) myBuildFileKey.getValueFactory();
    if (objectFactory == null) {
        throw new IllegalArgumentException("Can't instantiate a NamedObjectPanel for BuildFileKey " + myBuildFileKey.toString());
    }
    Collection<BuildFileKey> properties = objectFactory.getProperties();
    // Query the model for its view of the world, and merge it with the build file-based view.
    for (NamedObject obj : getObjectsFromModel(properties)) {
        boolean found = false;
        for (NamedObject o : myListModel) {
            if (o.getName().equals(obj.getName())) {
                found = true;
            }
        }
        if (!found) {
            NamedObject namedObject = new UndeletableNamedObject(obj.getName());
            addElement(namedObject);
            // Keep track of objects that are only in the model and not in the build file. We want to avoid creating them in the build file
            // unless some value in them is changed to non-default.
            myModelOnlyObjects.add(namedObject);
        }
        myModelObjects.put(obj.getName(), obj.getValues());
    }
    myList.updateUI();
    myDetailsPane.init(myGradleBuildFile, properties);
    if (myListModel.getSize() > 0) {
        myList.setSelectedIndex(0);
    }
    updateUiFromCurrentObject();
    ApplicationManager.getApplication().invokeLater(new Runnable() {

        @Override
        public void run() {
            updatePanelGroup();
        }
    }, ModalityState.any());
}
Also used : NamedObject(com.android.tools.idea.gradle.parser.NamedObject) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) ValueFactory(com.android.tools.idea.gradle.parser.ValueFactory) JBList(com.intellij.ui.components.JBList) SortedList(com.intellij.util.containers.SortedList) List(java.util.List) NamedObject(com.android.tools.idea.gradle.parser.NamedObject) BuildFileKey(com.android.tools.idea.gradle.parser.BuildFileKey)

Example 13 with GrStatementOwner

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

the class GradleBuildFileTest method testSetBooleanValue.

public void testSetBooleanValue() throws Exception {
    final GradleBuildFile file = getTestFile(getSimpleTestFile());
    final GrStatementOwner closure = file.getClosure("android/buildTypes/debug");
    WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() {

        @Override
        public void run() {
            file.setValue(closure, BuildFileKey.DEBUGGABLE, false);
        }
    });
    String expected = getSimpleTestFile().replaceAll("debuggable true", "debuggable false");
    assertContents(expected);
    assertEquals(false, file.getValue(closure, BuildFileKey.DEBUGGABLE));
}
Also used : GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)

Example 14 with GrStatementOwner

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

the class GradleBuildFileTest method testSetIntegerOrStringAsIntegerValuedString.

public void testSetIntegerOrStringAsIntegerValuedString() 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 15 with GrStatementOwner

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

the class GradleBuildFileTest method testGetIntegerOrStringAsString.

public void testGetIntegerOrStringAsString() throws Exception {
    final GradleBuildFile file = getTestFile("android {\n" + "    defaultConfig {\n" + "        targetSdkVersion 'foo'\n" + "    }\n" + "}\n");
    GrStatementOwner closure = file.getClosure("android/defaultConfig");
    assertNotNull(closure);
    assertEquals("foo", 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