Search in sources :

Example 1 with GrStringInjection

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

the class ConvertGStringToStringIntention method convertGStringLiteralToStringLiteral.

public static String convertGStringLiteralToStringLiteral(GrLiteral literal) {
    PsiElement child = literal.getFirstChild();
    if (child == null)
        return literal.getText();
    String text;
    ArrayList<String> list = new ArrayList<>();
    PsiElement prevSibling = null;
    PsiElement nextSibling;
    do {
        text = child.getText();
        nextSibling = child.getNextSibling();
        if (child instanceof GrStringInjection) {
            if (((GrStringInjection) child).getClosableBlock() != null) {
                text = prepareClosableBlock(((GrStringInjection) child).getClosableBlock());
            } else if (((GrStringInjection) child).getExpression() != null) {
                text = prepareExpression(((GrStringInjection) child).getExpression());
            } else {
                text = child.getText();
            }
        } else {
            text = prepareText(text, prevSibling == null, nextSibling == null, nextSibling instanceof GrClosableBlock || nextSibling instanceof GrReferenceExpression);
        }
        if (text != null) {
            list.add(text);
        }
        prevSibling = child;
        child = child.getNextSibling();
    } while (child != null);
    StringBuilder builder = new StringBuilder(literal.getTextLength() * 2);
    if (list.isEmpty())
        return "''";
    builder.append(list.get(0));
    for (int i = 1; i < list.size(); i++) {
        builder.append(" + ").append(list.get(i));
    }
    return builder.toString();
}
Also used : ArrayList(java.util.ArrayList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 2 with GrStringInjection

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

the class ConvertStringToMultilineIntention method prepareNewLiteralText.

private static StringBuilder prepareNewLiteralText(List<GrLiteral> literals) {
    String quote = !containsInjections(literals) && literals.get(0).getText().startsWith("'") ? "'''" : "\"\"\"";
    final StringBuilder buffer = new StringBuilder();
    buffer.append(quote);
    for (GrLiteral literal : literals) {
        if (literal instanceof GrLiteralImpl) {
            appendSimpleStringValue(literal, buffer, quote);
        } else {
            final GrStringImpl gstring = (GrStringImpl) literal;
            for (PsiElement child : gstring.getAllContentParts()) {
                if (child instanceof GrStringContent) {
                    appendSimpleStringValue(child, buffer, "\"\"\"");
                } else if (child instanceof GrStringInjection) {
                    buffer.append(child.getText());
                }
            }
        }
    }
    buffer.append(quote);
    return buffer;
}
Also used : GrStringContent(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent) GrLiteralImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl) GrStringImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrStringImpl) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiElement(com.intellij.psi.PsiElement)

Example 3 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 expressionIsIncorrect.

public static boolean expressionIsIncorrect(@Nullable GrExpression expression, boolean acceptVoidCalls) {
    if (expression instanceof GrParenthesizedExpression)
        return true;
    if (PsiUtil.isSuperReference(expression))
        return true;
    if (expression instanceof GrAssignmentExpression)
        return true;
    if (expression instanceof GrReferenceExpression && expression.getParent() instanceof GrCall) {
        final GroovyResolveResult resolveResult = ((GrReferenceExpression) expression).advancedResolve();
        final PsiElement resolved = resolveResult.getElement();
        return resolved instanceof PsiMethod && !resolveResult.isInvokedOnProperty() || resolved instanceof PsiClass;
    }
    if (expression instanceof GrReferenceExpression && expression.getParent() instanceof GrReferenceExpression) {
        return !PsiUtil.isThisReference(expression) && ((GrReferenceExpression) expression).resolve() instanceof PsiClass;
    }
    if (expression instanceof GrClosableBlock && expression.getParent() instanceof GrStringInjection)
        return true;
    if (!acceptVoidCalls && expression instanceof GrMethodCall && PsiType.VOID.equals(expression.getType()))
        return true;
    return false;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 4 with GrStringInjection

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

the class GrIntroduceFieldDialog method canBeInitializedOutsideBlock.

private static boolean canBeInitializedOutsideBlock(@NotNull GrIntroduceContext context, @NotNull PsiClass clazz) {
    final StringPartInfo part = context.getStringPart();
    GrExpression expression = context.getExpression();
    if (expression != null) {
        expression = (GrExpression) PsiUtil.skipParentheses(expression, false);
        if (expression == null)
            return false;
        if (expression instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) expression).resolve();
            if (PsiUtil.isLocalVariable(resolved)) {
                expression = ((GrVariable) resolved).getInitializerGroovy();
                if (expression == null)
                    return false;
            }
        }
        ExpressionChecker visitor = new ExpressionChecker(clazz, expression);
        expression.accept(visitor);
        return visitor.isResult();
    }
    if (part != null) {
        for (GrStringInjection injection : part.getInjections()) {
            GroovyPsiElement scope = injection.getExpression() != null ? injection.getExpression() : injection.getClosableBlock();
            assert scope != null;
            ExpressionChecker visitor = new ExpressionChecker(clazz, scope);
            scope.accept(visitor);
            if (!visitor.isResult()) {
                return visitor.isResult();
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 5 with GrStringInjection

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

the class GroovySpacingProcessor method visitClosure.

@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
    ASTNode rBraceAtTheEnd = GeeseUtil.getClosureRBraceAtTheEnd(myChild1);
    if (myGroovySettings.USE_FLYING_GEESE_BRACES && myType2 == GroovyTokenTypes.mRCURLY && rBraceAtTheEnd != null) {
        String text = rBraceAtTheEnd.getTreeParent().getText();
        if (text.indexOf('\n') < 0) {
            /* the case:
       foo {
         bar {print x}<we are here>}
        */
            myResult = Spacing.createSpacing(1, 1, 1, false, 1);
        } else {
            myResult = Spacing.createSpacing(0, 0, 0, true, 100, 0);
        }
    } else if (myType1 == GroovyTokenTypes.mLCURLY && myType2 == GroovyTokenTypes.mRCURLY) {
        //empty closure
        createSpaceInCode(false);
    } else if (myType1 == GroovyTokenTypes.mLCURLY && myType2 != GroovyElementTypes.PARAMETERS_LIST && myType2 != GroovyTokenTypes.mCLOSABLE_BLOCK_OP || myType2 == GroovyTokenTypes.mRCURLY) {
        //spaces between statements
        boolean spacesWithinBraces = closure.getParent() instanceof GrStringInjection ? myGroovySettings.SPACE_WITHIN_GSTRING_INJECTION_BRACES : mySettings.SPACE_WITHIN_BRACES;
        createDependentLFSpacing(true, spacesWithinBraces, closure.getTextRange());
    } else if (myType1 == GroovyTokenTypes.mCLOSABLE_BLOCK_OP) {
        myResult = GroovySpacingProcessorBasic.createDependentSpacingForClosure(mySettings, myGroovySettings, closure, true);
    } else if (myType1 == GroovyTokenTypes.mLCURLY && (myType2 == GroovyElementTypes.PARAMETERS_LIST || myType2 == GroovyTokenTypes.mCLOSABLE_BLOCK_OP)) {
        boolean spacesWithinBraces = closure.getParent() instanceof GrStringInjection ? myGroovySettings.SPACE_WITHIN_GSTRING_INJECTION_BRACES : mySettings.SPACE_WITHIN_BRACES;
        createSpaceInCode(spacesWithinBraces);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)

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