Search in sources :

Example 1 with GrTypeCastExpression

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

the class TypeCastSurrounder method surroundExpression.

@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, PsiElement context) {
    GrParenthesizedExpression parenthesized = (GrParenthesizedExpression) GroovyPsiElementFactory.getInstance(expression.getProject()).createExpressionFromText("((Type)a)", context);
    GrTypeCastExpression typeCast = (GrTypeCastExpression) parenthesized.getOperand();
    replaceToOldExpression(typeCast.getOperand(), expression);
    GrTypeElement typeElement = typeCast.getCastTypeElement();
    int endOffset = typeElement.getTextRange().getStartOffset() + expression.getTextRange().getStartOffset();
    parenthesized = (GrParenthesizedExpression) expression.replaceWithExpression(parenthesized, false);
    final GrTypeCastExpression newTypeCast = (GrTypeCastExpression) parenthesized.getOperand();
    final GrTypeElement newTypeElement = newTypeCast.getCastTypeElement();
    newTypeElement.delete();
    return new TextRange(endOffset, endOffset);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrParenthesizedExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrParenthesizedExpression) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) TextRange(com.intellij.openapi.util.TextRange)

Example 2 with GrTypeCastExpression

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

the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.

private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
    PsiElement parent = element.getParent();
    if (parent instanceof GrAnonymousClassDefinition) {
        parent = parent.getParent();
    }
    if (parent instanceof GrNewExpression) {
        return newExpressionProcessor.process((GrNewExpression) parent);
    }
    if (parent instanceof GrTypeElement) {
        final GrTypeElement typeElement = (GrTypeElement) parent;
        final PsiElement grandpa = typeElement.getParent();
        if (grandpa instanceof GrVariableDeclaration) {
            final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
            if (vars.length == 1) {
                final GrVariable variable = vars[0];
                if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
                    return false;
                }
            }
        } else if (grandpa instanceof GrMethod) {
            final GrMethod method = (GrMethod) grandpa;
            if (typeElement == method.getReturnTypeElementGroovy()) {
                ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {

                    @Override
                    public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
                        if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
                            return false;
                        }
                        return true;
                    }
                });
            }
        } else if (grandpa instanceof GrTypeCastExpression) {
            final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
            if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
                return false;
            }
        } else if (grandpa instanceof GrSafeCastExpression) {
            final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
            if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
                return false;
            }
        }
    }
    return true;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GrTypeCastExpression

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

the class GroovyTypeCastSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
    if (e instanceof GrTypeCastExpression) {
        GrTypeCastExpression castExpression = ((GrTypeCastExpression) e);
        GrTypeElement type = castExpression.getCastTypeElement();
        TextRange range = type.getTextRange();
        if (range.contains(cursorOffset)) {
            PsiElement leftParen = castExpression.getLeftParen();
            PsiElement rightParen = castExpression.getRightParen();
            if (leftParen.getTextOffset() < rightParen.getTextOffset()) {
                range = new TextRange(leftParen.getTextRange().getStartOffset(), rightParen.getTextRange().getEndOffset());
                result.add(range);
            }
        }
    }
    return result;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 4 with GrTypeCastExpression

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

the class GrConvertTypeCastToSafeCastIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrTypeCastExpression))
        return;
    GrExpression operand = ((GrTypeCastExpression) element).getOperand();
    GrTypeElement type = ((GrTypeCastExpression) element).getCastTypeElement();
    if (type == null)
        return;
    if (operand == null)
        return;
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    GrExpression safeCast = factory.createExpressionFromText(operand.getText() + " as " + type.getText());
    ((GrTypeCastExpression) element).replaceWithExpression(safeCast, true);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Aggregations

GrTypeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression)4 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)4 TextRange (com.intellij.openapi.util.TextRange)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 PsiElement (com.intellij.psi.PsiElement)1 Nullable (org.jetbrains.annotations.Nullable)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)1 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)1 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)1 GrParenthesizedExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrParenthesizedExpression)1 GrSafeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression)1 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)1 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)1 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)1