Search in sources :

Example 1 with GrStringContent

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

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

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

the class GroovyGStringSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    final List<TextRange> ranges = ContainerUtil.newArrayList();
    final PsiElement parent = e.getParent();
    if (parent instanceof GrStringContent && parent.getParent() instanceof GrString) {
        TextRange range = getLineTextRange(parent, cursorOffset);
        ranges.add(range);
    } else if (parent instanceof GrString) {
        PsiElement firstChild = parent.getFirstChild();
        PsiElement lastChild = parent.getLastChild();
        if (firstChild.getNode().getElementType() == GroovyTokenTypes.mGSTRING_BEGIN) {
            firstChild = firstChild.getNextSibling();
        }
        if (lastChild.getNode().getElementType() == GroovyTokenTypes.mGSTRING_END) {
            lastChild = lastChild.getPrevSibling();
        }
        if (firstChild != null && lastChild != null) {
            TextRange range = new TextRange(firstChild.getTextOffset(), lastChild.getTextOffset() + lastChild.getTextLength());
            ranges.add(range);
        }
        ranges.add(parent.getTextRange());
    } else if (parent instanceof GrStringInjection) {
        if (e instanceof GrReferenceExpression) {
            List<TextRange> r = new ArrayList<>(2);
            SelectWordUtil.addWordSelection(editor.getSettings().isCamelWords(), editorText, cursorOffset, r);
            for (TextRange textRange : r) {
                if (editorText.charAt(textRange.getStartOffset()) == '$') {
                    textRange = new TextRange(textRange.getStartOffset() + 1, textRange.getEndOffset());
                }
                ranges.add(textRange);
            }
        }
        ranges.add(parent.getTextRange());
        ranges.add(e.getTextRange());
    }
    return ranges;
}
Also used : GrStringContent(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) 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 4 with GrStringContent

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

the class ConvertMultilineStringToSingleLineIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    String quote = element.getText().substring(0, 1);
    StringBuilder buffer = new StringBuilder();
    buffer.append(quote);
    GrExpression old;
    if (element instanceof GrLiteralImpl) {
        appendSimpleStringValue(element, buffer, quote);
        old = (GrExpression) element;
    } else {
        final GrStringImpl gstring = (GrStringImpl) element;
        for (GroovyPsiElement child : gstring.getAllContentParts()) {
            if (child instanceof GrStringContent) {
                appendSimpleStringValue(child, buffer, "\"");
            } else if (child instanceof GrStringInjection) {
                buffer.append(child.getText());
            }
        }
        old = gstring;
    }
    buffer.append(quote);
    try {
        final int offset = editor.getCaretModel().getOffset();
        final TextRange range = old.getTextRange();
        int shift;
        if (range.getStartOffset() == offset) {
            shift = 0;
        } else if (range.getStartOffset() == offset - 1) {
            shift = -1;
        } else if (range.getEndOffset() == offset) {
            shift = -4;
        } else if (range.getEndOffset() == offset + 1) {
            shift = -3;
        } else {
            shift = -2;
        }
        final GrExpression newLiteral = GroovyPsiElementFactory.getInstance(project).createExpressionFromText(buffer.toString());
        old.replaceWithExpression(newLiteral, true);
        if (shift != 0) {
            editor.getCaretModel().moveToOffset(offset + shift);
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : GrStringContent(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrLiteralImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrStringImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrStringImpl) TextRange(com.intellij.openapi.util.TextRange) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)

Example 5 with GrStringContent

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

Aggregations

GrStringContent (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent)5 GrStringInjection (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)5 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiElement (com.intellij.psi.PsiElement)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3 GrLiteralImpl (org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrLiteralImpl)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)2 GrStringImpl (org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.literals.GrStringImpl)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ArrayList (java.util.ArrayList)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1