Search in sources :

Example 1 with GrStringImpl

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

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

Aggregations

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