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