Search in sources :

Example 1 with GrParametersOwner

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

the class MethodOrClosureScopeChooser method create.

/**
   * @param callback is invoked if any scope was chosen. The first arg is this scope and the second arg is a psielement to search for (super method of chosen method or
   *                 variable if the scope is a closure)
   */
public static JBPopup create(List<? extends GrParametersOwner> scopes, final Editor editor, final JBPopupOwner popupRef, final PairFunction<GrParametersOwner, PsiElement, Object> callback) {
    final JPanel panel = new JPanel(new BorderLayout());
    final JCheckBox superMethod = new JCheckBox(USE_SUPER_METHOD_OF, true);
    superMethod.setMnemonic('U');
    panel.add(superMethod, BorderLayout.SOUTH);
    final JBList list = new JBList(scopes.toArray());
    list.setVisibleRowCount(5);
    list.setCellRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            final String text;
            if (value instanceof PsiMethod) {
                final PsiMethod method = (PsiMethod) value;
                text = PsiFormatUtil.formatMethod(method, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE);
                final int flags = Iconable.ICON_FLAG_VISIBILITY;
                final Icon icon = method.getIcon(flags);
                if (icon != null)
                    setIcon(icon);
            } else {
                LOG.assertTrue(value instanceof GrClosableBlock);
                setIcon(JetgroovyIcons.Groovy.Groovy_16x16);
                text = "{...}";
            }
            setText(text);
            return this;
        }
    });
    list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setSelectedIndex(0);
    final List<RangeHighlighter> highlighters = new ArrayList<>();
    final TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    list.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(final ListSelectionEvent e) {
            final GrParametersOwner selectedMethod = (GrParametersOwner) list.getSelectedValue();
            if (selectedMethod == null)
                return;
            dropHighlighters(highlighters);
            updateView(selectedMethod, editor, attributes, highlighters, superMethod);
        }
    });
    updateView(scopes.get(0), editor, attributes, highlighters, superMethod);
    final JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(list);
    scrollPane.setBorder(null);
    panel.add(scrollPane, BorderLayout.CENTER);
    final List<Pair<ActionListener, KeyStroke>> keyboardActions = Collections.singletonList(Pair.<ActionListener, KeyStroke>create(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final GrParametersOwner ToSearchIn = (GrParametersOwner) list.getSelectedValue();
            final JBPopup popup = popupRef.get();
            if (popup != null && popup.isVisible()) {
                popup.cancel();
            }
            final PsiElement toSearchFor;
            if (ToSearchIn instanceof GrMethod) {
                final GrMethod method = (GrMethod) ToSearchIn;
                toSearchFor = superMethod.isEnabled() && superMethod.isSelected() ? method.findDeepestSuperMethod() : method;
            } else {
                toSearchFor = superMethod.isEnabled() && superMethod.isSelected() ? ToSearchIn.getParent() : null;
            }
            IdeFocusManager.findInstance().doWhenFocusSettlesDown(() -> callback.fun(ToSearchIn, toSearchFor), ModalityState.current());
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)));
    return JBPopupFactory.getInstance().createComponentPopupBuilder(panel, list).setTitle("Introduce parameter to").setMovable(false).setResizable(false).setRequestFocus(true).setKeyboardActions(keyboardActions).addListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
            dropHighlighters(highlighters);
        }
    }).createPopup();
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) ListSelectionEvent(javax.swing.event.ListSelectionEvent) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) JBPopup(com.intellij.openapi.ui.popup.JBPopup) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) ListSelectionListener(javax.swing.event.ListSelectionListener) ActionListener(java.awt.event.ActionListener) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) JBList(com.intellij.ui.components.JBList)

Example 2 with GrParametersOwner

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

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

the class GrIntroduceParameterHandler method findScopes.

@NotNull
private static List<GrParametersOwner> findScopes(@NotNull InitialInfo initialInfo) {
    PsiElement place = initialInfo.getContext();
    final List<GrParametersOwner> scopes = new ArrayList<>();
    while (true) {
        final GrParametersOwner parent = PsiTreeUtil.getParentOfType(place, GrMethod.class, GrClosableBlock.class);
        if (parent == null)
            break;
        scopes.add(parent);
        place = parent;
    }
    return scopes;
}
Also used : GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GrParametersOwner

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

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

the class GrInplaceParameterIntroducer method runRefactoring.

@Override
protected GrVariable runRefactoring(GrIntroduceContext context, GrIntroduceParameterSettings settings, boolean processUsages) {
    GrExpressionWrapper wrapper = createExpressionWrapper(context);
    if (processUsages) {
        GrIntroduceExpressionSettingsImpl patchedSettings = new GrIntroduceExpressionSettingsImpl(settings, settings.getName(), settings.declareFinal(), settings.parametersToRemove(), settings.generateDelegate(), settings.replaceFieldsWithGetters(), context.getExpression(), context.getVar(), settings.getSelectedType(), context.getVar() != null || settings.replaceAllOccurrences(), context.getVar() != null, settings.isForceReturn());
        GrIntroduceParameterProcessor processor = new GrIntroduceParameterProcessor(patchedSettings, wrapper);
        processor.run();
    } else {
        WriteAction.run(() -> new GrIntroduceParameterProcessor(settings, wrapper).performRefactoring(UsageInfo.EMPTY_ARRAY));
    }
    GrParametersOwner owner = settings.getToReplaceIn();
    return ArrayUtil.getLastElement(owner.getParameters());
}
Also used : GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner)

Aggregations

GrParametersOwner (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner)7 ArrayList (java.util.ArrayList)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 PsiElement (com.intellij.psi.PsiElement)2 MultiMap (com.intellij.util.containers.MultiMap)2 NotNull (org.jetbrains.annotations.NotNull)2 GrNamedElement (org.jetbrains.plugins.groovy.lang.psi.GrNamedElement)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Editor (com.intellij.openapi.editor.Editor)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 JBPopup (com.intellij.openapi.ui.popup.JBPopup)1