Search in sources :

Example 6 with GrString

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

the class GroovyLanguageInjectionSupport method getTopLevelInjectionTarget.

@NotNull
public static PsiElement getTopLevelInjectionTarget(@NotNull final PsiElement host) {
    PsiElement target = host;
    PsiElement parent = target.getParent();
    for (; parent != null; target = parent, parent = target.getParent()) {
        if (parent instanceof GrBinaryExpression)
            continue;
        if (parent instanceof GrString)
            continue;
        if (parent instanceof GrParenthesizedExpression)
            continue;
        if (parent instanceof GrConditionalExpression && ((GrConditionalExpression) parent).getCondition() != target)
            continue;
        if (parent instanceof GrAnnotationArrayInitializer)
            continue;
        if (parent instanceof GrListOrMap) {
            parent = parent.getParent();
            continue;
        }
        break;
    }
    return target;
}
Also used : GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrAnnotationArrayInitializer(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArrayInitializer) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with GrString

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

the class GradleEditorValueExtractor method visitGStringExpression.

@Override
public void visitGStringExpression(GrString expression) {
    GroovyPsiElement[] parts = expression.getAllContentParts();
    boolean registeredAssignment = false;
    for (GroovyPsiElement part : parts) {
        if (part instanceof GrStringContent) {
            if (!myInterestedInReferencesOnly && !registeredAssignment && !part.getTextRange().isEmpty()) {
                registeredAssignment = true;
                String text = expression.getText();
                TextRange range = expression.getTextRange();
                if (text.startsWith("'") || text.startsWith("\"")) {
                    text = text.substring(1);
                    range = TextRange.create(range.getStartOffset() + 1, range.getEndOffset());
                }
                if (text.endsWith("'") || text.endsWith("\"")) {
                    text = text.substring(0, text.length() - 1);
                    range = TextRange.create(range.getStartOffset(), range.getEndOffset() - 1);
                }
                myContext.addCachedValue(text, range);
            }
        } else if (part instanceof GrStringInjection) {
            GrExpression injectedExpression = ((GrStringInjection) part).getExpression();
            if (injectedExpression != null) {
                injectedExpression.accept(myVisitor);
            }
        }
    }
}
Also used : GrStringContent(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) TextRange(com.intellij.openapi.util.TextRange) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)

Example 8 with GrString

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

the class GrConcatenation2InjectorAdapter method computeAnchorAndOperands.

@Override
protected Pair<PsiElement, PsiElement[]> computeAnchorAndOperands(@NotNull PsiElement context) {
    PsiElement element = context;
    PsiElement parent = context.getParent();
    while (parent instanceof GrBinaryExpression && ((GrBinaryExpression) parent).getOperationTokenType() == GroovyTokenTypes.mPLUS || parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getOperationToken() == GroovyTokenTypes.mPLUS_ASSIGN || parent instanceof GrConditionalExpression && ((GrConditionalExpression) parent).getCondition() != element || parent instanceof GrTypeCastExpression || parent instanceof GrSafeCastExpression || parent instanceof GrParenthesizedExpression || parent instanceof GrString) {
        element = parent;
        parent = parent.getParent();
    }
    PsiElement[] operands;
    PsiElement anchor;
    if (element instanceof GrBinaryExpression) {
        operands = collectBinaryOperands(((GrBinaryExpression) element));
        anchor = element;
    } else if (element instanceof GrString) {
        operands = collectGStringOperands((GrString) element);
        anchor = element;
    } else if (element instanceof GrAssignmentExpression) {
        GrExpression rvalue = ((GrAssignmentExpression) element).getRValue();
        operands = new PsiElement[] { rvalue == null ? element : rvalue };
        anchor = element;
    } else {
        operands = new PsiElement[] { context };
        anchor = context;
    }
    return Pair.create(anchor, operands);
}
Also used : GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) PsiElement(com.intellij.psi.PsiElement)

Example 9 with GrString

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

