Search in sources :

Example 41 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral 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)

Example 42 with GrLiteral

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

the class GradleSettingsFile method removeModule.

/**
   * Removes the reference to the module from the settings file, if present. The module path must be colon separated, with a
   * leading colon, e.g. ":project:subproject". Must be run inside a write action.
   */
public void removeModule(@NotNull String modulePath) {
    checkInitialized();
    commitDocumentChanges();
    boolean removedAnyIncludes = false;
    for (GrMethodCall includeStatement : getMethodCalls(myGroovyFile, INCLUDE_METHOD)) {
        for (GrLiteral lit : getLiteralArguments(includeStatement)) {
            if (modulePath.equals(lit.getValue())) {
                lit.delete();
                removedAnyIncludes = true;
                if (getArguments(includeStatement).length == 0) {
                    includeStatement.delete();
                // If this happens we will fall through both for loops before we get into iteration trouble. We want to keep iterating in
                // case the module is added more than once (via hand-editing of the file).
                }
            }
        }
    }
    if (removedAnyIncludes) {
        for (Pair<String, GrAssignmentExpression> pair : getAllProjectLocationStatements()) {
            if (modulePath.equals(pair.first)) {
                pair.second.delete();
            }
        }
    }
}
Also used : GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 43 with GrLiteral

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

the class GradleDslLiteral method getValue.

@Override
@Nullable
public Object getValue() {
    if (myUnsavedValue != null) {
        return myUnsavedValue;
    }
    if (myExpression == null) {
        return null;
    }
    Object value = ((GrLiteral) myExpression).getValue();
    if (value != null) {
        return value;
    }
    if (myExpression instanceof GrString) {
        // String literal with variables. ex: compileSdkVersion = "$ANDROID-${VERSION}"
        String literalText = myExpression.getText();
        if (isQuotedString(literalText)) {
            literalText = unquoteString(literalText);
        }
        List<GradleResolvedVariable> resolvedVariables = Lists.newArrayList();
        GrStringInjection[] injections = ((GrString) myExpression).getInjections();
        for (GrStringInjection injection : injections) {
            String variableName = null;
            GrClosableBlock closableBlock = injection.getClosableBlock();
            if (closableBlock != null) {
                String blockText = closableBlock.getText();
                variableName = blockText.substring(1, blockText.length() - 1);
            } else {
                GrExpression expression = injection.getExpression();
                if (expression != null) {
                    variableName = expression.getText();
                }
            }
            if (!isEmpty(variableName)) {
                GradleDslExpression resolvedExpression = resolveReference(variableName, GradleDslExpression.class);
                if (resolvedExpression != null) {
                    Object resolvedValue = resolvedExpression.getValue();
                    if (resolvedValue != null) {
                        resolvedVariables.add(new GradleResolvedVariable(variableName, resolvedValue, resolvedExpression));
                        literalText = literalText.replace(injection.getText(), resolvedValue.toString());
                    }
                }
            }
        }
        setResolvedVariables(resolvedVariables);
        return literalText;
    }
    return null;
}
Also used : GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GradleResolvedVariable(com.android.tools.idea.gradle.dsl.parser.GradleResolvedVariable) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) Nullable(org.jetbrains.annotations.Nullable)

Example 44 with GrLiteral

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

the class GradleDslLiteral method createLiteral.

@Nullable
private GrLiteral createLiteral() {
    if (myUnsavedValue == null) {
        return null;
    }
    CharSequence unsavedValueText = null;
    if (myUnsavedValue instanceof String) {
        unsavedValueText = GrStringUtil.getLiteralTextByValue((String) myUnsavedValue);
    } else if (myUnsavedValue instanceof Integer || myUnsavedValue instanceof Boolean) {
        unsavedValueText = myUnsavedValue.toString();
    }
    myUnsavedValue = null;
    if (unsavedValueText == null) {
        return null;
    }
    GroovyPsiElementFactory factory = getPsiElementFactory();
    if (factory == null) {
        return null;
    }
    GrExpression newExpression = factory.createExpressionFromText(unsavedValueText);
    if (!(newExpression instanceof GrLiteral)) {
        return null;
    }
    return (GrLiteral) newExpression;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-plugins by JetBrains.

the class GrCucumberStepDeclarationSearcher method findDeclarationsAt.

@Override
public void findDeclarationsAt(@NotNull PsiElement element, int offsetInElement, Consumer<PomTarget> consumer) {
    PsiLanguageInjectionHost host = InjectedLanguageManager.getInstance(element.getProject()).getInjectionHost(element);
    if (host != null) {
        element = host;
    }
    if (element.getParent() instanceof GrLiteral) {
        element = element.getParent();
    }
    if (element instanceof GrLiteral) {
        //~literal
        final PsiElement parent = element.getParent();
        if (parent != null) {
            //(~literal)
            final PsiElement pparent = parent.getParent();
            if (pparent != null) {
                //When(~literal)
                final PsiElement ppparent = pparent.getParent();
                if (ppparent instanceof GrMethodCall && GrCucumberUtil.isStepDefinition(ppparent)) {
                    final GrMethodCall methodCall = (GrMethodCall) ppparent;
                    consumer.consume(GrStepDefinition.getStepDefinition(methodCall));
                }
            }
        }
    }
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)45 Nullable (org.jetbrains.annotations.Nullable)14 PsiElement (com.intellij.psi.PsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)10 NonNls (org.jetbrains.annotations.NonNls)9 PsiType (com.intellij.psi.PsiType)6 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 TextRange (com.intellij.openapi.util.TextRange)4 BigInteger (java.math.BigInteger)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 GrStringInjection (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)4 Document (com.intellij.openapi.editor.Document)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)3 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)3 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)3