use of com.intellij.codeInsight.generation.PsiElementClassMember 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.PsiElementClassMember in project intellij-community by JetBrains.
the class GenerateToStringActionHandlerImpl method doExecuteAction.
private static void doExecuteAction(@NotNull final Project project, @NotNull final PsiClass clazz, final Editor editor) {
if (!FileModificationService.getInstance().preparePsiElementsForWrite(clazz)) {
return;
}
logger.debug("+++ doExecuteAction - START +++");
if (logger.isDebugEnabled()) {
logger.debug("Current project " + project.getName());
}
final PsiElementClassMember[] dialogMembers = buildMembersToShow(clazz);
final MemberChooserHeaderPanel header = new MemberChooserHeaderPanel(clazz);
logger.debug("Displaying member chooser dialog");
final MemberChooser<PsiElementClassMember> chooser = new MemberChooser<PsiElementClassMember>(dialogMembers, true, true, project, PsiUtil.isLanguageLevel5OrHigher(clazz), header) {
@Nullable
@Override
protected String getHelpId() {
return "editing.altInsert.tostring";
}
};
//noinspection DialogTitleCapitalization
chooser.setTitle("Generate toString()");
chooser.setCopyJavadocVisible(false);
chooser.selectElements(getPreselection(clazz, dialogMembers));
header.setChooser(chooser);
chooser.show();
if (DialogWrapper.OK_EXIT_CODE == chooser.getExitCode()) {
Collection<PsiMember> selectedMembers = GenerationUtil.convertClassMembersToPsiMembers(chooser.getSelectedElements());
final TemplateResource template = header.getSelectedTemplate();
ToStringTemplatesManager.getInstance().setDefaultTemplate(template);
if (template.isValidTemplate()) {
final GenerateToStringWorker worker = new GenerateToStringWorker(clazz, editor, chooser.isInsertOverrideAnnotation());
// decide what to do if the method already exists
ConflictResolutionPolicy resolutionPolicy = worker.exitsMethodDialog(template);
WriteAction.run(() -> {
try {
worker.execute(selectedMembers, template, resolutionPolicy);
} catch (Exception e) {
GenerationUtil.handleException(project, e);
}
});
} else {
HintManager.getInstance().showErrorHint(editor, "toString() template '" + template.getFileName() + "' is invalid");
}
}
logger.debug("+++ doExecuteAction - END +++");
}
Aggregations