simpler lookup element comparables

This commit is contained in:
peter
2011-02-09 16:17:01 +01:00
parent 6a1666584d
commit 397977cfe8
5 changed files with 12 additions and 46 deletions
@@ -53,7 +53,6 @@ public abstract class CompletionSortingTestCase extends LightFixtureCompletionTe
protected static void refreshSorting(final LookupImpl lookup) {
for (final LookupElement item : lookup.getItems()) {
item.putUserData(CompletionLookupArranger.WEIGHT, null);
item.putUserData(CompletionLookupArranger.RELEVANCE_KEY, null);
}
lookup.setSelectionTouched(false);
@@ -39,7 +39,6 @@ public class CompletionLookupArranger extends LookupArranger {
private static final String SELECTED = "selected";
static final String IGNORED = "ignored";
private final CompletionLocation myLocation;
public static final Key<Comparable[]> WEIGHT = Key.create("WEIGHT");
private final Map<LookupElement, Comparable> mySortingWeights = new THashMap<LookupElement, Comparable>(TObjectHashingStrategy.IDENTITY);
public CompletionLookupArranger(final CompletionParameters parameters) {
@@ -95,16 +94,6 @@ public class CompletionLookupArranger extends LookupArranger {
return info.getContext() + "###" + info.getValue();
}
public Comparable[] getRelevanceWeight(final LookupElement item) {
if (item.getUserData(WEIGHT) != null) return item.getUserData(WEIGHT);
final Comparable[] result = new Comparable[]{WeighingService.weigh(CompletionService.RELEVANCE_KEY, item, myLocation)};
item.putUserData(WEIGHT, result);
return result;
}
public Comparable getSortingWeight(final LookupElement item) {
final Comparable comparable = mySortingWeights.get(item);
if (comparable != null) return comparable;
@@ -116,35 +105,13 @@ public class CompletionLookupArranger extends LookupArranger {
}
public static int doCompare(final double priority1, final double priority2, final Comparable[] weight1, final Comparable[] weight2) {
if (priority1 != priority2) {
final double v = priority1 - priority2;
if (v > 0) return -1;
if (v < 0) return 1;
}
for (int i = 0; i < weight1.length; i++) {
final Comparable w1 = weight1[i];
final Comparable w2 = i < weight2.length ? weight2[i]:null;
if (w1 != null || w2 != null) {
if (w1 == null) return 1;
if (w2 == null) return -1;
//noinspection unchecked
final int res = w1.compareTo(w2);
if (res != 0) return -res;
}
}
return 0;
}
@Override
public LookupItemWeightComparable getRelevance(LookupElement item) {
LookupItemWeightComparable result = item.getUserData(RELEVANCE_KEY);
if (result != null) return result;
final double priority = item instanceof LookupItem ? ((LookupItem)item).getPriority() : 0;
result = new LookupItemWeightComparable(priority, getRelevanceWeight(item));
result = new LookupItemWeightComparable(priority, WeighingService.weigh(CompletionService.RELEVANCE_KEY, item, myLocation));
item.putUserData(RELEVANCE_KEY, result);
@@ -15,27 +15,27 @@
*/
package com.intellij.codeInsight.lookup.impl;
import com.intellij.codeInsight.completion.CompletionLookupArranger;
import java.util.Arrays;
/**
* @author peter
*/
public class LookupItemWeightComparable implements Comparable<LookupItemWeightComparable> {
private final double myPriority;
private final Comparable[] myWeight;
private final Comparable myWeight;
public LookupItemWeightComparable(final double priority, final Comparable[] weight) {
public LookupItemWeightComparable(final double priority, final Comparable weight) {
myPriority = priority;
myWeight = weight;
}
public int compareTo(final LookupItemWeightComparable o) {
return CompletionLookupArranger.doCompare(myPriority, o.myPriority, myWeight, o.myWeight);
if (myPriority != o.myPriority) {
return myPriority - o.myPriority > 0 ? -1 : 1;
}
return -myWeight.compareTo(o.myWeight);
}
public String toString() {
return myPriority + " " + Arrays.toString(myWeight);
return myPriority + " " + myWeight;
}
}
@@ -58,7 +58,7 @@ public class DumpLookupElementWeights extends AnAction implements DumbAware {
Map<String, String> values = new HashMap<String, String>();
Set<String> toExclude = new HashSet<String>();
for (LookupElement item : items) {
final String weight = Arrays.toString(item.getUserData(CompletionLookupArranger.WEIGHT));
final String weight = item.getUserData(CompletionLookupArranger.RELEVANCE_KEY).toString();
final Matcher matcher = pattern.matcher(weight);
int start = 0;
while (matcher.find(start)) {
@@ -78,7 +78,7 @@ public class DumpLookupElementWeights extends AnAction implements DumbAware {
for (int i = 0; i < items.size(); i++) {
LookupElement item = items.get(i);
String weight = Arrays.toString(item.getUserData(CompletionLookupArranger.WEIGHT));
String weight = item.getUserData(CompletionLookupArranger.RELEVANCE_KEY).toString();
for (String s : toExclude) {
weight = StringUtil.replace(weight, s, "", false);
}
@@ -1700,7 +1700,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig
final List<String> strings = new ArrayList<String>();
for (int i = 0; i < model.size(); i++) {
final LookupElement item = model.get(i);
strings.add(item.getLookupString() + Arrays.toString(item.getUserData(CompletionLookupArranger.WEIGHT)));
strings.add(item.getLookupString() + item.getUserData(CompletionLookupArranger.RELEVANCE_KEY));
if (i == count - 1) {
strings.add("---");
}