the class ConvertToRegexIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrLiteral))
        return;
    StringBuilder buffer = new StringBuilder();
    buffer.append("/");
    if (GrStringUtil.isDollarSlashyString(((GrLiteral) element))) {
        buffer.append(GrStringUtil.removeQuotes(element.getText()));
    } else if (element instanceof GrLiteralImpl) {
        Object value = ((GrLiteralImpl) element).getValue();
        if (value instanceof String) {
            GrStringUtil.escapeSymbolsForSlashyStrings(buffer, (String) value);
        } else {
            String rawText = GrStringUtil.removeQuotes(element.getText());
            unescapeAndAppend(buffer, rawText);
        }
    } else if (element instanceof GrString) {
        for (PsiElement part : ((GrString) element).getAllContentParts()) {
            if (part instanceof GrStringContent) {
                unescapeAndAppend(buffer, part.getText());
            } else if (part instanceof GrStringInjection) {
                buffer.append(part.getText());
            }
        }
    }
    buffer.append("/");
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    GrExpression regex = factory.createExpressionFromText(buffer);
    //don't use replaceWithExpression since it can revert regex to string if regex brakes syntax
    element.replace(regex);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrStringContent(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent) GrLiteralImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) 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 10 with GrString

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

the class RemoveUnnecessaryEscapeCharactersIntention method removeUnnecessaryEscapeSymbols.

private static String removeUnnecessaryEscapeSymbols(final GrLiteral literal) {
    final String text = literal.getText();
    final String quote = GrStringUtil.getStartQuote(text);
    final String value = GrStringUtil.removeQuotes(text);
    final StringBuilder buffer = new StringBuilder();
    buffer.append(quote);
    if (quote == "'") {
        GrStringUtil.escapeAndUnescapeSymbols(value, "", "\"$", buffer);
    } else if (quote == "'''") {
        int position = buffer.length();
        GrStringUtil.escapeAndUnescapeSymbols(value, "", "\"'$n", buffer);
        GrStringUtil.fixAllTripleQuotes(buffer, position);
    } else if (quote == "\"") {
        if (literal instanceof GrString) {
            final ASTNode node = literal.getNode();
            for (ASTNode child = node.getFirstChildNode(); child != null; child = child.getTreeNext()) {
                final IElementType type = child.getElementType();
                if (type == GroovyTokenTypes.mGSTRING_BEGIN || type == GroovyTokenTypes.mGSTRING_END)
                    continue;
                if (type == GroovyElementTypes.GSTRING_INJECTION) {
                    buffer.append(child.getText());
                } else {
                    GrStringUtil.escapeAndUnescapeSymbols(child.getText(), "", "'", buffer);
                }
            }
        } else {
            GrStringUtil.escapeAndUnescapeSymbols(value, "", "'", buffer);
        }
    } else if (quote == "\"\"\"") {
        if (literal instanceof GrString) {
            final ASTNode node = literal.getNode();
            for (ASTNode child = node.getFirstChildNode(); child != null; child = child.getTreeNext()) {
                final IElementType type = child.getElementType();
                if (type == GroovyTokenTypes.mGSTRING_BEGIN || type == GroovyTokenTypes.mGSTRING_END)
                    continue;
                if (type == GroovyElementTypes.GSTRING_INJECTION) {
                    buffer.append(child.getText());
                } else {
                    final int position = buffer.length();
                    GrStringUtil.escapeAndUnescapeSymbols(child.getText(), "", "\"'n", buffer);
                    GrStringUtil.fixAllTripleDoubleQuotes(buffer, position);
                }
            }
        } else {
            final int position = buffer.length();
            GrStringUtil.escapeAndUnescapeSymbols(value, "", "\"'n", buffer);
            GrStringUtil.fixAllTripleDoubleQuotes(buffer, position);
        }
    } else {
        return text;
    }
    buffer.append(quote);
    return buffer.toString();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) ASTNode(com.intellij.lang.ASTNode) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)

Aggregations

GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)15 PsiElement (com.intellij.psi.PsiElement)6 GrStringInjection (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)6 TextRange (com.intellij.openapi.util.TextRange)4 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)4 ASTNode (com.intellij.lang.ASTNode)3 IElementType (com.intellij.psi.tree.IElementType)3 Nullable (org.jetbrains.annotations.Nullable)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrStringContent (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)2 GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)2 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)2 GradleResolvedVariable (com.android.tools.idea.gradle.dsl.parser.GradleResolvedVariable)1 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)1 NamedFoldingDescriptor (com.intellij.lang.folding.NamedFoldingDescriptor)1