Search in sources :

Example 16 with GrStringInjection

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

the class GrIntroduceHandlerBase method findAnchor.

@Nullable
public static PsiElement findAnchor(@NotNull PsiElement[] occurrences, @NotNull PsiElement container) {
    if (occurrences.length == 0)
        return null;
    PsiElement candidate;
    if (occurrences.length == 1) {
        candidate = findContainingStatement(occurrences[0]);
    } else {
        candidate = occurrences[0];
        while (candidate != null && candidate.getParent() != container) {
            candidate = candidate.getParent();
        }
    }
    final GrStringInjection injection = PsiTreeUtil.getParentOfType(candidate, GrStringInjection.class);
    if (injection != null && !injection.getText().contains("\n")) {
        candidate = findContainingStatement(injection);
    }
    if (candidate == null)
        return null;
    if ((container instanceof GrWhileStatement) && candidate.equals(((GrWhileStatement) container).getCondition())) {
        return container;
    }
    if ((container instanceof GrIfStatement) && candidate.equals(((GrIfStatement) container).getCondition())) {
        return container;
    }
    if ((container instanceof GrForStatement) && candidate.equals(((GrForStatement) container).getClause())) {
        return container;
    }
    while (candidate instanceof GrIfStatement && candidate.getParent() instanceof GrIfStatement && ((GrIfStatement) candidate.getParent()).getElseBranch() == candidate) {
        candidate = candidate.getParent();
    }
    return candidate;
}
Also used : GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with GrStringInjection

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

Aggregations

GrStringInjection (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)17 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)7 TextRange (com.intellij.openapi.util.TextRange)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 PsiElement (com.intellij.psi.PsiElement)5 GrStringContent (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent)5 Nullable (org.jetbrains.annotations.Nullable)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)4 GrLiteralImpl (org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl)3 ArrayList (java.util.ArrayList)2 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)2 GrStringImpl (org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrStringImpl)2 GradleResolvedVariable (com.android.tools.idea.gradle.dsl.parser.GradleResolvedVariable)1 ASTNode (com.intellij.lang.ASTNode)1 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)1 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)1 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)1