expected type is sometimes more important than prefix-variant case mismatch

This commit is contained in:
peter
2012-06-18 12:31:17 +02:00
parent 8d8309c532
commit b7edeb92b5
6 changed files with 28 additions and 19 deletions
@@ -62,7 +62,9 @@ public class JavaCompletionSorting {
if (!smart) {
ContainerUtil.addIfNotNull(afterNegativeStats, preferStatics(position, expectedTypes));
}
afterNegativeStats.add(new PreferLocalVariablesLiteralsAndAnnoMethodsWeigher(type, position));
if (!JavaCompletionData.START_FOR.accepts(position)) {
afterNegativeStats.add(new PreferLocalVariablesLiteralsAndAnnoMethodsWeigher(type, position));
}
ContainerUtil.addIfNotNull(afterNegativeStats, recursion(parameters, expectedTypes));
if (!smart && !afterNew) {
afterNegativeStats.add(new PreferExpected(false, expectedTypes));
@@ -0,0 +1,9 @@
class Foo {
boolean ENABLED;
void enable() {}
{
if (!en<caret>)
}
}
@@ -4,6 +4,7 @@
*/
package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.codeInsight.completion.impl.CompletionServiceImpl;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupManager;
@@ -28,6 +29,7 @@ public abstract class CompletionSortingTestCase extends LightFixtureCompletionTe
protected void tearDown() throws Exception {
LookupManager.getInstance(getProject()).hideActiveLookup();
UISettings.getInstance().SORT_LOOKUP_ELEMENTS_LEXICOGRAPHICALLY = false;
CodeInsightSettings.getInstance().COMPLETION_CASE_SENSITIVE = CodeInsightSettings.FIRST_LETTER;
super.tearDown();
}
@@ -202,7 +202,7 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
}
public void testLocalVarsOverMethods() {
checkPreferredItems(0, "value");
checkPreferredItems(0, "value", "valueOf");
}
public void testCurrentClassBest() {
@@ -329,12 +329,12 @@ public class NormalCompletionOrderingTest extends CompletionSortingTestCase {
public void testCaseInsensitivePrefixMatch() {
CodeInsightSettings.getInstance().COMPLETION_CASE_SENSITIVE = CodeInsightSettings.NONE;
try {
checkPreferredItems(1, "Foo", "foo1", "foo2");
}
finally {
CodeInsightSettings.getInstance().COMPLETION_CASE_SENSITIVE = CodeInsightSettings.FIRST_LETTER;
}
checkPreferredItems(1, "Foo", "foo1", "foo2");
}
public void testExpectedTypeIsMoreImportantThanCase() {
CodeInsightSettings.getInstance().COMPLETION_CASE_SENSITIVE = CodeInsightSettings.NONE;
checkPreferredItems(0, "ENABLED", "enable");
}
public void testPreferKeywordsToVoidMethodsInExpectedTypeContext() {
@@ -54,8 +54,7 @@ public class PrefixMatchingWeigher extends CompletionWeigher {
return new MinusculeMatcher(CamelHumpMatcher.applyMiddleMatching(prefix), sensitivity);
}
public static StartMatchingDegree getStartMatchingDegree(LookupElement element, CompletionLocation location) {
StartMatchingDegree result = StartMatchingDegree.middleMatch;
public static boolean isMiddleMatch(LookupElement element, CompletionLocation location) {
String prefix = location.getCompletionParameters().getLookup().itemPattern(element);
if (StringUtil.isNotEmpty(prefix)) {
MinusculeMatcher matcher = getMinusculeMatcher(prefix);
@@ -64,18 +63,15 @@ public class PrefixMatchingWeigher extends CompletionWeigher {
if (fragments != null) {
Iterator<TextRange> iterator = fragments.iterator();
if (!ls.isEmpty() && prefix.charAt(0) == ls.charAt(0)) {
return StartMatchingDegree.startMatchSameCase;
return false;
}
if (iterator.hasNext() && iterator.next().contains(0)) {
result = StartMatchingDegree.startMatchDifferentCase;
return false;
}
}
}
}
return result;
return true;
}
public enum StartMatchingDegree {
startMatchSameCase, startMatchDifferentCase, middleMatch
}
}
@@ -237,14 +237,14 @@ public class CompletionServiceImpl extends CompletionService{
final CompletionLocation location = new CompletionLocation(parameters);
CompletionSorterImpl sorter = emptySorter();
sorter = sorter.withClassifier(new ClassifierFactory<LookupElement>("prefixHumps") {
sorter = sorter.withClassifier(new ClassifierFactory<LookupElement>("startMatching") {
@Override
public Classifier<LookupElement> createClassifier(Classifier<LookupElement> next) {
return new ComparingClassifier<LookupElement>(next, "prefixHumps") {
return new ComparingClassifier<LookupElement>(next, "startMatching") {
@NotNull
@Override
public Comparable getWeight(LookupElement element) {
return PrefixMatchingWeigher.getStartMatchingDegree(element, location);
return PrefixMatchingWeigher.isMiddleMatch(element, location);
}
};
}