use of com.intellij.codeInsight.generation.PsiFieldMember in project intellij-community by JetBrains.
the class CreateConstructorParameterFromFieldFix method invoke.
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
if (!FileModificationService.getInstance().prepareFileForWrite(file))
return;
PsiMethod[] constructors = myClass.getConstructors();
if (constructors.length == 0) {
final AddDefaultConstructorFix defaultConstructorFix = new AddDefaultConstructorFix(myClass);
ApplicationManager.getApplication().runWriteAction(() -> defaultConstructorFix.invoke(project, editor, file));
constructors = myClass.getConstructors();
}
Arrays.sort(constructors, new Comparator<PsiMethod>() {
@Override
public int compare(PsiMethod c1, PsiMethod c2) {
final PsiMethod cc1 = RefactoringUtil.getChainedConstructor(c1);
final PsiMethod cc2 = RefactoringUtil.getChainedConstructor(c2);
if (cc1 == c2)
return 1;
if (cc2 == c1)
return -1;
if (cc1 == null) {
return cc2 == null ? 0 : compare(c1, cc2);
} else {
return cc2 == null ? compare(cc1, c2) : compare(cc1, cc2);
}
}
});
final List<SmartPsiElementPointer<PsiElement>> cleanupElements = new ArrayList<>();
final List<PsiMethod> filtered = filterConstructorsIfFieldAlreadyAssigned(constructors, getField());
if (filtered.size() > 1) {
final PsiMethodMember[] members = new PsiMethodMember[filtered.size()];
int i = 0;
for (PsiMethod constructor : filtered) {
members[i++] = new PsiMethodMember(constructor);
}
final List<PsiMethodMember> elements;
if (ApplicationManager.getApplication().isUnitTestMode()) {
elements = Arrays.asList(members);
} else {
final MemberChooser<PsiMethodMember> chooser = new MemberChooser<>(members, false, true, project);
chooser.setTitle("Choose Constructors to Add Parameter to");
chooser.show();
elements = chooser.getSelectedElements();
if (elements == null)
return;
}
for (PsiMethodMember member : elements) {
if (!addParameterToConstructor(project, file, editor, member.getElement(), new PsiField[] { getField() }, cleanupElements)) {
break;
}
}
} else if (!filtered.isEmpty()) {
final Collection<SmartPsiElementPointer<PsiField>> fieldsToFix = getFieldsToFix();
try {
final PsiMethod constructor = filtered.get(0);
final LinkedHashSet<PsiField> fields = new LinkedHashSet<>();
getFieldsToFix().add(myField);
for (SmartPsiElementPointer<PsiField> elementPointer : fieldsToFix) {
final PsiField field = elementPointer.getElement();
if (field != null && isAvailable(field) && filterConstructorsIfFieldAlreadyAssigned(new PsiMethod[] { constructor }, field).contains(constructor)) {
fields.add(field);
}
}
if (filtered.size() == constructors.length && fields.size() > 1 && !ApplicationManager.getApplication().isUnitTestMode()) {
PsiFieldMember[] members = new PsiFieldMember[fields.size()];
int i = 0;
for (PsiField field : fields) {
members[i++] = new PsiFieldMember(field);
}
MemberChooser<PsiElementClassMember> chooser = new MemberChooser<>(members, false, true, project);
chooser.setTitle("Choose Fields to Generate Constructor Parameters for");
chooser.show();
if (chooser.getExitCode() != DialogWrapper.OK_EXIT_CODE)
return;
final List<PsiElementClassMember> selectedElements = chooser.getSelectedElements();
if (selectedElements == null)
return;
fields.clear();
for (PsiElementClassMember member : selectedElements) {
fields.add((PsiField) member.getElement());
}
}
addParameterToConstructor(project, file, editor, constructor, filtered.size() == constructors.length ? fields.toArray(new PsiField[fields.size()]) : new PsiField[] { getField() }, cleanupElements);
} finally {
fieldsToFix.clear();
}
}
GlobalInspectionContextBase.cleanupElements(project, null, cleanupElements);
}
use of com.intellij.codeInsight.generation.PsiFieldMember in project intellij-community by JetBrains.
the class AddFieldBreakpointDialog method createCenterPanel.
protected JComponent createCenterPanel() {
myClassChooser.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
public void textChanged(DocumentEvent event) {
updateUI();
}
});
myClassChooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PsiClass currentClass = getSelectedClass();
TreeClassChooser chooser = TreeClassChooserFactory.getInstance(myProject).createAllProjectScopeChooser(DebuggerBundle.message("add.field.breakpoint.dialog.classchooser.title"));
if (currentClass != null) {
PsiFile containingFile = currentClass.getContainingFile();
if (containingFile != null) {
PsiDirectory containingDirectory = containingFile.getContainingDirectory();
if (containingDirectory != null) {
chooser.selectDirectory(containingDirectory);
}
}
}
chooser.showDialog();
PsiClass selectedClass = chooser.getSelected();
if (selectedClass != null) {
myClassChooser.setText(selectedClass.getQualifiedName());
}
}
});
myFieldChooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PsiClass selectedClass = getSelectedClass();
if (selectedClass != null) {
PsiField[] fields = selectedClass.getFields();
MemberChooser<PsiFieldMember> chooser = new MemberChooser<>(ContainerUtil.map2Array(fields, PsiFieldMember.class, PsiFieldMember::new), false, false, myProject);
chooser.setTitle(DebuggerBundle.message("add.field.breakpoint.dialog.field.chooser.title", fields.length));
chooser.setCopyJavadocVisible(false);
TransactionGuard.getInstance().submitTransactionAndWait(() -> chooser.show());
List<PsiFieldMember> selectedElements = chooser.getSelectedElements();
if (selectedElements != null && selectedElements.size() == 1) {
PsiField field = selectedElements.get(0).getElement();
myFieldChooser.setText(field.getName());
}
}
}
});
myFieldChooser.setEnabled(false);
return myPanel;
}
Aggregations