Search in sources :

Example 6 with GrVariable

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

the class GrSetStrongTypeIntention method getOrCreateTypeElement.

@NotNull
private static TypeInfo getOrCreateTypeElement(@NotNull PsiElement parent, @NotNull PsiElement elementToBuildTemplateOn) {
    GrModifierList modifierList = getModifierList(parent);
    if (modifierList != null && modifierList.hasModifierProperty(GrModifier.DEF) && modifierList.getModifiers().length == 1) {
        PsiElement modifier = modifierList.getModifier(GrModifier.DEF);
        LOG.assertTrue(modifier != null);
        int modifierOffset = modifier.getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
        return new TypeInfo(modifier, modifierOffset);
    } else {
        int nameElementOffset;
        final PsiClassType typeToUse = TypesUtil.createType("Abc", parent);
        if (elementToBuildTemplateOn instanceof GrVariableDeclaration) {
            GrVariableDeclaration decl = (GrVariableDeclaration) elementToBuildTemplateOn;
            decl.setType(typeToUse);
            nameElementOffset = decl.getModifierList().getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
        } else {
            GrVariable var = (GrVariable) parent;
            var.setType(typeToUse);
            nameElementOffset = var.getNameIdentifierGroovy().getTextRange().getStartOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
        }
        return new TypeInfo(getTypeElement(parent), nameElementOffset);
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiClassType(com.intellij.psi.PsiClassType) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiElement(com.intellij.psi.PsiElement) SupertypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SupertypeConstraint) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with GrVariable

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

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

the class GrIntroduceParameterDialog method doOKAction.

@Override
public void doOKAction() {
    saveSettings();
    super.doOKAction();
    final GrParametersOwner toReplaceIn = myInfo.getToReplaceIn();
    final GrExpression expr = GroovyIntroduceParameterUtil.findExpr(myInfo);
    final GrVariable var = GroovyIntroduceParameterUtil.findVar(myInfo);
    final StringPartInfo stringPart = findStringPart();
    if (myTypeComboBox.isClosureSelected() || expr == null && var == null && stringPart == null) {
        GrIntroduceParameterSettings settings = new ExtractClosureHelperImpl(myInfo, getEnteredName(), myDeclareFinalCheckBox.isSelected(), getParametersToRemove(), myDelegateViaOverloadingMethodCheckBox.isSelected(), getReplaceFieldsWithGetter(), myForceReturnCheckBox.isSelected(), false, myTypeComboBox.getSelectedType() == null);
        if (toReplaceIn instanceof GrMethod) {
            invokeRefactoring(new ExtractClosureFromMethodProcessor(settings));
        } else {
            invokeRefactoring(new ExtractClosureFromClosureProcessor(settings));
        }
    } else {
        GrIntroduceParameterSettings settings = new GrIntroduceExpressionSettingsImpl(myInfo, getEnteredName(), myDeclareFinalCheckBox.isSelected(), getParametersToRemove(), myDelegateViaOverloadingMethodCheckBox.isSelected(), getReplaceFieldsWithGetter(), expr, var, myTypeComboBox.getSelectedType(), var != null, true, myForceReturnCheckBox.isSelected());
        if (toReplaceIn instanceof GrMethod) {
            invokeRefactoring(new GrIntroduceParameterProcessor(settings));
        } else {
            invokeRefactoring(new GrIntroduceClosureParameterProcessor(settings));
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ExtractClosureFromClosureProcessor(org.jetbrains.plugins.groovy.refactoring.extract.closure.ExtractClosureFromClosureProcessor) ExtractClosureFromMethodProcessor(org.jetbrains.plugins.groovy.refactoring.extract.closure.ExtractClosureFromMethodProcessor) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) ExtractClosureHelperImpl(org.jetbrains.plugins.groovy.refactoring.extract.closure.ExtractClosureHelperImpl) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)

Example 9 with GrVariable

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

the class GrIntroduceParameterHandler method createContext.

private static GrIntroduceContext createContext(@NotNull IntroduceParameterInfo info, @NotNull Editor editor) {
    GrExpression expr = GroovyIntroduceParameterUtil.findExpr(info);
    GrVariable var = GroovyIntroduceParameterUtil.findVar(info);
    StringPartInfo stringPart = info.getStringPartInfo();
    return new GrIntroduceVariableHandler().getContext(info.getProject(), editor, expr, var, stringPart, info.getToReplaceIn());
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) GrIntroduceVariableHandler(org.jetbrains.plugins.groovy.refactoring.introduce.variable.GrIntroduceVariableHandler)

Example 10 with GrVariable

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

the class GrIntroduceParameterProcessor method createExpressionWrapper.

private static GrExpressionWrapper createExpressionWrapper(GrIntroduceParameterSettings settings) {
    LOG.assertTrue(settings.getToReplaceIn() instanceof GrMethod);
    LOG.assertTrue(settings.getToSearchFor() instanceof PsiMethod);
    final StringPartInfo stringPartInfo = settings.getStringPartInfo();
    GrVariable var = settings.getVar();
    final GrExpression expression = stringPartInfo != null ? stringPartInfo.createLiteralFromSelected() : var != null ? var.getInitializerGroovy() : settings.getExpression();
    return new GrExpressionWrapper(expression);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)

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