Search in sources :

Example 41 with GrExpression

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

the class GrSplitDeclarationIntention method processSingleVar.

private static void processSingleVar(Project project, GrVariableDeclaration declaration, GrVariable variable) {
    GrExpression initializer = variable.getInitializerGroovy();
    if (initializer != null) {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
        GrExpression assignment = factory.createExpressionFromText(variable.getName() + " = " + initializer.getText());
        initializer.delete();
        declaration = GroovyRefactoringUtil.addBlockIntoParent(declaration);
        declaration.getParent().addAfter(assignment, declaration);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 42 with GrExpression

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

the class IndexingMethodConversionPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrMethodCallExpression)) {
        return false;
    }
    if (ErrorUtil.containsError(element)) {
        return false;
    }
    final GrMethodCallExpression callExpression = (GrMethodCallExpression) element;
    final GrArgumentList argList = callExpression.getArgumentList();
    final GrExpression[] arguments = argList.getExpressionArguments();
    final GrExpression invokedExpression = callExpression.getInvokedExpression();
    if (!(invokedExpression instanceof GrReferenceExpression)) {
        return false;
    }
    final GrReferenceExpression referenceExpression = (GrReferenceExpression) invokedExpression;
    final GrExpression qualifier = referenceExpression.getQualifierExpression();
    if (qualifier == null) {
        return false;
    }
    final IElementType referenceType = referenceExpression.getDotTokenType();
    if (!GroovyTokenTypes.mDOT.equals(referenceType)) {
        return false;
    }
    final String methodName = referenceExpression.getReferenceName();
    if ("getAt".equals(methodName)) {
        return arguments.length == 1;
    }
    if ("get".equals(methodName)) {
        final PsiType qualifierType = qualifier.getType();
        if (!isMap(qualifierType)) {
            return false;
        }
        return arguments.length == 1;
    } else if ("setAt".equals(methodName)) {
        return arguments.length == 2;
    } else if ("put".equals(methodName)) {
        final PsiType qualifierType = qualifier.getType();
        if (!isMap(qualifierType)) {
            return false;
        }
        return arguments.length == 2;
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

Example 43 with GrExpression

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

the class InitialInfo method inferOutputType.

@Nullable
private PsiType inferOutputType(VariableInfo[] outputInfos, GrStatement[] statements, ArrayList<GrStatement> returnStatements, boolean hasReturnValue, StringPartInfo stringPartInfo) {
    if (stringPartInfo != null) {
        return stringPartInfo.getLiteral().getType();
    }
    PsiType outputType = PsiType.VOID;
    if (outputInfos.length > 0) {
        if (outputInfos.length == 1) {
            outputType = outputInfos[0].getType();
        } else {
            outputType = JavaPsiFacade.getElementFactory(myProject).createTypeFromText(CommonClassNames.JAVA_UTIL_LIST, getContext());
        }
    } else if (ExtractUtil.isSingleExpression(statements)) {
        outputType = ((GrExpression) statements[0]).getType();
    } else if (hasReturnValue) {
        assert !returnStatements.isEmpty();
        List<PsiType> types = new ArrayList<>(returnStatements.size());
        for (GrStatement statement : returnStatements) {
            if (statement instanceof GrReturnStatement) {
                GrExpression returnValue = ((GrReturnStatement) statement).getReturnValue();
                if (returnValue != null) {
                    types.add(returnValue.getType());
                }
            } else if (statement instanceof GrExpression) {
                types.add(((GrExpression) statement).getType());
            }
        }
        outputType = TypesUtil.getLeastUpperBoundNullable(types, getContext().getManager());
    }
    return outputType;
}
Also used : ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiType(com.intellij.psi.PsiType) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 44 with GrExpression

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

the class GroovyExtractMethodHandler method renameParameterOccurrences.

private static void renameParameterOccurrences(GrMethod method, ExtractMethodInfoHelper helper) throws IncorrectOperationException {
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return;
    GrStatement[] statements = block.getStatements();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
    for (ParameterInfo info : helper.getParameterInfos()) {
        final String oldName = info.getOriginalName();
        final String newName = info.getName();
        final ArrayList<GrExpression> result = new ArrayList<>();
        if (!oldName.equals(newName)) {
            for (final GrStatement statement : statements) {
                statement.accept(new PsiRecursiveElementVisitor() {

                    @Override
                    public void visitElement(final PsiElement element) {
                        super.visitElement(element);
                        if (element instanceof GrReferenceExpression) {
                            GrReferenceExpression expr = (GrReferenceExpression) element;
                            if (!expr.isQualified() && oldName.equals(expr.getReferenceName())) {
                                result.add(expr);
                            }
                        }
                    }
                });
                for (GrExpression expr : result) {
                    expr.replaceWithExpression(factory.createExpressionFromText(newName), false);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) ParameterInfo(org.jetbrains.plugins.groovy.refactoring.extract.ParameterInfo) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 45 with GrExpression

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

the class GroovyExtractMethodHandler method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable DataContext dataContext) {
    editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
    final SelectionModel model = editor.getSelectionModel();
    if (model.hasSelection()) {
        invokeImpl(project, editor, file, model.getSelectionStart(), model.getSelectionEnd());
    } else {
        final List<GrExpression> expressions = GrIntroduceHandlerBase.collectExpressions(file, editor, editor.getCaretModel().getOffset(), true);
        final Pass<GrExpression> callback = new Callback(project, editor, file);
        if (expressions.size() == 1) {
            callback.pass(expressions.get(0));
        } else if (expressions.isEmpty()) {
            model.selectLineAtCaret();
            invokeImpl(project, editor, file, model.getSelectionStart(), model.getSelectionEnd());
        } else {
            IntroduceTargetChooser.showChooser(editor, expressions, callback, GrIntroduceHandlerBase.GR_EXPRESSION_RENDERER);
        }
    }
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Aggregations

GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)312 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)93 Nullable (org.jetbrains.annotations.Nullable)68 PsiElement (com.intellij.psi.PsiElement)62 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)43 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)43 NotNull (org.jetbrains.annotations.NotNull)37 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)35 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)33 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)29 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)27 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)26 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)26 PsiType (com.intellij.psi.PsiType)24 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)23 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)23 IElementType (com.intellij.psi.tree.IElementType)22 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)18 ArrayList (java.util.ArrayList)16 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)16