Search in sources :

Example 11 with GrVariable

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

the class GrVariableInplaceRenameHandler method isAvailable.

@Override
protected boolean isAvailable(PsiElement element, Editor editor, PsiFile file) {
    if (!editor.getSettings().isVariableInplaceRenameEnabled())
        return false;
    if (!(element instanceof GrVariable))
        return false;
    if (element instanceof GrField)
        return false;
    final SearchScope scope = element.getUseScope();
    if (!(scope instanceof LocalSearchScope))
        return false;
    final PsiElement[] scopeElements = ((LocalSearchScope) scope).getScope();
    return scopeElements.length == 1 || scopeElements.length == 2 && (scopeElements[0] instanceof GrDocComment ^ scopeElements[1] instanceof GrDocComment);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiElement(com.intellij.psi.PsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)

Example 12 with GrVariable

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable 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 13 with GrVariable

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

the class GroovyIntroduceParameterUtil method findVar.

@Nullable
static GrVariable findVar(IntroduceParameterInfo info) {
    GrVariable variable = info.getVar();
    if (variable != null)
        return variable;
    final GrStatement[] statements = info.getStatements();
    if (statements.length != 1)
        return null;
    return GrIntroduceHandlerBase.findVariable(statements[0]);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with GrVariable

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

the class GroovyIntroduceParameterUtil method getOccurrences.

static PsiElement[] getOccurrences(GrIntroduceParameterSettings settings) {
    final GrParametersOwner scope = settings.getToReplaceIn();
    final GrExpression expression = settings.getExpression();
    if (expression != null) {
        final PsiElement expr = PsiUtil.skipParentheses(expression, false);
        if (expr == null)
            return PsiElement.EMPTY_ARRAY;
        final PsiElement[] occurrences = GroovyRefactoringUtil.getExpressionOccurrences(expr, scope);
        if (occurrences == null || occurrences.length == 0) {
            throw new GrRefactoringError(GroovyRefactoringBundle.message("no.occurrences.found"));
        }
        return occurrences;
    } else {
        final GrVariable var = settings.getVar();
        LOG.assertTrue(var != null);
        final List<PsiElement> list = Collections.synchronizedList(new ArrayList<PsiElement>());
        ReferencesSearch.search(var, new LocalSearchScope(scope)).forEach(psiReference -> {
            final PsiElement element = psiReference.getElement();
            if (element != null) {
                list.add(element);
            }
            return true;
        });
        return list.toArray(new PsiElement[list.size()]);
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) GrRefactoringError(org.jetbrains.plugins.groovy.refactoring.GrRefactoringError) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 15 with GrVariable

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

the class GrInplaceVariableIntroducer method addAdditionalVariables.

@Override
protected void addAdditionalVariables(TemplateBuilderImpl builder) {
    GrVariable variable = getVariable();
    assert variable != null && variable.getInitializerGroovy() != null;
    final PsiType initializerType = variable.getInitializerGroovy().getType();
    TypeConstraint[] constraints = initializerType != null && !initializerType.equals(PsiType.NULL) ? new SupertypeConstraint[] { SupertypeConstraint.create(initializerType) } : TypeConstraint.EMPTY_ARRAY;
    ChooseTypeExpression typeExpression = new ChooseTypeExpression(constraints, variable.getManager(), variable.getResolveScope(), true, GroovyApplicationSettings.getInstance().INTRODUCE_LOCAL_SELECT_DEF);
    PsiElement element = getTypeELementOrDef(variable);
    if (element == null)
        return;
    builder.replaceElement(element, "Variable_type", typeExpression, true, true);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) PsiElement(com.intellij.psi.PsiElement) PsiType(com.intellij.psi.PsiType)

Aggregations

GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)88 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)34 PsiElement (com.intellij.psi.PsiElement)24 Nullable (org.jetbrains.annotations.Nullable)20 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)16 NotNull (org.jetbrains.annotations.NotNull)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)14 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)11 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)10 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)10 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)8 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)8 PsiType (com.intellij.psi.PsiType)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)7 TextRange (com.intellij.openapi.util.TextRange)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 ASTNode (com.intellij.lang.ASTNode)5