mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
LookupElement classifier interface simplified
This commit is contained in:
+2
-3
@@ -34,6 +34,7 @@ import com.intellij.psi.WeighingService;
|
||||
import com.intellij.psi.statistics.StatisticsInfo;
|
||||
import com.intellij.psi.statistics.StatisticsManager;
|
||||
import com.intellij.util.Alarm;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import gnu.trove.THashMap;
|
||||
import gnu.trove.TObjectHashingStrategy;
|
||||
@@ -147,9 +148,7 @@ public class CompletionLookupArranger extends LookupArranger {
|
||||
|
||||
final List<LookupElement> byRelevance = new ArrayList<LookupElement>();
|
||||
for (CompletionSorterImpl sorter : myClassifiers.keySet()) {
|
||||
for (List<LookupElement> elements : myClassifiers.get(sorter).classify((List<LookupElement>)inputBySorter.get(sorter))) {
|
||||
byRelevance.addAll(elements);
|
||||
}
|
||||
ContainerUtil.addAll(byRelevance, myClassifiers.get(sorter).classify((List<LookupElement>)inputBySorter.get(sorter)));
|
||||
}
|
||||
|
||||
LinkedHashSet<LookupElement> model = new LinkedHashSet<LookupElement>();
|
||||
|
||||
+15
-27
@@ -73,44 +73,32 @@ class LiftShorterItemsClassifier extends Classifier<LookupElement> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<List<LookupElement>> classify(List<LookupElement> source) {
|
||||
public Iterable<LookupElement> classify(List<LookupElement> source) {
|
||||
return liftShorterElements(source, new THashSet<LookupElement>(TObjectHashingStrategy.IDENTITY));
|
||||
}
|
||||
|
||||
private Iterable<List<LookupElement>> liftShorterElements(List<LookupElement> source, THashSet<LookupElement> lifted) {
|
||||
private List<LookupElement> liftShorterElements(List<LookupElement> source, THashSet<LookupElement> lifted) {
|
||||
final Set<LookupElement> srcSet = new THashSet<LookupElement>(source, TObjectHashingStrategy.IDENTITY);
|
||||
final Iterable<List<LookupElement>> classified = myNext.classify(source);
|
||||
final Set<LookupElement> processed = new THashSet<LookupElement>(TObjectHashingStrategy.IDENTITY);
|
||||
|
||||
final ArrayList<List<LookupElement>> result = new ArrayList<List<LookupElement>>();
|
||||
for (List<LookupElement> list : classified) {
|
||||
final ArrayList<LookupElement> group = new ArrayList<LookupElement>();
|
||||
for (LookupElement element : list) {
|
||||
assert srcSet.contains(element) : myNext;
|
||||
if (processed.add(element)) {
|
||||
for (String prefix : getSortedPrefixes(element)) {
|
||||
List<LookupElement> shorter = new SmartList<LookupElement>();
|
||||
for (LookupElement shorterElement : myElements.get(prefix)) {
|
||||
if (srcSet.contains(shorterElement) && processed.add(shorterElement)) {
|
||||
shorter.add(shorterElement);
|
||||
}
|
||||
final List<LookupElement> result = new ArrayList<LookupElement>();
|
||||
for (LookupElement element : myNext.classify(source)) {
|
||||
assert srcSet.contains(element) : myNext;
|
||||
if (processed.add(element)) {
|
||||
for (String prefix : getSortedPrefixes(element)) {
|
||||
List<LookupElement> shorter = new SmartList<LookupElement>();
|
||||
for (LookupElement shorterElement : myElements.get(prefix)) {
|
||||
if (srcSet.contains(shorterElement) && processed.add(shorterElement)) {
|
||||
shorter.add(shorterElement);
|
||||
}
|
||||
|
||||
lifted.addAll(shorter);
|
||||
|
||||
final Iterable<List<LookupElement>> shorterClassified = myNext.classify(shorter);
|
||||
if (group.isEmpty()) {
|
||||
ContainerUtil.addAll(result, shorterClassified);
|
||||
} else {
|
||||
group.addAll(ContainerUtil.flatten(shorterClassified));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
group.add(element);
|
||||
lifted.addAll(shorter);
|
||||
|
||||
ContainerUtil.addAll(result, myNext.classify(shorter));
|
||||
}
|
||||
result.add(element);
|
||||
}
|
||||
result.add(group);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import java.util.List;
|
||||
public abstract class Classifier<T> {
|
||||
public abstract void addElement(T t);
|
||||
|
||||
public abstract Iterable<List<T>> classify(List<T> source);
|
||||
public abstract Iterable<T> classify(List<T> source);
|
||||
|
||||
public abstract void describeItems(LinkedHashMap<T, StringBuilder> map);
|
||||
|
||||
|
||||
@@ -40,8 +40,8 @@ public abstract class ClassifierFactory<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<List<T>> classify(List<T> source) {
|
||||
return Collections.singleton(source);
|
||||
public Iterable<T> classify(List<T> source) {
|
||||
return source;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,29 +50,6 @@ public abstract class ClassifierFactory<T> {
|
||||
};
|
||||
}
|
||||
|
||||
public static <T> Classifier<T> sortingListClassifier(final Comparator<T> comparator) {
|
||||
return new Classifier<T>() {
|
||||
@Override
|
||||
public void addElement(T t) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<List<T>> classify(List<T> source) {
|
||||
final List<T> copy = new ArrayList<T>(source);
|
||||
Collections.sort(source, comparator);
|
||||
return Collections.singletonList(copy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeItems(LinkedHashMap<T, StringBuilder> map) {
|
||||
final String str = "sorted by " + comparator;
|
||||
for (StringBuilder builder : map.values()) {
|
||||
builder.append(str);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
||||
@@ -55,8 +55,8 @@ public abstract class ComparingClassifier<T> extends Classifier<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<List<T>> classify(List<T> source) {
|
||||
List<List<T>> result = new ArrayList<List<T>>();
|
||||
public Iterable<T> classify(List<T> source) {
|
||||
List<T> result = new ArrayList<T>();
|
||||
for (List<T> list : groupByWeights(source).values()) {
|
||||
ContainerUtil.addAll(result, myNext.classify(list));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user