Search in sources :

Example 36 with GrVariableDeclaration

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

the class GroovyStatementMover method allRanges.

private List<LineRange> allRanges(final GroovyPsiElement scope, final boolean stmtLevel, final boolean topLevel) {
    final ArrayList<LineRange> result = new ArrayList<>();
    scope.accept(new PsiRecursiveElementVisitor() {

        int lastStart = -1;

        private void addRange(int endLine) {
            if (lastStart >= 0) {
                result.add(new LineRange(lastStart, endLine));
            }
            lastStart = endLine;
        }

        @Override
        public void visitElement(PsiElement element) {
            if (stmtLevel && element instanceof GrCodeBlock) {
                final PsiElement lBrace = ((GrCodeBlock) element).getLBrace();
                if (nlsAfter(lBrace)) {
                    assert lBrace != null;
                    addRange(new LineRange(lBrace).endLine);
                }
                addChildRanges(((GrCodeBlock) element).getStatements());
                final PsiElement rBrace = ((GrCodeBlock) element).getRBrace();
                if (nlsAfter(rBrace)) {
                    assert rBrace != null;
                    final int endLine = new LineRange(rBrace).endLine;
                    if (lastStart >= 0) {
                        for (int i = lastStart + 1; i < endLine; i++) {
                            addRange(i);
                        }
                    }
                }
            } else if (stmtLevel && element instanceof GrCaseSection) {
                final GrCaseLabel[] allLabels = ((GrCaseSection) element).getCaseLabels();
                final GrCaseLabel label = allLabels[0];
                if (nlsAfter(label)) {
                    addRange(new LineRange(label).endLine);
                }
                addChildRanges(((GrCaseSection) element).getStatements());
            } else if (element instanceof GroovyFileBase) {
                addChildRanges(((GroovyFileBase) element).getTopStatements());
            } else if (!stmtLevel && !topLevel && element instanceof GrTypeDefinitionBody) {
                addChildRanges(((GrTypeDefinitionBody) element).getMemberDeclarations());
            } else {
                super.visitElement(element);
            }
        }

        private boolean shouldDigInside(GroovyPsiElement statement) {
            if (stmtLevel && (statement instanceof GrMethod || statement instanceof GrTypeDefinition)) {
                return false;
            }
            if (statement instanceof GrVariableDeclaration && !stmtLevel) {
                return false;
            }
            return true;
        }

        private void addChildRanges(GroovyPsiElement[] statements) {
            for (int i = 0; i < statements.length; i++) {
                GroovyPsiElement statement = statements[i];
                if (nlsAfter(statement)) {
                    final LineRange range = getLineRange(statement);
                    if ((i == 0 || isStatement(statements[i - 1])) && isStatement(statement)) {
                        for (int j = lastStart; j < range.startLine; j++) {
                            addRange(j + 1);
                        }
                    }
                    lastStart = range.startLine;
                    if (shouldDigInside(statement)) {
                        statement.accept(this);
                    }
                    addRange(range.endLine);
                }
            }
        }
    });
    return result;
}
Also used : PsiRecursiveElementVisitor(com.intellij.psi.PsiRecursiveElementVisitor) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 37 with GrVariableDeclaration

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

the class GroovyMethodInliner method inlineReferenceImpl.

