Middle Matching Disrupts My Workflow (IDEA-89279)

This commit is contained in:
Maxim.Mossienko
2012-10-25 15:20:41 +02:00
parent ba759c57c9
commit f8756f2991
3 changed files with 56 additions and 37 deletions
@@ -243,7 +243,7 @@ public class CompletionServiceImpl extends CompletionService{
for (final Weigher weigher : WeighingService.getWeighers(CompletionService.RELEVANCE_KEY)) {
final String id = weigher.toString();
if ("prefix".equals(id)) {
sorter = sorter.withClassifier(CompletionSorterImpl.weighingFactory(new PrefixMatchingClassifier(location)));
sorter = sorter.withClassifier(CompletionSorterImpl.weighingFactory(new RealPrefixMatchingWeigher(location)));
}
else if ("stats".equals(id)) {
sorter = sorter.withClassifier(CompletionSorterImpl.weighingFactory(new StatisticsWeigher.LookupStatisticsWeigher(location)));
@@ -276,25 +276,11 @@ public class CompletionServiceImpl extends CompletionService{
return new CompletionSorterImpl(new ArrayList<ClassifierFactory<LookupElement>>());
}
private static class PreferStartMatching extends LookupElementWeigher {
private final CompletionLocation myLocation;
public PreferStartMatching(CompletionLocation location) {
super("middleMatching", false, true);
myLocation = location;
}
@Override
public Comparable weigh(@NotNull LookupElement element) {
return !isStartMatch(element, myLocation.getCompletionParameters().getLookup());
}
}
public static boolean isStartMatch(LookupElement element, Lookup lookup) {
return getItemMatcher(element, lookup).isStartMatch(element);
}
private static PrefixMatcher getItemMatcher(LookupElement element, Lookup lookup) {
static PrefixMatcher getItemMatcher(LookupElement element, Lookup lookup) {
PrefixMatcher itemMatcher = lookup.itemMatcher(element);
String pattern = lookup.itemPattern(element);
if (!pattern.equals(itemMatcher.getPrefix())) {
@@ -302,25 +288,4 @@ public class CompletionServiceImpl extends CompletionService{
}
return itemMatcher;
}
private static class PrefixMatchingClassifier extends LookupElementWeigher {
private final CompletionLocation myLocation;
public PrefixMatchingClassifier(CompletionLocation location) {
super("prefix", false, true);
myLocation = location;
}
@Override
public Comparable weigh(@NotNull LookupElement element) {
final PrefixMatcher matcher = getItemMatcher(element, myLocation.getCompletionParameters().getLookup());
int max = Integer.MIN_VALUE;
for (String lookupString : element.getAllLookupStrings()) {
max = Math.max(max, matcher.matchingDegree(lookupString));
}
return -max;
}
}
}
@@ -0,0 +1,23 @@
package com.intellij.codeInsight.completion.impl;
import com.intellij.codeInsight.completion.CompletionLocation;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementWeigher;
import org.jetbrains.annotations.NotNull;
/**
* @author Peter
*/
public class PreferStartMatching extends LookupElementWeigher {
private final CompletionLocation myLocation;
public PreferStartMatching(CompletionLocation location) {
super("middleMatching", false, true);
myLocation = location;
}
@Override
public Comparable weigh(@NotNull LookupElement element) {
return !CompletionServiceImpl.isStartMatch(element, myLocation.getCompletionParameters().getLookup());
}
}
@@ -0,0 +1,31 @@
package com.intellij.codeInsight.completion.impl;
import com.intellij.codeInsight.completion.CompletionLocation;
import com.intellij.codeInsight.completion.PrefixMatcher;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementWeigher;
import org.jetbrains.annotations.NotNull;
/**
* @author Peter
*/
public class RealPrefixMatchingWeigher extends LookupElementWeigher {
private final CompletionLocation myLocation;
public RealPrefixMatchingWeigher(CompletionLocation location) {
super("prefix", false, true);
myLocation = location;
}
@Override
public Comparable weigh(@NotNull LookupElement element) {
final PrefixMatcher matcher = CompletionServiceImpl.getItemMatcher(element, myLocation.getCompletionParameters().getLookup());
int max = Integer.MIN_VALUE;
for (String lookupString : element.getAllLookupStrings()) {
max = Math.max(max, matcher.matchingDegree(lookupString));
}
return -max;
}
}