Search in sources :

Example 46 with GrParameter

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

the class ResolveUtil method findDuplicate.

public static PsiNamedElement findDuplicate(@NotNull GrVariable variable) {
    if (isScriptField(variable)) {
        final String name = variable.getName();
        final GroovyScriptClass script = (GroovyScriptClass) ((GroovyFile) variable.getContainingFile()).getScriptClass();
        assert script != null;
        List<GrField> duplicates = ContainerUtil.filter(script.getFields(), (GrField f) -> {
            if (!(f instanceof GrScriptField))
                return false;
            if (!name.equals(f.getName()))
                return false;
            if (((GrScriptField) f).getOriginalVariable() == variable)
                return false;
            return true;
        });
        return duplicates.size() > 0 ? duplicates.get(0) : null;
    } else {
        PsiNamedElement duplicate = resolveExistingElement(variable, new DuplicateVariablesProcessor(variable), GrVariable.class);
        final PsiElement context1 = variable.getContext();
        if (duplicate == null && variable instanceof GrParameter && context1 != null) {
            final PsiElement context = context1.getContext();
            if (context instanceof GrClosableBlock || context instanceof GrMethod && !(context.getParent() instanceof GroovyFile) || context instanceof GrTryCatchStatement) {
                duplicate = resolveExistingElement(context.getParent(), new DuplicateVariablesProcessor(variable), GrVariable.class);
            }
        }
        if (duplicate instanceof GrLightParameter && "args".equals(duplicate.getName())) {
            return null;
        } else {
            return duplicate;
        }
    }
}
Also used : GrLightParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrScriptField(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrScriptField) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 47 with GrParameter

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

the class GrIntroduceClosureParameterProcessor method changeSignature.

private static void changeSignature(GrClosableBlock block, GrIntroduceParameterSettings settings) {
    final String name = settings.getName();
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(name, block);
    final GrParameter[] parameters = block.getParameters();
    settings.parametersToRemove().forEachDescending(new TIntProcedure() {

        @Override
        public boolean execute(final int paramNum) {
            try {
                PsiParameter param = parameters[paramNum];
                param.delete();
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
    final PsiType type = settings.getSelectedType();
    final String typeText = type == null ? null : type.getCanonicalText();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    GrParameter parameter = factory.createParameter(name, typeText, block);
    parameter.getModifierList().setModifierProperty(PsiModifier.FINAL, settings.declareFinal());
    final GrParameterList parameterList = block.getParameterList();
    final PsiParameter anchorParameter = GroovyIntroduceParameterUtil.getAnchorParameter(parameterList, block.isVarArgs());
    parameter = (GrParameter) parameterList.addAfter(parameter, anchorParameter);
    if (block.getArrow() == null) {
        final PsiElement arrow = block.addAfter(factory.createClosureFromText("{->}").getArrow().copy(), parameterList);
        final PsiElement child = block.getFirstChild().getNextSibling();
        if (PsiImplUtil.isWhiteSpaceOrNls(child)) {
            final String text = child.getText();
            child.delete();
            block.addAfter(factory.createLineTerminator(text), arrow);
        }
    }
    JavaCodeStyleManager.getInstance(parameter.getProject()).shortenClassReferences(parameter);
    fieldConflictsResolver.fix();
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) TIntProcedure(gnu.trove.TIntProcedure) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) FieldConflictsResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.FieldConflictsResolver) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 48 with GrParameter

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

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

the class GrDynamicImplicitMethod method navigate.

@Override
public void navigate(boolean requestFocus) {
    DynamicToolWindowWrapper.getInstance(getProject()).getToolWindow().activate(() -> {
        DynamicToolWindowWrapper toolWindowWrapper = DynamicToolWindowWrapper.getInstance(getProject());
        final TreeTable treeTable = toolWindowWrapper.getTreeTable();
        final ListTreeTableModelOnColumns model = toolWindowWrapper.getTreeTableModel();
        Object root = model.getRoot();
        if (root == null || !(root instanceof DefaultMutableTreeNode))
            return;
        DefaultMutableTreeNode treeRoot = ((DefaultMutableTreeNode) root);
        DefaultMutableTreeNode desiredNode;
        JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
        final PsiClassType fqClassName = facade.getElementFactory().createTypeByFQClassName(myContainingClassName, ProjectScope.getAllScope(getProject()));
        final PsiClass psiClass = fqClassName.resolve();
        if (psiClass == null)
            return;
        PsiClass trueClass = null;
        DMethodElement methodElement = null;
        final GrParameter[] parameters = getParameters();
        List<String> parameterTypes = new ArrayList<>();
        for (GrParameter parameter : parameters) {
            final String type = parameter.getType().getCanonicalText();
            parameterTypes.add(type);
        }
        for (PsiClass aSuper : PsiUtil.iterateSupers(psiClass, true)) {
            methodElement = DynamicManager.getInstance(getProject()).findConcreteDynamicMethod(aSuper.getQualifiedName(), getName(), ArrayUtil.toStringArray(parameterTypes));
            if (methodElement != null) {
                trueClass = aSuper;
                break;
            }
        }
        if (trueClass == null)
            return;
        final DefaultMutableTreeNode classNode = TreeUtil.findNodeWithObject(treeRoot, new DClassElement(getProject(), trueClass.getQualifiedName()));
        if (classNode == null)
            return;
        desiredNode = TreeUtil.findNodeWithObject(classNode, methodElement);
        if (desiredNode == null)
            return;
        final TreePath path = TreeUtil.getPathFromRoot(desiredNode);
        treeTable.getTree().expandPath(path);
        treeTable.getTree().setSelectionPath(path);
        treeTable.getTree().fireTreeExpanded(path);
        //        ToolWindowManager.getInstance(myProject).getFocusManager().requestFocus(treeTable, true);
        treeTable.revalidate();
        treeTable.repaint();
    }, true);
}
Also used : ListTreeTableModelOnColumns(com.intellij.ui.treeStructure.treetable.ListTreeTableModelOnColumns) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeTable(com.intellij.ui.treeStructure.treetable.TreeTable) ArrayList(java.util.ArrayList) DMethodElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DMethodElement) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) DClassElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement) TreePath(javax.swing.tree.TreePath)

Example 50 with GrParameter

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

the class RemoveUnusedGrParameterFix method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    PsiElement at = file.findElementAt(editor.getCaretModel().getOffset());
    GrParameter parameter = PsiTreeUtil.getParentOfType(at, GrParameter.class);
    if (parameter == null)
        return;
    if (!FileModificationService.getInstance().prepareFileForWrite(parameter.getContainingFile()))
        return;
    GrMethod method = (GrMethod) parameter.getDeclarationScope();
    GrChangeSignatureProcessor processor = new GrChangeSignatureProcessor(parameter.getProject(), createChangeInfo(method, parameter));
    processor.run();
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrChangeSignatureProcessor(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureProcessor) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)99 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)22 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)19 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 ArrayList (java.util.ArrayList)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 TextRange (com.intellij.openapi.util.TextRange)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 Project (com.intellij.openapi.project.Project)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)6