use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock 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.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method findUsagesForLocal.
private static Collection<PsiReference> findUsagesForLocal(GrClosableBlock initializer, final GrVariable var) {
final Instruction[] flow = ControlFlowUtils.findControlFlowOwner(initializer).getControlFlow();
final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, var);
Instruction writeInstr = null;
final PsiElement parent = initializer.getParent();
if (parent instanceof GrVariable) {
writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == var);
} else if (parent instanceof GrAssignmentExpression) {
final GrReferenceExpression refExpr = (GrReferenceExpression) ((GrAssignmentExpression) parent).getLValue();
final Instruction instruction = ContainerUtil.find(flow, instruction1 -> instruction1.getElement() == refExpr);
LOG.assertTrue(instruction != null);
final BitSet prev = writes.get(instruction.num());
if (prev.cardinality() == 1) {
writeInstr = flow[prev.nextSetBit(0)];
}
}
LOG.assertTrue(writeInstr != null);
Collection<PsiReference> result = new ArrayList<>();
for (Instruction instruction : flow) {
if (!(instruction instanceof ReadWriteVariableInstruction))
continue;
if (((ReadWriteVariableInstruction) instruction).isWrite())
continue;
final PsiElement element = instruction.getElement();
if (element instanceof GrVariable && element != var)
continue;
if (!(element instanceof GrReferenceExpression))
continue;
final GrReferenceExpression ref = (GrReferenceExpression) element;
if (ref.isQualified() || ref.resolve() != var)
continue;
final BitSet prev = writes.get(instruction.num());
if (prev.cardinality() == 1 && prev.get(writeInstr.num())) {
result.add(ref);
}
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method insertDeclaration.
private GrVariableDeclaration insertDeclaration(GrVariable original, GrVariableDeclaration declaration) {
if (original instanceof GrField) {
final PsiClass containingClass = ((GrField) original).getContainingClass();
LOG.assertTrue(containingClass != null);
return (GrVariableDeclaration) containingClass.addBefore(declaration, original.getParent());
}
final GrStatementOwner block;
if (original instanceof PsiParameter) {
final PsiElement container = original.getParent().getParent();
if (container instanceof GrMethod) {
block = ((GrMethod) container).getBlock();
} else if (container instanceof GrClosableBlock) {
block = (GrCodeBlock) container;
} else if (container instanceof GrForStatement) {
final GrStatement body = ((GrForStatement) container).getBody();
if (body instanceof GrBlockStatement) {
block = ((GrBlockStatement) body).getBlock();
} else {
GrBlockStatement blockStatement = myFactory.createBlockStatement();
LOG.assertTrue(blockStatement != null);
if (body != null) {
blockStatement.getBlock().addStatementBefore((GrStatement) body.copy(), null);
blockStatement = (GrBlockStatement) body.replace(blockStatement);
} else {
blockStatement = (GrBlockStatement) container.add(blockStatement);
}
block = blockStatement.getBlock();
}
} else {
throw new IncorrectOperationException();
}
LOG.assertTrue(block != null);
return (GrVariableDeclaration) block.addStatementBefore(declaration, null);
}
PsiElement parent = original.getParent();
LOG.assertTrue(parent instanceof GrVariableDeclaration);
final PsiElement pparent = parent.getParent();
if (pparent instanceof GrIfStatement) {
if (((GrIfStatement) pparent).getThenBranch() == parent) {
block = ((GrIfStatement) pparent).replaceThenBranch(myFactory.createBlockStatement()).getBlock();
} else {
block = ((GrIfStatement) pparent).replaceElseBranch(myFactory.createBlockStatement()).getBlock();
}
parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
} else if (pparent instanceof GrLoopStatement) {
block = ((GrLoopStatement) pparent).replaceBody(myFactory.createBlockStatement()).getBlock();
parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
} else {
LOG.assertTrue(pparent instanceof GrStatementOwner);
block = (GrStatementOwner) pparent;
}
return (GrVariableDeclaration) block.addStatementBefore(declaration, (GrStatement) parent);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method generateDelegateClosure.
private GrClosableBlock generateDelegateClosure(GrClosableBlock originalClosure, GrVariable anchor, String newName) {
GrClosableBlock result;
if (originalClosure.hasParametersSection()) {
result = myFactory.createClosureFromText("{->}", anchor);
final GrParameterList parameterList = (GrParameterList) originalClosure.getParameterList().copy();
result.getParameterList().replace(parameterList);
} else {
result = myFactory.createClosureFromText("{}", anchor);
}
StringBuilder call = new StringBuilder();
call.append(newName).append('(');
final GrParameter[] parameters = result.getParameters();
for (int i = 0; i < parameters.length; i++) {
if (!mySettings.parametersToRemove().contains(i)) {
call.append(parameters[i].getName()).append(", ");
}
}
call.append(myParameterInitializer.getText());
call.append(")");
final GrStatement statement = myFactory.createStatementFromText(call.toString());
result.addStatementBefore(statement, null);
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class GrVariableInplaceRenamer method renameSynthetic.
@Override
protected void renameSynthetic(String newName) {
PsiNamedElement elementToRename = getVariable();
if (elementToRename instanceof ClosureSyntheticParameter && !"it".equals(newName)) {
final GrClosableBlock closure = ((ClosureSyntheticParameter) elementToRename).getClosure();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
final PsiType type = ((ClosureSyntheticParameter) elementToRename).getTypeGroovy();
final GrParameter newParam = factory.createParameter(newName, TypesUtil.unboxPrimitiveTypeWrapper(type));
final GrParameter added = closure.addParameter(newParam);
JavaCodeStyleManager.getInstance(added.getProject()).shortenClassReferences(added);
}
}
Aggregations