Search in sources :

Example 71 with GrClosableBlock

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

the class GrIntroduceParameterDialog method init.

@Override
protected void init() {
    super.init();
    JavaRefactoringSettings settings = JavaRefactoringSettings.getInstance();
    initReplaceFieldsWithGetters(settings);
    myDeclareFinalCheckBox.setSelected(hasFinalModifier());
    myDelegateViaOverloadingMethodCheckBox.setVisible(myInfo.getToSearchFor() != null);
    setTitle(RefactoringBundle.message("introduce.parameter.title"));
    myTable.init(myInfo);
    final GrParameter[] parameters = myInfo.getToReplaceIn().getParameters();
    toRemoveCBs.forEachEntry(new TObjectIntProcedure<JCheckBox>() {

        @Override
        public boolean execute(JCheckBox checkbox, int index) {
            checkbox.setSelected(true);
            final GrParameter param = parameters[index];
            final ParameterInfo pinfo = findParamByOldName(param.getName());
            if (pinfo != null) {
                pinfo.passAsParameter = false;
            }
            return true;
        }
    });
    updateSignature();
    if (myCanIntroduceSimpleParameter) {
        mySignaturePanel.setVisible(false);
        //action to hide signature panel if we have variants to introduce simple parameter
        myTypeComboBox.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                mySignaturePanel.setVisible(myTypeComboBox.isClosureSelected());
                pack();
            }
        });
    }
    final PsiType closureReturnType = inferClosureReturnType();
    if (PsiType.VOID.equals(closureReturnType)) {
        myForceReturnCheckBox.setEnabled(false);
        myForceReturnCheckBox.setSelected(false);
    } else {
        myForceReturnCheckBox.setSelected(isForceReturn());
    }
    if (myInfo.getToReplaceIn() instanceof GrClosableBlock) {
        myDelegateViaOverloadingMethodCheckBox.setEnabled(false);
        myDelegateViaOverloadingMethodCheckBox.setToolTipText("Delegating is not allowed in closure context");
    }
    pack();
}
Also used : JavaRefactoringSettings(com.intellij.refactoring.JavaRefactoringSettings) ItemEvent(java.awt.event.ItemEvent) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) ItemListener(java.awt.event.ItemListener) ParameterInfo(org.jetbrains.plugins.groovy.refactoring.extract.ParameterInfo) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiType(com.intellij.psi.PsiType)

Example 72 with GrClosableBlock

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

the class GroovyIntroduceParameterUtil method addClosureToCall.

