Search in sources :

Example 6 with GrVariableDeclaration

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

the class GrSetStrongTypeIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            PsiElement parent = element.getParent();
            PsiElement pparent;
            if (isNameIdentifierOfVariable(element, parent) || isModifierListOfVar(element, parent)) {
                pparent = parent.getParent();
            } else if (isModifierListOfVarDecl(element, parent)) {
                pparent = parent;
            } else {
                return false;
            }
            if (pparent instanceof GrVariableDeclaration) {
                if (((GrVariableDeclaration) pparent).getTypeElementGroovy() != null)
                    return false;
                GrVariable[] variables = ((GrVariableDeclaration) pparent).getVariables();
                for (GrVariable variable : variables) {
                    if (isVarDeclaredWithInitializer(variable))
                        return true;
                }
            } else if (pparent instanceof GrForInClause) {
                final GrVariable variable = ((GrForInClause) pparent).getDeclaredVariable();
                return variable != null && variable.getTypeElementGroovy() == null && PsiUtil.extractIteratedType((GrForInClause) pparent) != null;
            } else if (parent instanceof GrParameter && pparent instanceof GrParameterList) {
                return ((GrParameter) parent).getTypeElementGroovy() == null && getClosureParameterType((PsiParameter) parent) != null;
            } else {
                final GrVariable variable = (GrVariable) parent;
                return variable.getTypeElementGroovy() == null && isVarDeclaredWithInitializer(variable);
            }
            return false;
        }

        private boolean isModifierListOfVarDecl(PsiElement element, PsiElement parent) {
            return parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).getModifierList() == element;
        }

        private boolean isModifierListOfVar(PsiElement element, PsiElement parent) {
            return parent instanceof GrVariable && ((GrVariable) parent).getModifierList() == element;
        }

        private boolean isNameIdentifierOfVariable(PsiElement element, PsiElement parent) {
            return parent instanceof GrVariable && ((GrVariable) parent).getTypeElementGroovy() == null && element == ((GrVariable) parent).getNameIdentifierGroovy();
        }
    };
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrForInClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with GrVariableDeclaration

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

the class MoveGroovyMemberHandler method doMove.

@Override
@NotNull
public PsiMember doMove(@NotNull MoveMembersOptions options, @NotNull PsiMember member, PsiElement anchor, @NotNull PsiClass targetClass) {
    GroovyChangeContextUtil.encodeContextInfo(member);
    final PsiDocComment docComment;
    if (member instanceof PsiDocCommentOwner) {
        docComment = ((PsiDocCommentOwner) member).getDocComment();
    } else {
        docComment = null;
    }
    PsiMember moved;
    if (options.makeEnumConstant() && member instanceof GrVariable && EnumConstantsUtil.isSuitableForEnumConstant(((PsiVariable) member).getType(), targetClass)) {
        final GrEnumConstant prototype = createEnumConstant(member.getName(), ((GrVariable) member).getInitializerGroovy(), member.getProject());
        moved = (PsiMember) addEnumConstant(targetClass, prototype, anchor);
        member.delete();
    } else if (member instanceof GrEnumConstant) {
        moved = (PsiMember) addEnumConstant(targetClass, (GrEnumConstant) member, null);
    } else if (member instanceof GrField) {
        if (anchor != null)
            anchor = anchor.getParent();
        final GrVariableDeclaration parent = (GrVariableDeclaration) member.getParent();
        GrVariableDeclaration movedDeclaration = (GrVariableDeclaration) targetClass.addAfter(parent, anchor);
        int number = ArrayUtil.find(parent.getMembers(), member);
        final GrMember[] members = movedDeclaration.getMembers();
        for (int i = 0; i < number; i++) {
            members[i].delete();
        }
        for (int i = number + 1; i < members.length; i++) {
            members[i].delete();
        }
        if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
            //might need to make modifiers explicit, see IDEADEV-11416
            final PsiModifierList list = movedDeclaration.getModifierList();
            VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
            list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
            list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
        }
        moved = movedDeclaration.getMembers()[0];
    } else if (member instanceof GrMethod) {
        moved = (PsiMember) targetClass.addAfter(member, anchor);
        if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
            //might need to make modifiers explicit, see IDEADEV-11416
            final PsiModifierList list = moved.getModifierList();
            assert list != null;
            list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
            list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
            VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
        }
    } else {
        moved = (PsiMember) targetClass.addAfter(member, anchor);
    }
    if (docComment != null) {
        PsiElement insertedDocComment = targetClass.addBefore(docComment, moved);
        PsiElement prevSibling = insertedDocComment.getPrevSibling();
        addLineFeedIfNeeded(prevSibling);
        docComment.delete();
    }
    member.delete();
    return moved;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrEnumConstant(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with GrVariableDeclaration

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

the class ConvertSimpleGetterToPropertyIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    GrMethod method = (GrMethod) element.getParent();
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return;
    GrStatement statement = block.getStatements()[0];
    GrExpression value;
    if (statement instanceof GrReturnStatement) {
        value = ((GrReturnStatement) statement).getReturnValue();
    } else {
        value = (GrExpression) statement;
    }
    String fieldName = GroovyPropertyUtils.getPropertyNameByGetter(method);
    if (fieldName == null)
        return;
    PsiClass aClass = method.getContainingClass();
    if (aClass == null)
        return;
    List<String> modifiers = ContainerUtil.newArrayList();
    for (String modifier : MODIFIERS_TO_CHECK) {
        if (method.hasModifierProperty(modifier))
            modifiers.add(modifier);
    }
    modifiers.add(PsiModifier.FINAL);
    GrTypeElement returnTypeElement = method.getReturnTypeElementGroovy();
    PsiType returnType = returnTypeElement == null ? null : returnTypeElement.getType();
    GrVariableDeclaration declaration = GroovyPsiElementFactory.getInstance(project).createFieldDeclaration(ArrayUtil.toStringArray(modifiers), fieldName, value, returnType);
    PsiElement replaced = method.replace(declaration);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) PsiType(com.intellij.psi.PsiType)

