diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/PreferByKindWeigher.java b/java/java-impl/src/com/intellij/codeInsight/completion/PreferByKindWeigher.java index 071023bca39a..071e348d88c4 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/PreferByKindWeigher.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/PreferByKindWeigher.java @@ -133,11 +133,10 @@ public class PreferByKindWeigher extends LookupElementWeigher { annoMethod, probableKeyword, castVariable, - localOrParameter, + variable, qualifiedWithField, qualifiedWithGetter, superMethodParameters, - field, expectedTypeConstant, expectedTypeArgument, getter, @@ -167,8 +166,10 @@ public class PreferByKindWeigher extends LookupElementWeigher { return MyResult.castVariable; } - if (object instanceof PsiLocalVariable || object instanceof PsiParameter || object instanceof PsiThisExpression) { - return MyResult.localOrParameter; + if (object instanceof PsiLocalVariable || object instanceof PsiParameter || + object instanceof PsiThisExpression || + object instanceof PsiField && !((PsiField)object).hasModifierProperty(PsiModifier.STATIC)) { + return MyResult.variable; } if (object instanceof String && item.getUserData(JavaCompletionUtil.SUPER_METHOD_PARAMETERS) == Boolean.TRUE) { @@ -197,7 +198,7 @@ public class PreferByKindWeigher extends LookupElementWeigher { if (chain != null) { Object qualifier = chain.getQualifier().getObject(); if (qualifier instanceof PsiLocalVariable || qualifier instanceof PsiParameter) { - return MyResult.localOrParameter; + return MyResult.variable; } if (qualifier instanceof PsiField) { return MyResult.qualifiedWithField; @@ -212,7 +213,6 @@ public class PreferByKindWeigher extends LookupElementWeigher { if (myCompletionType == CompletionType.SMART) { - if (object instanceof PsiField) return MyResult.field; if (isGetter(object)) return MyResult.getter; return MyResult.normal; diff --git a/java/java-impl/src/com/intellij/internal/ExpressionStatisticsAction.java b/java/java-impl/src/com/intellij/internal/ExpressionStatisticsAction.java index d4879c5aa28a..1248cb626f31 100644 --- a/java/java-impl/src/com/intellij/internal/ExpressionStatisticsAction.java +++ b/java/java-impl/src/com/intellij/internal/ExpressionStatisticsAction.java @@ -142,7 +142,11 @@ public class ExpressionStatisticsAction extends AnAction { data.packages++; } else if (target instanceof PsiField) { - data.fields++; + if (((PsiField)target).hasModifierProperty(PsiModifier.STATIC) && ((PsiField)target).hasModifierProperty(PsiModifier.FINAL)) { + data.constants++; + } else { + data.fields++; + } } else { data.other++; @@ -155,6 +159,7 @@ public class ExpressionStatisticsAction extends AnAction { int methods; int classes; int fields; + int constants; int packages; int other; @@ -163,11 +168,12 @@ public class ExpressionStatisticsAction extends AnAction { return "localVars=" + localVars + "\nparameters=" + parameters + "\nmethods=" + methods + + "\nconstants=" + constants + "\nfields=" + fields + "\nclasses=" + classes + "\npackages=" + packages + "\nother=" + other + - "\ntotal=" + (localVars + parameters + methods + fields + classes + packages + other); + "\ntotal=" + (localVars + parameters + methods + constants + fields + classes + packages + other); } } diff --git a/java/java-tests/testData/codeInsight/completion/normalSorting/PreferConflictingFieldAfterThis.java b/java/java-tests/testData/codeInsight/completion/normalSorting/PreferConflictingFieldAfterThis.java new file mode 100644 index 000000000000..1ef2c0cb8578 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normalSorting/PreferConflictingFieldAfterThis.java @@ -0,0 +1,9 @@ +class Foo { + String text; + + void doSmth() {} + + void foo(String text) { + this. + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/normalSorting/PreferExpectedTypeFieldOverUnexpectedLocalVariables.java b/java/java-tests/testData/codeInsight/completion/normalSorting/PreferExpectedTypeFieldOverUnexpectedLocalVariables.java new file mode 100644 index 000000000000..6ff66a83734d --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normalSorting/PreferExpectedTypeFieldOverUnexpectedLocalVariables.java @@ -0,0 +1,6 @@ +class Foo { + String field; + void foo(Integer local) { + String s = + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionOrderingTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionOrderingTest.groovy index e84787c9b8af..54279fd23e1a 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionOrderingTest.groovy +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionOrderingTest.groovy @@ -789,7 +789,14 @@ class ContainerUtil extends ContainerUtilRt { incUseCount(lookup, unrelatedItem) //nothing should change assertPreferredItems 0, 'MyEnum.bar', 'MyEnum', 'MyEnum.foo' - + } + + void testPreferExpectedTypeFieldOverUnexpectedLocalVariables() { + checkPreferredItems 0, 'field', 'local' + } + + void testPreferConflictingFieldAfterThis() { + checkPreferredItems 0, 'text' } } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionTest.groovy index e8952731473c..55fbc2cd80cb 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionTest.groovy @@ -45,7 +45,7 @@ class NormalCompletionTest extends LightFixtureCompletionTestCase { void testSimple() throws Exception { configureByFile("Simple.java") - assertStringItems("_local1", "_local2", "_field", "_method", "_baseField", "_baseMethod") + assertStringItems("_field", "_local1", "_local2", "_baseField", "_method", "_baseMethod") } void testCastToPrimitive1() throws Exception { diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartTypeCompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartTypeCompletionTest.java index 4893b8723a54..fd679a84e554 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartTypeCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartTypeCompletionTest.java @@ -473,7 +473,7 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase { public void testNoUninitializedFieldsInConstructor() throws Throwable { configureByTestName(); - assertStringItems("aac", "aab", "hashCode"); + assertStringItems("aab", "aac", "hashCode"); } public void testFieldsSetInAnotherConstructor() throws Throwable { doTest(); } public void testFieldsSetAbove() throws Throwable { doTest(); } diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.groovy index 63d377944e93..cb02f1717d68 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.groovy @@ -56,7 +56,7 @@ class VariablesCompletionTest extends LightFixtureCompletionTestCase { void testLocals2() throws Exception { configureByFile(FILE_PREFIX + "locals/" + "TestSource2.java") - myFixture.assertPreferredCompletionItems 0, 'abc', 'aaa' + myFixture.assertPreferredCompletionItems 0, 'aaa', 'abc' checkResultByFile(FILE_PREFIX + "locals/" + "TestResult2.java") }