@Nullable
public static GrExpression addClosureToCall(PsiElement initializer, GrArgumentList list) {
    if (!(initializer instanceof GrClosableBlock))
        return null;
    final PsiElement parent = list.getParent();
    if (!(parent instanceof GrMethodCallExpression))
        return null;
    PsiElement anchor;
    final GrClosableBlock[] cls = ((GrMethodCallExpression) parent).getClosureArguments();
    if (cls.length > 0) {
        anchor = cls[cls.length - 1];
    } else {
        anchor = list;
    }
    return (GrExpression) parent.addAfter(initializer, anchor);
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 73 with GrClosableBlock

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

the class GroovyIntroduceParameterUtil method removeParamsFromUnresolvedCall.

public static void removeParamsFromUnresolvedCall(GrCall callExpression, PsiParameter[] parameters, TIntArrayList parametersToRemove) {
    final GrExpression[] arguments = callExpression.getExpressionArguments();
    final GrClosableBlock[] closureArguments = callExpression.getClosureArguments();
    final GrNamedArgument[] namedArguments = callExpression.getNamedArguments();
    final boolean hasNamedArgs;
    if (namedArguments.length > 0) {
        if (parameters.length > 0) {
            final PsiType type = parameters[0].getType();
            hasNamedArgs = InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP);
        } else {
            hasNamedArgs = false;
        }
    } else {
        hasNamedArgs = false;
    }
    parametersToRemove.forEachDescending(new TIntProcedure() {

        @Override
        public boolean execute(int paramNum) {
            try {
                if (paramNum == 0 && hasNamedArgs) {
                    for (GrNamedArgument namedArgument : namedArguments) {
                        namedArgument.delete();
                    }
                } else {
                    if (hasNamedArgs)
                        paramNum--;
                    if (paramNum < arguments.length) {
                        arguments[paramNum].delete();
                    } else if (paramNum < arguments.length + closureArguments.length) {
                        closureArguments[paramNum - arguments.length].delete();
                    }
                }
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) TIntProcedure(gnu.trove.TIntProcedure) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 74 with GrClosableBlock

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

the class GroovyIntroduceParameterMethodUsagesProcessor method removeParamsFromUnresolvedCall.

private static void removeParamsFromUnresolvedCall(GrCall callExpression, IntroduceParameterData data) {
    final GrExpression[] arguments = callExpression.getExpressionArguments();
    final GrClosableBlock[] closureArguments = callExpression.getClosureArguments();
    final GrNamedArgument[] namedArguments = callExpression.getNamedArguments();
    final boolean hasNamedArgs;
    if (namedArguments.length > 0) {
        final PsiMethod method = data.getMethodToSearchFor();
        final PsiParameter[] parameters = method.getParameterList().getParameters();
        if (parameters.length > 0) {
            final PsiType type = parameters[0].getType();
            hasNamedArgs = InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP);
        } else {
            hasNamedArgs = false;
        }
    } else {
        hasNamedArgs = false;
    }
    data.getParametersToRemove().forEachDescending(new TIntProcedure() {

        @Override
        public boolean execute(int paramNum) {
            try {
                if (paramNum == 0 && hasNamedArgs) {
                    for (GrNamedArgument namedArgument : namedArguments) {
                        namedArgument.delete();
                    }
                } else {
                    if (hasNamedArgs)
                        paramNum--;
                    if (paramNum < arguments.length) {
                        arguments[paramNum].delete();
                    } else if (paramNum < arguments.length + closureArguments.length) {
                        closureArguments[paramNum - arguments.length].delete();
                    }
                }
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) TIntProcedure(gnu.trove.TIntProcedure) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 75 with GrClosableBlock

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

the class GroovyLineMarkerProvider method getGroovyCategory.

private static int getGroovyCategory(@NotNull PsiElement element, @NotNull CharSequence documentChars) {
    if (element instanceof GrVariableDeclarationImpl) {
        GrVariable[] variables = ((GrVariableDeclarationImpl) element).getVariables();
        if (variables.length == 1 && variables[0] instanceof GrField && variables[0].getInitializerGroovy() instanceof GrClosableBlock) {
            return 2;
        }
    }
    if (element instanceof GrField || element instanceof GrTypeParameter)
        return 1;
    if (element instanceof GrTypeDefinition || element instanceof GrClassInitializer)
        return 2;
    if (element instanceof GrMethod) {
        if (((GrMethod) element).hasModifierProperty(PsiModifier.ABSTRACT) && !(((GrMethod) element).getBlock() != null && GrTraitUtil.isTrait(((GrMethod) element).getContainingClass()))) {
            return 1;
        }
        TextRange textRange = element.getTextRange();
        int start = textRange.getStartOffset();
        int end = Math.min(documentChars.length(), textRange.getEndOffset());
        int crlf = StringUtil.getLineBreakCount(documentChars.subSequence(start, end));
        return crlf == 0 ? 1 : 2;
    }
    return 0;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrClassInitializer(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrClassInitializer) GrTypeParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameter) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrVariableDeclarationImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableDeclarationImpl) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)116 PsiElement (com.intellij.psi.PsiElement)32 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)31 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)26 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)26 Nullable (org.jetbrains.annotations.Nullable)23 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)23 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)17 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)15 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)15 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)14 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)13 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 ArrayList (java.util.ArrayList)11 NotNull (org.jetbrains.annotations.NotNull)10 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)10 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)10 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)10 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9