mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
sort members in toString chooser from super to child (IDEA-93029)
This commit is contained in:
@@ -24,6 +24,7 @@ import org.jetbrains.java.generate.config.FilterPattern;
|
||||
import org.jetbrains.java.generate.psi.PsiAdapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -60,9 +61,27 @@ public class GenerateToStringUtils {
|
||||
FilterPattern pattern) {
|
||||
if (log.isDebugEnabled()) log.debug("Filtering fields using the pattern: " + pattern);
|
||||
List<PsiField> availableFields = new ArrayList<>();
|
||||
collectAvailableFields(clazz, includeSuperClass, pattern, availableFields, new HashSet<>());
|
||||
return availableFields.toArray(new PsiField[availableFields.size()]);
|
||||
}
|
||||
|
||||
private static void collectAvailableFields(PsiClass clazz,
|
||||
boolean includeSuperClass,
|
||||
FilterPattern pattern,
|
||||
List<PsiField> availableFields,
|
||||
HashSet<PsiClass> visited) {
|
||||
|
||||
int sortElements = GenerateToStringContext.getConfig().getSortElements();
|
||||
|
||||
if (includeSuperClass && sortElements == 3) {
|
||||
PsiClass superClass = clazz.getSuperClass();
|
||||
if (superClass != null && visited.add(superClass)) {
|
||||
collectAvailableFields(superClass, true, pattern, availableFields, visited);
|
||||
}
|
||||
}
|
||||
|
||||
// performs til filtering process
|
||||
PsiField[] fields = includeSuperClass ? clazz.getAllFields() : clazz.getFields();
|
||||
PsiField[] fields = includeSuperClass && sortElements != 3 ? clazz.getAllFields() : clazz.getFields();
|
||||
for (PsiField field : fields) {
|
||||
if (!JavaResolveUtil.isAccessible(field, field.getContainingClass(), field.getModifierList(), clazz, null, null)) {
|
||||
continue;
|
||||
@@ -72,8 +91,6 @@ public class GenerateToStringUtils {
|
||||
availableFields.add(field);
|
||||
}
|
||||
}
|
||||
|
||||
return availableFields.toArray(new PsiField[availableFields.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,7 +110,21 @@ public class GenerateToStringUtils {
|
||||
public static PsiMethod[] filterAvailableMethods(PsiClass clazz, @NotNull FilterPattern pattern) {
|
||||
if (log.isDebugEnabled()) log.debug("Filtering methods using the pattern: " + pattern);
|
||||
List<PsiMethod> availableMethods = new ArrayList<>();
|
||||
PsiMethod[] methods = clazz.getAllMethods();
|
||||
collectAvailableMethods(clazz, pattern, availableMethods, new HashSet<>());
|
||||
return availableMethods.toArray(new PsiMethod[availableMethods.size()]);
|
||||
}
|
||||
|
||||
private static void collectAvailableMethods(PsiClass clazz, @NotNull FilterPattern pattern,
|
||||
List<PsiMethod> availableMethods,
|
||||
HashSet<PsiClass> visited) {
|
||||
int sortElements = GenerateToStringContext.getConfig().getSortElements();
|
||||
if (sortElements == 3) {
|
||||
PsiClass superClass = clazz.getSuperClass();
|
||||
if (superClass != null && visited.add(superClass)) {
|
||||
collectAvailableMethods(superClass, pattern, availableMethods, visited);
|
||||
}
|
||||
}
|
||||
PsiMethod[] methods = sortElements != 3 ? clazz.getAllMethods() : clazz.getMethods();
|
||||
for (PsiMethod method : methods) {
|
||||
// the method should be a getter
|
||||
if (!PsiAdapter.isGetterMethod(method)) {
|
||||
@@ -133,6 +164,5 @@ public class GenerateToStringUtils {
|
||||
log.debug("Adding the method " + methodName + " as there is not a field for this getter");
|
||||
availableMethods.add(method);
|
||||
}
|
||||
return availableMethods.toArray(new PsiMethod[availableMethods.size()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,7 +192,7 @@ public class GenerationUtil {
|
||||
logger.debug("Velocity Context - adding members (fields and methods)");
|
||||
List<Element> elements = ElementUtils.getOnlyAsFieldAndMethodElements(selectedMembers, selectedNotNullMembers, useAccessors);
|
||||
// sort elements if enabled and not using chooser dialog
|
||||
if (sortElements != 0) {
|
||||
if (sortElements != 0 && sortElements < 3) {
|
||||
Collections.sort(elements, new ElementComparator(sortElements));
|
||||
}
|
||||
vc.put("members", elements);
|
||||
|
||||
@@ -117,6 +117,7 @@ public class ConfigUI extends JPanel {
|
||||
panel.add(innerPanel);
|
||||
sortElementsComboBox.addItem("Ascending");
|
||||
sortElementsComboBox.addItem("Descending");
|
||||
sortElementsComboBox.addItem("Super class members first");
|
||||
constraint.gridwidth = GridBagConstraints.REMAINDER;
|
||||
constraint.fill = GridBagConstraints.BOTH;
|
||||
constraint.gridx = 0;
|
||||
@@ -246,12 +247,15 @@ public class ConfigUI extends JPanel {
|
||||
enableMethods.setSelected(config.isEnableMethods());
|
||||
moveCaretToMethod.setSelected(config.isJumpToMethod());
|
||||
|
||||
sortElements.setSelected(config.getSortElements() != 0);
|
||||
int sort = config.getSortElements();
|
||||
sortElements.setSelected(sort != 0);
|
||||
sortElementsComboBox.setEnabled(sortElements.isSelected());
|
||||
if (config.getSortElements() == 0 || config.getSortElements() == 1) {
|
||||
if (sort == 0 || sort == 1) {
|
||||
sortElementsComboBox.setSelectedIndex(0);
|
||||
} else if (config.getSortElements() == 2) {
|
||||
} else if (sort == 2) {
|
||||
sortElementsComboBox.setSelectedIndex(1);
|
||||
} else if (sort == 3) {
|
||||
sortElementsComboBox.setSelectedIndex(2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,10 +302,8 @@ public class ConfigUI extends JPanel {
|
||||
|
||||
if (!sortElements.isSelected()) {
|
||||
config.setSortElements(0);
|
||||
} else if (sortElementsComboBox.getSelectedIndex() == 0) {
|
||||
config.setSortElements(1); // selected index of 0 is ascending
|
||||
} else {
|
||||
config.setSortElements(2); // selected index of 0 is ascending
|
||||
config.setSortElements(sortElementsComboBox.getSelectedIndex() + 1);
|
||||
}
|
||||
|
||||
return config;
|
||||
|
||||
Reference in New Issue
Block a user