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();
}
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));
}
}
}
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;
}
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()]);
}
}
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());
}
Aggregations