Search in sources :

Example 1 with ParameterInfo

use of org.jetbrains.plugins.groovy.refactoring.extract.ParameterInfo in project intellij-community by JetBrains.

the class GroovyExtractMethodHandler method renameParameterOccurrences.

private static void renameParameterOccurrences(GrMethod method, ExtractMethodInfoHelper helper) throws IncorrectOperationException {
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return;
    GrStatement[] statements = block.getStatements();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
    for (ParameterInfo info : helper.getParameterInfos()) {
        final String oldName = info.getOriginalName();
        final String newName = info.getName();
        final ArrayList<GrExpression> result = new ArrayList<>();
        if (!oldName.equals(newName)) {
            for (final GrStatement statement : statements) {
                statement.accept(new PsiRecursiveElementVisitor() {

                    @Override
                    public void visitElement(final PsiElement element) {
                        super.visitElement(element);
                        if (element instanceof GrReferenceExpression) {
                            GrReferenceExpression expr = (GrReferenceExpression) element;
                            if (!expr.isQualified() && oldName.equals(expr.getReferenceName())) {
                                result.add(expr);
                            }
                        }
                    }
                });
                for (GrExpression expr : result) {
                    expr.replaceWithExpression(factory.createExpressionFromText(newName), false);
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) ParameterInfo(org.jetbrains.plugins.groovy.refactoring.extract.ParameterInfo) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)

Example 2 with ParameterInfo

use of org.jetbrains.plugins.groovy.refactoring.extract.ParameterInfo 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)

Aggregations

ParameterInfo (org.jetbrains.plugins.groovy.refactoring.extract.ParameterInfo)2 PsiType (com.intellij.psi.PsiType)1 JavaRefactoringSettings (com.intellij.refactoring.JavaRefactoringSettings)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 ArrayList (java.util.ArrayList)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)1