@Nullable
static RangeMarker inlineReferenceImpl(@NotNull GrCallExpression call, @NotNull GrMethod method, boolean resultOfCallExplicitlyUsed, boolean isTailMethodCall, @Nullable Editor editor) {
    try {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(call.getProject());
        final Project project = call.getProject();
        // Variable declaration for qualifier expression
        GrVariableDeclaration qualifierDeclaration = null;
        GrReferenceExpression innerQualifier = null;
        GrExpression qualifier = null;
        if (call instanceof GrMethodCallExpression && ((GrMethodCallExpression) call).getInvokedExpression() != null) {
            GrExpression invoked = ((GrMethodCallExpression) call).getInvokedExpression();
            if (invoked instanceof GrReferenceExpression && ((GrReferenceExpression) invoked).getQualifierExpression() != null) {
                qualifier = ((GrReferenceExpression) invoked).getQualifierExpression();
                if (PsiUtil.isSuperReference(qualifier)) {
                    qualifier = null;
                } else if (!GroovyInlineMethodUtil.isSimpleReference(qualifier)) {
                    String qualName = generateQualifierName(call, method, project, qualifier);
                    qualifier = (GrExpression) PsiUtil.skipParentheses(qualifier, false);
                    qualifierDeclaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, qualifier, null, qualName);
                    innerQualifier = (GrReferenceExpression) factory.createExpressionFromText(qualName);
                } else {
                    innerQualifier = (GrReferenceExpression) qualifier;
                }
            }
        }
        GrMethod _method = prepareNewMethod(call, method, qualifier);
        GrExpression result = getAloneResultExpression(_method);
        if (result != null) {
            GrExpression expression = call.replaceWithExpression(result, false);
            TextRange range = expression.getTextRange();
            return editor != null ? editor.getDocument().createRangeMarker(range.getStartOffset(), range.getEndOffset(), true) : null;
        }
        GrMethod newMethod = prepareNewMethod(call, method, innerQualifier);
        String resultName = InlineMethodConflictSolver.suggestNewName("result", newMethod, call);
        // Add variable for method result
        Collection<GrStatement> returnStatements = ControlFlowUtils.collectReturns(newMethod.getBlock());
        final int returnCount = returnStatements.size();
        PsiType methodType = method.getInferredReturnType();
        GrOpenBlock body = newMethod.getBlock();
        assert body != null;
        GrExpression replaced;
        if (resultOfCallExplicitlyUsed && !isTailMethodCall) {
            GrExpression resultExpr = null;
            if (PsiType.VOID.equals(methodType)) {
                resultExpr = factory.createExpressionFromText("null");
            } else if (returnCount == 1) {
                final GrExpression returnExpression = ControlFlowUtils.extractReturnExpression(returnStatements.iterator().next());
                if (returnExpression != null) {
                    resultExpr = factory.createExpressionFromText(returnExpression.getText());
                }
            } else if (returnCount > 1) {
                resultExpr = factory.createExpressionFromText(resultName);
            }
            if (resultExpr == null) {
                resultExpr = factory.createExpressionFromText("null");
            }
            replaced = call.replaceWithExpression(resultExpr, false);
        } else {
            replaced = call;
        }
        // Calculate anchor to insert before
        GrExpression enclosingExpr = GroovyRefactoringUtil.addBlockIntoParent(replaced);
        GrVariableDeclarationOwner owner = PsiTreeUtil.getParentOfType(enclosingExpr, GrVariableDeclarationOwner.class);
        assert owner != null;
        PsiElement element = enclosingExpr;
        while (element != null && element.getParent() != owner) {
            element = element.getParent();
        }
        assert element != null && element instanceof GrStatement;
        GrStatement anchor = (GrStatement) element;
        if (!resultOfCallExplicitlyUsed) {
            assert anchor == enclosingExpr;
        }
        // add qualifier reference declaration
        if (qualifierDeclaration != null) {
            owner.addVariableDeclarationBefore(qualifierDeclaration, anchor);
        }
        // Process method return statements
        if (returnCount > 1 && !PsiType.VOID.equals(methodType) && !isTailMethodCall) {
            PsiType type = methodType != null && methodType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) ? null : methodType;
            GrVariableDeclaration resultDecl = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, resultName);
            GrStatement statement = ((GrStatementOwner) owner).addStatementBefore(resultDecl, anchor);
            JavaCodeStyleManager.getInstance(statement.getProject()).shortenClassReferences(statement);
            // Replace all return statements with assignments to 'result' variable
            for (GrStatement returnStatement : returnStatements) {
                GrExpression value = ControlFlowUtils.extractReturnExpression(returnStatement);
                if (value != null) {
                    GrExpression assignment = factory.createExpressionFromText(resultName + " = " + value.getText());
                    returnStatement.replaceWithStatement(assignment);
                } else {
                    returnStatement.replaceWithStatement(factory.createExpressionFromText(resultName + " = null"));
                }
            }
        }
        if (!isTailMethodCall && resultOfCallExplicitlyUsed && returnCount == 1) {
            returnStatements.iterator().next().removeStatement();
        } else if (!isTailMethodCall && (PsiType.VOID.equals(methodType) || returnCount == 1)) {
            for (GrStatement returnStatement : returnStatements) {
                if (returnStatement instanceof GrReturnStatement) {
                    final GrExpression returnValue = ((GrReturnStatement) returnStatement).getReturnValue();
                    if (returnValue != null && GroovyRefactoringUtil.hasSideEffect(returnValue)) {
                        returnStatement.replaceWithStatement(returnValue);
                        continue;
                    }
                } else if (GroovyRefactoringUtil.hasSideEffect(returnStatement)) {
                    continue;
                }
                returnStatement.removeStatement();
            }
        }
        // Add all method statements
        GrStatement[] statements = body.getStatements();
        for (GrStatement statement : statements) {
            ((GrStatementOwner) owner).addStatementBefore(statement, anchor);
        }
        if (resultOfCallExplicitlyUsed && !isTailMethodCall) {
            TextRange range = replaced.getTextRange();
            RangeMarker marker = editor != null ? editor.getDocument().createRangeMarker(range.getStartOffset(), range.getEndOffset(), true) : null;
            reformatOwner(owner);
            return marker;
        } else {
            GrStatement stmt;
            if (isTailMethodCall && enclosingExpr.getParent() instanceof GrReturnStatement) {
                stmt = (GrReturnStatement) enclosingExpr.getParent();
            } else {
                stmt = enclosingExpr;
            }
            stmt.removeStatement();
            reformatOwner(owner);
            return null;
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrVariableDeclarationOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrVariableDeclarationOwner) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with GrVariableDeclaration

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

the class GrIntroduceConstantProcessor method addDeclaration.

