Search in sources :

Example 16 with GrReturnStatement

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

the class ConditionalUtils method isReturn.

public static boolean isReturn(GrStatement statement, String value) {
    if (statement == null) {
        return false;
    }
    if (!(statement instanceof GrReturnStatement)) {
        return false;
    }
    final GrReturnStatement returnStatement = (GrReturnStatement) statement;
    final GrExpression returnValue = returnStatement.getReturnValue();
    if (returnValue == null) {
        return false;
    }
    final String returnValueText = returnValue.getText();
    return value.equals(returnValueText);
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)

Example 17 with GrReturnStatement

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

the class GroovyLanguageInjectionSupport method doInject.

private static boolean doInject(@NotNull String languageId, @NotNull PsiElement psiElement, @NotNull PsiLanguageInjectionHost host) {
    final PsiElement target = getTopLevelInjectionTarget(psiElement);
    final PsiElement parent = target.getParent();
    final Project project = psiElement.getProject();
    if (parent instanceof GrReturnStatement) {
        final GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(parent);
        if (owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
            return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) owner.getParent(), -1, host, languageId);
        }
    } else if (parent instanceof GrMethod) {
        return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (GrMethod) parent, -1, host, languageId);
    } else if (parent instanceof GrAnnotationNameValuePair) {
        final PsiReference ref = parent.getReference();
        if (ref != null) {
            final PsiElement resolved = ref.resolve();
            if (resolved instanceof PsiMethod) {
                return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) resolved, -1, host, languageId);
            }
        }
    } else if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall) {
        final PsiMethod method = ((GrMethodCall) parent.getParent()).resolveMethod();
        if (method != null) {
            final int index = GrInjectionUtil.findParameterIndex(target, ((GrMethodCall) parent.getParent()));
            if (index >= 0) {
                return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, method, index, host, languageId);
            }
        }
    } else if (parent instanceof GrAssignmentExpression) {
        final GrExpression expr = ((GrAssignmentExpression) parent).getLValue();
        if (expr instanceof GrReferenceExpression) {
            final PsiElement element = ((GrReferenceExpression) expr).resolve();
            if (element != null) {
                return doInject(languageId, element, host);
            }
        }
    } else {
        if (parent instanceof PsiVariable) {
            Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
            if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) parent, host, languageId, fixer))
                return true;
        } else if (target instanceof PsiVariable && !(target instanceof LightElement)) {
            Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
            if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) target, host, languageId, fixer))
                return true;
        }
    }
    return false;
}
Also used : Processor(com.intellij.util.Processor) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) LightElement(com.intellij.psi.impl.light.LightElement) Project(com.intellij.openapi.project.Project) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 18 with GrReturnStatement

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

the class GrCastFix method findExpressionToCast.

private static GrExpression findExpressionToCast(ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PsiElement parent = element.getParent();
    if (parent instanceof GrVariable) {
        return ((GrVariable) parent).getInitializerGroovy();
    } else if (parent instanceof GrAssignmentExpression) {
        return ((GrAssignmentExpression) parent).getRValue();
    } else if (parent instanceof GrThrowStatement) {
        return ((GrThrowStatement) parent).getException();
    } else if (parent instanceof GrReturnStatement) {
        return ((GrReturnStatement) parent).getReturnValue();
    } else if (element instanceof GrExpression) {
        return (GrExpression) element;
    }
    PsiFile file = element.getContainingFile();
    VirtualFile virtualFile = file.getVirtualFile();
    String url = virtualFile == null ? "" : virtualFile.getPresentableUrl();
    LOG.error("can't find expression to cast at position " + element.getTextRange(), new Attachment(url, file.getText()));
    return null;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiFile(com.intellij.psi.PsiFile) Attachment(com.intellij.openapi.diagnostic.Attachment) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement)

Example 19 with GrReturnStatement

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

the class ConditionalUtils method isReturn.

public static boolean isReturn(GrStatement statement, @NonNls String value) {
    if (statement == null) {
        return false;
    }
    if (!(statement instanceof GrReturnStatement)) {
        return false;
    }
    final GrReturnStatement returnStatement = (GrReturnStatement) statement;
    final GrExpression returnValue = returnStatement.getReturnValue();
    if (returnValue == null) {
        return false;
    }
    final String returnValueText = returnValue.getText();
    return value.equals(returnValueText);
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)

Example 20 with GrReturnStatement

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

the class ExpandBooleanIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrStatement containingStatement = (GrStatement) element;
    if (ExpandBooleanPredicate.isBooleanAssignment(containingStatement)) {
        final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) containingStatement;
        final GrExpression rhs = assignmentExpression.getRValue();
        assert rhs != null;
        final String rhsText = rhs.getText();
        final GrExpression lhs = assignmentExpression.getLValue();
        final String lhsText = lhs.getText();
        @NonNls final String statement = "if(" + rhsText + "){\n" + lhsText + " = true\n}else{\n" + lhsText + " = false\n}";
        PsiImplUtil.replaceStatement(statement, containingStatement);
    } else if (ExpandBooleanPredicate.isBooleanReturn(containingStatement)) {
        final GrReturnStatement returnStatement = (GrReturnStatement) containingStatement;
        final GrExpression returnValue = returnStatement.getReturnValue();
        final String valueText = returnValue.getText();
        @NonNls final String statement = "if(" + valueText + "){\nreturn true\n}else{\nreturn false\n}";
        PsiImplUtil.replaceStatement(statement, containingStatement);
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Aggregations

GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)27 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)16 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)12 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)8 PsiElement (com.intellij.psi.PsiElement)7 Nullable (org.jetbrains.annotations.Nullable)5 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)5 PsiType (com.intellij.psi.PsiType)4 NotNull (org.jetbrains.annotations.NotNull)4 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrThrowStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement)3 GrVariableDeclarationOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner)3 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElementPredicate (org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate)2