diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java index 3aac36f658b0..d341310da349 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java @@ -62,7 +62,6 @@ public class JavaCompletionSorting { afterPriority.add(new PreferDefaultTypeWeigher(expectedTypes, parameters)); } ContainerUtil.addIfNotNull(afterPriority, recursion(parameters, expectedTypes)); - afterPriority.add(new PreferSimilarlyEnding(expectedTypes)); List afterProximity = new ArrayList(); afterProximity.add(new PreferContainingSameWords(expectedTypes)); @@ -86,10 +85,11 @@ public class JavaCompletionSorting { afterPrefix.add(new PreferExpected(false, expectedTypes)); } afterPrefix.add(new PreferByKindWeigher(type, position)); + afterPrefix.add(new PreferSimilarlyEnding(expectedTypes)); Collections.addAll(afterPrefix, new PreferNonGeneric(), new PreferAccessible(position), new PreferSimple(), new PreferEnumConstants(parameters)); - - + + sorter = sorter.weighAfter("priority", afterPriority.toArray(new LookupElementWeigher[afterPriority.size()])); sorter = sorter.weighAfter("prefix", afterPrefix.toArray(new LookupElementWeigher[afterPrefix.size()])); sorter = sorter.weighAfter("proximity", afterProximity.toArray(new LookupElementWeigher[afterProximity.size()])); diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionStatistician.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionStatistician.java index b0309da46d85..4a42e11903be 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionStatistician.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionStatistician.java @@ -16,6 +16,7 @@ package com.intellij.codeInsight.completion; import com.intellij.codeInsight.ExpectedTypeInfo; +import com.intellij.codeInsight.ExpectedTypeInfoImpl; import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupItem; import com.intellij.psi.*; @@ -45,23 +46,26 @@ public class JavaCompletionStatistician extends CompletionStatistician{ PsiType qualifierType = JavaCompletionUtil.getQualifierType(item); if (o instanceof PsiMember) { + final ExpectedTypeInfo[] infos = JavaCompletionUtil.EXPECTED_TYPES.getValue(location); + final ExpectedTypeInfo firstInfo = infos != null && infos.length > 0 ? infos[0] : null; String key2 = JavaStatisticsManager.getMemberUseKey2((PsiMember)o); if (o instanceof PsiClass) { - final ExpectedTypeInfo[] infos = JavaCompletionUtil.EXPECTED_TYPES.getValue(location); - PsiType expectedType = infos != null && infos.length > 0 ? infos[0].getDefaultType() : null; + PsiType expectedType = firstInfo != null ? firstInfo.getDefaultType() : null; return new StatisticsInfo(JavaStatisticsManager.getAfterNewKey(expectedType), key2); } PsiClass containingClass = ((PsiMember)o).getContainingClass(); if (containingClass != null) { - String context = JavaStatisticsManager.getMemberUseKey2(containingClass); + String expectedName = firstInfo instanceof ExpectedTypeInfoImpl ? ((ExpectedTypeInfoImpl)firstInfo).expectedName.compute() : null; + String contextPrefix = expectedName == null ? "" : "expectedName=" + expectedName + "###"; + String context = contextPrefix + JavaStatisticsManager.getMemberUseKey2(containingClass); if (o instanceof PsiMethod) { String memberValue = JavaStatisticsManager.getMemberUseKey2(RecursionWeigher.findDeepestSuper((PsiMethod)o)); - List superMethodInfos = ContainerUtil.newArrayList(new StatisticsInfo(context, memberValue)); + List superMethodInfos = ContainerUtil.newArrayList(new StatisticsInfo(contextPrefix + context, memberValue)); for (PsiClass superClass : InheritanceUtil.getSuperClasses(containingClass)) { - superMethodInfos.add(new StatisticsInfo(JavaStatisticsManager.getMemberUseKey2(superClass), memberValue)); + superMethodInfos.add(new StatisticsInfo(contextPrefix + JavaStatisticsManager.getMemberUseKey2(superClass), memberValue)); } return StatisticsInfo.createComposite(superMethodInfos); } diff --git a/java/java-tests/testData/codeInsight/completion/normalSorting/StatsMoreImportantThanExpectedType.java b/java/java-tests/testData/codeInsight/completion/normalSorting/StatsMoreImportantThanExpectedType.java index 2f1162c317ae..a1bd6e25efae 100644 --- a/java/java-tests/testData/codeInsight/completion/normalSorting/StatsMoreImportantThanExpectedType.java +++ b/java/java-tests/testData/codeInsight/completion/normalSorting/StatsMoreImportantThanExpectedType.java @@ -3,7 +3,7 @@ public class AnotherTestClass { test(getnu); } - private static void test(int i, int j) { + private static void test(int i) { } public static NumberProvider getNumProvider() { diff --git a/java/java-tests/testData/codeInsight/completion/smartTypeSorting/ExpectedNameDependentStats.java b/java/java-tests/testData/codeInsight/completion/smartTypeSorting/ExpectedNameDependentStats.java new file mode 100644 index 000000000000..0247002ed85c --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartTypeSorting/ExpectedNameDependentStats.java @@ -0,0 +1,9 @@ +class Foo { + String myFoo; + String myBar; + + String getFoo() { + return my + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/smartTypeSorting/PreferLocalOverFactoryMatchingName.java b/java/java-tests/testData/codeInsight/completion/smartTypeSorting/PreferLocalOverFactoryMatchingName.java new file mode 100644 index 000000000000..16f5a44d76f7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartTypeSorting/PreferLocalOverFactoryMatchingName.java @@ -0,0 +1,16 @@ +public class Aaaaaaa { + void foo(ActionEvent e) { + bar(); + } + + void bar(ActionEvent event) { + + } + +} + +class ActionEvent { + static ActionEvent createEvent() { + + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionOrderingTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionOrderingTest.groovy index c1fc757f89e5..21f4f6b5131a 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionOrderingTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionOrderingTest.groovy @@ -533,7 +533,7 @@ import java.lang.annotation.Target; invokeCompletion(getTestName(false) + ".java") assertPreferredItems 0, 'getNumber', 'getNumProvider' lookup.currentItem = lookup.items[1] - myFixture.type '\n, getn' + myFixture.type '\n);\ntest(getnu' myFixture.completeBasic() assertPreferredItems 0, 'getNumProvider', 'getNumber' } diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionOrderingTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionOrderingTest.groovy index fa418ec9c5fb..8d3299208608 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionOrderingTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionOrderingTest.groovy @@ -21,7 +21,7 @@ public class SmartTypeCompletionOrderingTest extends CompletionSortingTestCase { } public void testJComponentAdd() throws Throwable { - checkPreferredItems(0, "name", "getName", "b", "fooBean239", "foo", "this"); + checkPreferredItems(0, "name", "b", "fooBean239", "foo", "this"); } public void testJComponentAddNew() throws Throwable { @@ -257,11 +257,11 @@ public class SmartTypeCompletionOrderingTest extends CompletionSortingTestCase { assertPreferredItems(0, "bar", "foo", "equals", "false", "true"); } - public void testFieldNameOutweighsStats() throws Throwable { + public void testExpectedNameDependentStats() throws Throwable { final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java"); assertPreferredItems(0, "myFoo", "myBar"); incUseCount(lookup, 1); //myBar - assertPreferredItems(0, "myFoo", "myBar"); + assertPreferredItems(0, "myBar", "myFoo"); } public void testPreferSameNamedMethods() { @@ -307,6 +307,9 @@ public class SmartTypeCompletionOrderingTest extends CompletionSortingTestCase { public void testPreferOtherGetterInSetterCall() { checkPreferredItems 0, 'color', 'getColor', 'getZooColor', 'hashCode' } + public void testPreferLocalOverFactoryMatchingName() { + checkPreferredItems 0, 'e', 'createEvent' + } @Override protected String getBasePath() {