protected GrVariableDeclaration addDeclaration(PsiClass targetClass) {
    GrVariableDeclaration declaration = createField(targetClass);
    final GrVariableDeclaration added;
    if (targetClass instanceof GrEnumTypeDefinition) {
        final GrEnumConstantList enumConstants = ((GrEnumTypeDefinition) targetClass).getEnumConstantList();
        added = (GrVariableDeclaration) targetClass.addAfter(declaration, enumConstants);
    } else {
        added = ((GrVariableDeclaration) targetClass.add(declaration));
    }
    JavaCodeStyleManager.getInstance(added.getProject()).shortenClassReferences(added);
    return added;
}
Also used : GrEnumConstantList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstantList) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrEnumTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrEnumTypeDefinition)

Example 39 with GrVariableDeclaration

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

the class GroovyCreateFieldFromUsageHelper method setupTemplateImpl.

@Override
public Template setupTemplateImpl(PsiField f, Object expectedTypes, PsiClass targetClass, Editor editor, PsiElement context, boolean createConstantField, PsiSubstitutor substitutor) {
    GrVariableDeclaration fieldDecl = (GrVariableDeclaration) f.getParent();
    GrField field = (GrField) fieldDecl.getVariables()[0];
    TemplateBuilderImpl builder = new TemplateBuilderImpl(fieldDecl);
    Project project = context.getProject();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    if (expectedTypes instanceof TypeConstraint[]) {
        GrTypeElement typeElement = fieldDecl.getTypeElementGroovy();
        assert typeElement != null;
        ChooseTypeExpression expr = new ChooseTypeExpression((TypeConstraint[]) expectedTypes, PsiManager.getInstance(project), typeElement.getResolveScope());
        builder.replaceElement(typeElement, expr);
    } else if (expectedTypes instanceof ExpectedTypeInfo[]) {
        new GuessTypeParameters(factory).setupTypeElement(field.getTypeElement(), (ExpectedTypeInfo[]) expectedTypes, substitutor, builder, context, targetClass);
    }
    if (createConstantField) {
        field.setInitializerGroovy(factory.createExpressionFromText("0", null));
        builder.replaceElement(field.getInitializerGroovy(), new EmptyExpression());
    }
    fieldDecl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(fieldDecl);
    Template template = builder.buildTemplate();
    TextRange range = fieldDecl.getTextRange();
    editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    if (expectedTypes instanceof ExpectedTypeInfo[]) {
        if (((ExpectedTypeInfo[]) expectedTypes).length > 1)
            template.setToShortenLongNames(false);
    }
    return template;
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GuessTypeParameters(com.intellij.codeInsight.daemon.impl.quickfix.GuessTypeParameters) TextRange(com.intellij.openapi.util.TextRange) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) EmptyExpression(com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression) Template(com.intellij.codeInsight.template.Template) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) Project(com.intellij.openapi.project.Project) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)

Example 40 with GrVariableDeclaration

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

the class CreateLocalVariableFromUsageFix method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiFile file = element.getContainingFile();
    PsiClassType type = JavaPsiFacade.getInstance(project).getElementFactory().createTypeByFQClassName("Object", GlobalSearchScope.allScope(project));
    GrVariableDeclaration decl = GroovyPsiElementFactory.getInstance(project).createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, myRefExpression.getReferenceName());
    int offset = myRefExpression.getTextRange().getStartOffset();
    GrStatement anchor = findAnchor(file, offset);
    TypeConstraint[] constraints = GroovyExpectedTypesProvider.calculateTypeConstraints(myRefExpression);
    if (myRefExpression.equals(anchor)) {
        decl = myRefExpression.replaceWithStatement(decl);
    } else {
        decl = myOwner.addVariableDeclarationBefore(decl, anchor);
    }
    GrTypeElement typeElement = decl.getTypeElementGroovy();
    assert typeElement != null;
    ChooseTypeExpression expr = new ChooseTypeExpression(constraints, PsiManager.getInstance(project), typeElement.getResolveScope());
    TemplateBuilderImpl builder = new TemplateBuilderImpl(decl);
    builder.replaceElement(typeElement, expr);
    decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
    Template template = builder.buildTemplate();
    Editor newEditor = positionCursor(project, myOwner.getContainingFile(), decl);
    TextRange range = decl.getTextRange();
    newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    TemplateManager manager = TemplateManager.getInstance(project);
    manager.startTemplate(newEditor, template);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) TextRange(com.intellij.openapi.util.TextRange) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Template(com.intellij.codeInsight.template.Template) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Editor(com.intellij.openapi.editor.Editor)

Aggregations

GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)43 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)19 PsiElement (com.intellij.psi.PsiElement)15 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)9 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 Nullable (org.jetbrains.annotations.Nullable)7 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)7 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)7 NotNull (org.jetbrains.annotations.NotNull)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5 TextRange (com.intellij.openapi.util.TextRange)4 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)4 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)4 Template (com.intellij.codeInsight.template.Template)3