Example 9 with GrVariableDeclaration

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

the class GrSplitDeclarationIntention method getElementPredicate.

@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
    return new PsiElementPredicate() {

        @Override
        public boolean satisfiedBy(PsiElement element) {
            if (element instanceof GrVariableDeclaration) {
                GrVariableDeclaration decl = (GrVariableDeclaration) element;
                GrVariable[] variables = decl.getVariables();
                if (variables.length > 1 && PsiUtil.isLocalVariable(variables[0])) {
                    if (!decl.isTuple() || decl.getTupleInitializer() instanceof GrListOrMap) {
                        myText = GroovyIntentionsBundle.message("split.into.separate.declaration");
                    } else {
                        myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
                    }
                    return true;
                } else if (variables.length == 1 && PsiUtil.isLocalVariable(variables[0]) && variables[0].getInitializerGroovy() != null) {
                    myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
                    return true;
                }
            }
            return false;
        }
    };
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with GrVariableDeclaration

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

the class GrIntroduceConstantProcessor method run.

@Nullable
public GrField run() {
    final PsiClass targetClass = settings.getTargetClass();
    if (targetClass == null)
        return null;
    if (checkErrors(targetClass)) {
        return null;
    }
    final GrVariableDeclaration declaration = addDeclaration(targetClass);
    final GrField field = (GrField) declaration.getVariables()[0];
    GrVariable localVar = GrIntroduceHandlerBase.resolveLocalVar(context);
    if (localVar != null) {
        assert localVar.getInitializerGroovy() != null : "initializer should exist: " + localVar.getText();
        GrIntroduceHandlerBase.deleteLocalVar(localVar);
        if (settings.replaceAllOccurrences()) {
            processOccurrences(field);
        } else {
            replaceOccurrence(field, localVar.getInitializerGroovy(), isEscalateVisibility());
        }
    } else if (context.getStringPart() != null) {
        final GrExpression ref = context.getStringPart().replaceLiteralWithConcatenation(field.getName());
        final PsiElement element = replaceOccurrence(field, ref, isEscalateVisibility());
        updateCaretPosition(element);
    } else if (context.getExpression() != null) {
        if (settings.replaceAllOccurrences()) {
            processOccurrences(field);
        } else {
            replaceOccurrence(field, context.getExpression(), isEscalateVisibility());
        }
    }
    return field;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

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