Search in sources :

Example 1 with GrLiteral

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

the class GroovyEnterHandler method handleInString.

private static boolean handleInString(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return false;
    final VirtualFile vfile = FileDocumentManager.getInstance().getFile(editor.getDocument());
    assert vfile != null;
    PsiFile file = PsiManager.getInstance(project).findFile(vfile);
    Document document = editor.getDocument();
    String fileText = document.getText();
    if (fileText.length() == caretOffset)
        return false;
    if (!checkStringApplicable(editor, caretOffset))
        return false;
    if (file == null)
        return false;
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final PsiElement stringElement = inferStringPair(file, caretOffset);
    if (stringElement == null)
        return false;
    ASTNode node = stringElement.getNode();
    final IElementType nodeElementType = node.getElementType();
    boolean isInsertIndent = isInsertIndent(caretOffset, stringElement.getTextRange().getStartOffset(), fileText);
    // For simple String literals like 'abc'
    CaretModel caretModel = editor.getCaretModel();
    if (nodeElementType == GroovyTokenTypes.mSTRING_LITERAL) {
        if (isSingleQuoteString(stringElement)) {
            //the case of print '\<caret>'
            if (isSlashBeforeCaret(caretOffset, fileText)) {
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else if (stringElement.getParent() instanceof GrReferenceExpression) {
                TextRange range = stringElement.getTextRange();
                convertEndToMultiline(range.getEndOffset(), document, fileText, '\'');
                document.insertString(range.getStartOffset(), "''");
                caretModel.moveToOffset(caretOffset + 2);
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else {
                EditorModificationUtil.insertStringAtCaret(editor, "'+");
                originalHandler.execute(editor, dataContext);
                EditorModificationUtil.insertStringAtCaret(editor, "'");
                PsiDocumentManager.getInstance(project).commitDocument(document);
                CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
            }
        } else {
            insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        }
        return true;
    }
    if (GSTRING_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && GSTRING_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.GSTRING) {
        PsiElement parent = stringElement.getParent();
        if (nodeElementType == GroovyTokenTypes.mGSTRING_LITERAL) {
            parent = stringElement;
        } else {
            while (parent != null && !(parent instanceof GrLiteral)) {
                parent = parent.getParent();
            }
        }
        if (parent == null)
            return false;
        if (isDoubleQuotedString(parent)) {
            PsiElement exprSibling = stringElement.getNextSibling();
            boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
            if (rightFromDollar)
                caretOffset--;
            TextRange parentRange = parent.getTextRange();
            if (rightFromDollar || parent.getParent() instanceof GrReferenceExpression) {
                convertEndToMultiline(parent.getTextRange().getEndOffset(), document, fileText, '"');
                document.insertString(parentRange.getStartOffset(), "\"\"");
                caretModel.moveToOffset(caretOffset + 2);
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
                if (rightFromDollar) {
                    caretModel.moveCaretRelatively(1, 0, false, false, true);
                }
            } else if (isSlashBeforeCaret(caretOffset, fileText)) {
                EditorModificationUtil.insertStringAtCaret(editor, "\n");
            } else {
                EditorModificationUtil.insertStringAtCaret(editor, "\"+");
                originalHandler.execute(editor, dataContext);
                EditorModificationUtil.insertStringAtCaret(editor, "\"");
                PsiDocumentManager.getInstance(project).commitDocument(document);
                CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
            }
        } else {
            insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        }
        return true;
    }
    if (REGEX_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && REGEX_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.REGEX) {
        PsiElement parent = stringElement.getParent();
        if (nodeElementType == GroovyTokenTypes.mREGEX_LITERAL || nodeElementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
            parent = stringElement;
        } else {
            while (parent != null && !(parent instanceof GrLiteral)) {
                parent = parent.getParent();
            }
        }
        if (parent == null || parent.getLastChild() instanceof PsiErrorElement)
            return false;
        PsiElement exprSibling = stringElement.getNextSibling();
        boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
        if (rightFromDollar) {
            caretModel.moveToOffset(caretOffset - 1);
        }
        insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
        if (rightFromDollar) {
            caretModel.moveCaretRelatively(1, 0, false, false, true);
        }
        return true;
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CaretModel(com.intellij.openapi.editor.CaretModel) TextRange(com.intellij.openapi.util.TextRange) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Document(com.intellij.openapi.editor.Document) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) ASTNode(com.intellij.lang.ASTNode) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 2 with GrLiteral

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

the class GroovyStatementMover method checkAvailable.

@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
    final Project project = file.getProject();
    if (!HandlerUtils.canBeInvoked(editor, project) || !(file instanceof GroovyFileBase))
        return false;
    LineRange range = getLineRangeFromSelection(editor);
    final Document document = editor.getDocument();
    final int offset = document.getLineStartOffset(range.startLine);
    final GrLiteral literal = PsiTreeUtil.findElementOfClassAtOffset(file, offset, GrLiteral.class, false);
    //multiline string
    if (literal != null && literal.textContains('\n'))
        return false;
    final GroovyPsiElement pivot = getElementToMove((GroovyFileBase) file, offset);
    if (pivot == null)
        return false;
    final LineRange pivotRange = getLineRange(pivot);
    range = new LineRange(Math.min(range.startLine, pivotRange.startLine), Math.max(range.endLine, pivotRange.endLine));
    final GroovyPsiElement scope = PsiTreeUtil.getParentOfType(pivot, GrMethod.class, GrTypeDefinitionBody.class, GroovyFileBase.class);
    final boolean stmtLevel = isStatement(pivot);
    boolean topLevel = pivot instanceof GrTypeDefinition && pivot.getParent() instanceof GroovyFileBase;
    final List<LineRange> allRanges = allRanges(scope, stmtLevel, topLevel);
    LineRange prev = null;
    LineRange next = null;
    for (LineRange each : allRanges) {
        if (each.endLine <= range.startLine) {
            prev = each;
        }
        if (each.containsLine(range.startLine)) {
            range = new LineRange(each.startLine, range.endLine);
        }
        if (each.startLine < range.endLine && each.endLine > range.endLine) {
            range = new LineRange(range.startLine, each.endLine);
        }
        if (each.startLine >= range.endLine && next == null) {
            next = each;
        }
    }
    info.toMove = range;
    info.toMove2 = down ? next : prev;
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) Document(com.intellij.openapi.editor.Document) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 3 with GrLiteral

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

the class ExpandBooleanPredicate method isBooleanAssignment.

public static boolean isBooleanAssignment(GrStatement expression) {
    if (!(expression instanceof GrAssignmentExpression)) {
        return false;
    }
    final GrAssignmentExpression assignment = (GrAssignmentExpression) expression;
    final GrExpression rhs = assignment.getRValue();
    if (rhs == null) {
        return false;
    }
    if (rhs instanceof GrLiteral) {
        return false;
    }
    final PsiType assignmentType = rhs.getType();
    if (assignmentType == null) {
        return false;
    }
    return assignmentType.equals(PsiType.BOOLEAN) || assignmentType.equalsToText("java.lang.Boolean");
}
Also used : GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiType(com.intellij.psi.PsiType)

Example 4 with GrLiteral

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

the class ConvertGStringToStringIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrLiteral exp = (GrLiteral) element;
    PsiImplUtil.replaceExpression(convertGStringLiteralToStringLiteral(exp), exp);
}
Also used : GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Example 5 with GrLiteral

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

the class ConvertIntegerToBinaryIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrLiteral exp = (GrLiteral) element;
    @NonNls String textString = exp.getText().replaceAll("_", "");
    final int textLength = textString.length();
    final char lastChar = textString.charAt(textLength - 1);
    final boolean isLong = lastChar == 'l' || lastChar == 'L';
    if (isLong) {
        textString = textString.substring(0, textLength - 1);
    }
    final BigInteger val;
    if (textString.startsWith("0x") || textString.startsWith("0X")) {
        final String rawTextString = textString.substring(2);
        val = new BigInteger(rawTextString, 16);
    } else if (textString.startsWith("0")) {
        final String rawTextString = textString.substring(2);
        val = new BigInteger(rawTextString, 8);
    } else {
        val = new BigInteger(textString, 10);
    }
    String octString = "0b" + val.toString(2);
    if (isLong) {
        octString += 'L';
    }
    PsiImplUtil.replaceExpression(octString, exp);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) BigInteger(java.math.BigInteger) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)

Aggregations

GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)45 Nullable (org.jetbrains.annotations.Nullable)14 PsiElement (com.intellij.psi.PsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)10 NonNls (org.jetbrains.annotations.NonNls)9 PsiType (com.intellij.psi.PsiType)6 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 TextRange (com.intellij.openapi.util.TextRange)4 BigInteger (java.math.BigInteger)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 GrStringInjection (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)4 Document (com.intellij.openapi.editor.Document)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)3 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)3 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)3