From f8756f299166dbbf8f197996f7c1893e1d3ead9d Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Thu, 25 Oct 2012 15:18:10 +0200 Subject: [PATCH] Middle Matching Disrupts My Workflow (IDEA-89279) --- .../impl/CompletionServiceImpl.java | 39 +------------------ .../completion/impl/PreferStartMatching.java | 23 +++++++++++ .../impl/RealPrefixMatchingWeigher.java | 31 +++++++++++++++ 3 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/completion/impl/PreferStartMatching.java create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/completion/impl/RealPrefixMatchingWeigher.java diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/CompletionServiceImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/CompletionServiceImpl.java index 8ab05d80133f..7806c2fbf413 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/CompletionServiceImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/CompletionServiceImpl.java @@ -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>()); } - 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; - } - - } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/PreferStartMatching.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/PreferStartMatching.java new file mode 100644 index 000000000000..569806405562 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/PreferStartMatching.java @@ -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()); + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/RealPrefixMatchingWeigher.java b/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/RealPrefixMatchingWeigher.java new file mode 100644 index 000000000000..26491a0bb36d --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/completion/impl/RealPrefixMatchingWeigher.java @@ -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; + } + +}