|
| 1 | +package org.enso.compiler.docs; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Set; |
| 6 | +import org.enso.compiler.core.ir.Module; |
| 7 | +import org.enso.compiler.core.ir.module.scope.Definition; |
| 8 | +import org.enso.compiler.core.ir.module.scope.Definition.Data; |
| 9 | +import org.enso.compiler.core.ir.module.scope.Definition.Type; |
| 10 | +import org.enso.compiler.core.ir.module.scope.definition.Method; |
| 11 | +import scala.jdk.javaapi.CollectionConverters; |
| 12 | + |
| 13 | +/** |
| 14 | + * Bindings are sorted to categories. Every category is sorted alphabetically. |
| 15 | + * Categories are roughly: |
| 16 | + * <ul> |
| 17 | + * <li>Types</li> |
| 18 | + * <li>Instance and static methods on types</li> |
| 19 | + * <li>Module methods</li> |
| 20 | + * <li>Extension and conversion methods</li> |
| 21 | + * </ul> |
| 22 | + */ |
| 23 | +public final class BindingSorter { |
| 24 | + private BindingSorter() {} |
| 25 | + |
| 26 | + /** |
| 27 | + * Returns sorted list of bindings defined on the given {@code moduleIr}. |
| 28 | + */ |
| 29 | + public static List<Definition> sortedBindings(Module moduleIr) { |
| 30 | + var bindings = CollectionConverters.asJava(moduleIr.bindings()); |
| 31 | + var comparator = new BindingComparator(moduleIr); |
| 32 | + return bindings.stream().sorted(comparator).toList(); |
| 33 | + } |
| 34 | + |
| 35 | + public static List<Definition.Data> sortConstructors(List<Definition.Data> constructors) { |
| 36 | + var comparator = new ConstructorComparator(); |
| 37 | + return constructors.stream().sorted(comparator).toList(); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + private static int compareTypes(Type type1, Type type2) { |
| 42 | + return type1.name().name().compareTo(type2.name().name()); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + private static final class BindingComparator implements java.util.Comparator<Definition> { |
| 47 | + private final Module moduleIr; |
| 48 | + private Set<String> typeNames; |
| 49 | + |
| 50 | + private BindingComparator(Module moduleIr) { |
| 51 | + this.moduleIr = moduleIr; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public int compare(Definition def1, Definition def2) { |
| 56 | + return switch (def1) { |
| 57 | + case Method method1 when def2 instanceof Method methods -> |
| 58 | + compareMethods(method1, methods); |
| 59 | + case Type type1 when def2 instanceof Type type2 -> |
| 60 | + compareTypes(type1, type2); |
| 61 | + case Type type1 when def2 instanceof Method method2 -> compareTypeAndMethod(type1, method2); |
| 62 | + case Method method1 when def2 instanceof Type type2 -> |
| 63 | + -compareTypeAndMethod(type2, method1); |
| 64 | + default -> throw new AssertionError("unexpected type " + def1.getClass()); |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + private int compareTypeAndMethod(Type type, Method method) { |
| 69 | + if (method.typeName().isDefined()) { |
| 70 | + if (isExtensionMethod(method)) { |
| 71 | + return -1; |
| 72 | + } |
| 73 | + var typeName = type.name().name(); |
| 74 | + var methodTypeName = method.typeName().get().name(); |
| 75 | + if (typeName.equals(methodTypeName)) { |
| 76 | + return -1; |
| 77 | + } else { |
| 78 | + return typeName.compareTo(methodTypeName); |
| 79 | + } |
| 80 | + } |
| 81 | + return -1; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + private int compareMethods(Method method1, Method method2) { |
| 86 | + return switch (method1) { |
| 87 | + case |
| 88 | + Method.Explicit explicitMethod1 when method2 instanceof Method.Explicit explicitMethod2 -> { |
| 89 | + if (explicitMethod1.isPrivate() != explicitMethod2.isPrivate()) { |
| 90 | + if (explicitMethod1.isPrivate()) { |
| 91 | + yield 1; |
| 92 | + } else { |
| 93 | + yield -1; |
| 94 | + } |
| 95 | + } |
| 96 | + if (isExtensionMethod(explicitMethod1) != isExtensionMethod(explicitMethod2)) { |
| 97 | + if (isExtensionMethod(explicitMethod1)) { |
| 98 | + yield 1; |
| 99 | + } else { |
| 100 | + yield -1; |
| 101 | + } |
| 102 | + } |
| 103 | + var type1 = explicitMethod1.methodReference().typePointer(); |
| 104 | + var type2 = explicitMethod2.methodReference().typePointer(); |
| 105 | + if (type1.isDefined() && type2.isDefined()) { |
| 106 | + // Both methods are instance or static methods - compare by type name |
| 107 | + var typeName1 = type1.get().name(); |
| 108 | + var typeName2 = type2.get().name(); |
| 109 | + if (typeName1.equals(typeName2)) { |
| 110 | + // Methods are defined on the same type |
| 111 | + yield explicitMethod1.methodName().name() |
| 112 | + .compareTo(explicitMethod2.methodName().name()); |
| 113 | + } else { |
| 114 | + yield type1.get().name().compareTo(type2.get().name()); |
| 115 | + } |
| 116 | + } else if (type1.isDefined() && !type2.isDefined()) { |
| 117 | + // Instance or static methods on types have precedence over module methods |
| 118 | + yield -1; |
| 119 | + } else if (!type1.isDefined() && type2.isDefined()) { |
| 120 | + yield 1; |
| 121 | + } |
| 122 | + assert !type1.isDefined() && !type2.isDefined(); |
| 123 | + yield explicitMethod1.methodName().name() |
| 124 | + .compareTo(explicitMethod2.methodName().name()); |
| 125 | + } |
| 126 | + // Comparison of conversion methods is not supported. |
| 127 | + case Method.Conversion conversion1 when method2 instanceof Method.Conversion conversion2 -> |
| 128 | + 0; |
| 129 | + case Method.Explicit explicit when method2 instanceof Method.Conversion -> -1; |
| 130 | + case Method.Conversion conversion when method2 instanceof Method.Explicit -> 1; |
| 131 | + default -> throw new AssertionError( |
| 132 | + "Unexpected type: method1=%s, method2=%s".formatted(method1.getClass(), |
| 133 | + method2.getClass())); |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * An extension method is a method that is defined on a type that is defined outside the |
| 139 | + * current module. |
| 140 | + */ |
| 141 | + private boolean isExtensionMethod(Method method) { |
| 142 | + if (method.typeName().isDefined()) { |
| 143 | + var typeName = method.typeName().get().name(); |
| 144 | + return !typeNamesInModule().contains(typeName); |
| 145 | + } |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + private Set<String> typeNamesInModule() { |
| 150 | + if (typeNames == null) { |
| 151 | + typeNames = new HashSet<>(); |
| 152 | + moduleIr.bindings().foreach(binding -> { |
| 153 | + if (binding instanceof Definition.Type type) { |
| 154 | + typeNames.add(type.name().name()); |
| 155 | + } |
| 156 | + return null; |
| 157 | + }); |
| 158 | + } |
| 159 | + return typeNames; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private static final class ConstructorComparator implements java.util.Comparator<Definition.Data> { |
| 164 | + |
| 165 | + @Override |
| 166 | + public int compare(Data cons1, Data cons2) { |
| 167 | + return cons1.name().name().compareTo(cons2.name().name()); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments