void () { invokeCompletion(getTestName(false) + ".java"); myFixture.completeBasic(); myFixture.type("cont"); assertPreferredItems(0, "containsAll", "contains"); myFixture.getLookup().setCurrentItem(myFixture.getLookupElements()[1]); myFixture.type("\nc)) {\nif (set."); myFixture.completeBasic(); myFixture.type("cont"); assertPreferredItems(0, "contains", "containsAll"); } public void testDeepestSuperMethodStats() { invokeCompletion(getTestName(false) + ".java"); assertPreferredItems(0, "addX", "addY"); myFixture.type("y\n;set1.ad"); myFixture.completeBasic(); assertPreferredItems(0, "addY", "addX"); myFixture.type("x\n;set2.ad"); myFixture.completeBasic(); assertPreferredItems(0, "addX", "addY"); } public void testCommonPrefixMoreImportantThanKind() { CodeInsightSettings.getInstance().setCompletionCaseSensitive(CodeInsightSettings.NONE); checkPreferredItems(0, "PsiElement", "psiElement"); } public void testNoExpectedTypeInStringConcatenation() { checkPreferredItems(0, "vx"); } public void testLocalVarsOverStats() { CodeInsightSettings.getInstance().setCompletionCaseSensitive(CodeInsightSettings.NONE); checkPreferredItems(0, "psiElement", "PsiElement"); incUseCount(1); assertPreferredItems(0, "psiElement", "PsiElement"); } public void testHonorRecency() { invokeCompletion(getTestName(false) + ".java"); myFixture.completeBasic(); myFixture.type("setou\nz."); myFixture.completeBasic(); myFixture.type("set"); assertPreferredItems(0, "setOurText", "setText"); myFixture.type("te"); assertPreferredItems(0, "setText", "setOurText"); myFixture.type("\nz."); myFixture.completeBasic(); myFixture.type("set"); assertPreferredItems(0, "setText", "setOurText"); } public void testPreferString() { checkPreferredItems(0, "String", "System", "Short"); } public void testAnnotationEnum() { checkPreferredItems(0, "MyEnum.BAR", "MyEnum", "MyEnum.FOO"); } @NeedsIndex.ForStandardLibrary public void testPreferClassesOfExpectedClassType() { myFixture.addClass("class XException extends Exception {}"); checkPreferredItems(0, "XException.class", "XException", "XClass", "XIntf"); } public void testNoNumberValueOf() { checkPreferredItems(0, "value"); } public void testNoBooleansInMultiplication() { checkPreferredItems(0, "fact"); } @NeedsIndex.ForStandardLibrary public void testPreferAnnotationsToInterfaceKeyword() { checkPreferredItems(0, "Deprecated", "Override", "SuppressWarnings"); } @NeedsIndex.ForStandardLibrary public void testPreferThrownExceptionsInCatch() { checkPreferredItems(0, "final", "FileNotFoundException", "File"); } public void testHonorFirstLetterCase() { CodeInsightSettings.getInstance().setCompletionCaseSensitive(CodeInsightSettings.NONE); checkPreferredItems(0, "posIdMap", "PImageDecoder", "PNGImageDecoder"); } @NeedsIndex.Full public void testGlobalStaticMemberStats() { configureNoCompletion(getTestName(false) + ".java"); myFixture.complete(CompletionType.BASIC, 2); assertPreferredItems(0, "newLinkedSet0", "newLinkedSet1", "newLinkedSet2"); CompletionSortingTestCase.imitateItemSelection(getLookup(), 1); myFixture.complete(CompletionType.BASIC, 2); assertPreferredItems(0, "newLinkedSet1", "newLinkedSet0", "newLinkedSet2"); } @NeedsIndex.ForStandardLibrary public void testStaticMemberTypes() { checkPreferredItems(0, "newMap", "newList"); } @NeedsIndex.ForStandardLibrary public void testNoStatsInSuperInvocation() { checkPreferredItems(0, "put", "putAll"); myFixture.type("\n"); assertTrue(myFixture.getEditor().getDocument().getText().contains("put")); myFixture.type(");\nsuper."); myFixture.completeBasic(); assertPreferredItems(0, "get"); } public void testLiveTemplateOrdering() { LiveTemplateCompletionContributor.setShowTemplatesInTests(true, myFixture.getTestRootDisposable()); checkPreferredItems(0, "return"); assertTrue(ContainerUtil.exists(getLookup().getItems(), it -> it.getLookupString().equals("ritar"))); } @NeedsIndex.ForStandardLibrary public void testPreferLocalToExpectedTypedMethod() { checkPreferredItems(0, "event", "equals"); } public void testDispreferJustUsedEnumConstantsInSwitch() { checkPreferredItems(0, "BAR", "FOO", "GOO"); myFixture.type("\nbreak;\ncase "); LookupElement[] items = myFixture.completeBasic(); assertPreferredItems(0, "FOO", "GOO", "BAR"); assertEquals(NormalCompletionTestCase.renderElement(items[0]).getItemTextForeground(), JBColor.foreground()); assertEquals(NormalCompletionTestCase.renderElement(ContainerUtil.find(items, it -> it.getLookupString().equals("BAR"))) .getItemTextForeground(), JBColor.RED); } @NeedsIndex.ForStandardLibrary public void testPreferValueTypesReturnedFromMethod() { checkPreferredItems(0, "StringBuffer", "String", "Serializable", "SomeInterface", "SomeInterface", "SomeOtherClass"); assertEquals("SomeInterface<String>", NormalCompletionTestCase.renderElement(myFixture.getLookupElements()[3]).getItemText()); assertEquals("SomeInterface", NormalCompletionTestCase.renderElement(myFixture.getLookupElements()[4]).getItemText()); } @NeedsIndex.Full public void testPreferCastTypesHavingSpecifiedMethod() { checkPreferredItems(0, "MainClass1", "MainClass2", "Maa"); } public void testNaturalSorting() { checkPreferredItems(0, "fun1", "fun2", "fun10"); } @NeedsIndex.ForStandardLibrary public void testPreferVarsHavingReferencedMember() { checkPreferredItems(0, "xzMap", "xaString"); } @NeedsIndex.ForStandardLibrary public void testPreferCollectionsStaticOfExpectedType() { checkPreferredItems(0, "unmodifiableList", "unmodifiableCollection"); } @NeedsIndex.SmartMode(reason = "isEffectivelyDeprecated needs smart mode") public void testDispreferDeprecatedMethodWithUnresolvedQualifier() { myFixture.addClass("package foo; public class Assert { public static void assertTrue() {} }"); myFixture.addClass( "package bar; @Deprecated public class Assert { public static void assertTrue() {} public static void assertTrue2() {} }"); checkPreferredItems(0, "Assert.assertTrue", "Assert.assertTrue", "Assert.assertTrue2"); LookupElementPresentation p = NormalCompletionTestCase.renderElement(myFixture.getLookup().getItems().get(0)); assertTrue(p.getTailText().contains("foo")); assertFalse(p.isStrikeout()); p = NormalCompletionTestCase.renderElement(myFixture.getLookup().getItems().get(1)); assertTrue(p.getTailText().contains("bar")); assertTrue(p.isStrikeout()); } public void testPreferClassKeywordWhenExpectedClassType() { checkPreferredItems(0, "class"); } public void testPreferBooleanKeywordsWhenExpectedBoolean() { checkPreferredItems(0, "false", "factory"); } public void testPreferBooleanKeywordsWhenExpectedBoolean2() { checkPreferredItems(0, "false", "factory"); } @NeedsIndex.Full public void testPreferExplicitlyImportedStaticMembers() { myFixture.addClass( "\nclass ContainerUtilRt {\n static void newHashSet();\n static void newHashSet2();\n}\nclass ContainerUtil extends ContainerUtilRt {\n static void newHashSet();\n static void newHashSet3();\n}\n"); checkPreferredItems(0, "newHashSet", "newHashSet", "newHashSet3", "newHashSet2"); assertEquals("ContainerUtil", ((PsiMethod)myFixture.getLookupElements()[0].getPsiElement()).getContainingClass().getName()); } public void testPreferCatchAndFinallyAfterTry() { checkPreferredItems(0, "catch", "finally"); } @NeedsIndex.Full public void testPreselectClosestExactPrefixItem() { UISettings.getInstance().setSortLookupElementsLexicographically(true); myFixture.addClass("package pack1; public class SameNamed {}"); myFixture.addClass("package pack2; public class SameNamed {}"); checkPreferredItems(1, "SameNamed", "SameNamed"); assertTrue(NormalCompletionTestCase.renderElement(myFixture.getLookupElements()[0]).getTailText().contains("pack1")); assertTrue(NormalCompletionTestCase.renderElement(myFixture.getLookupElements()[1]).getTailText().contains("pack2")); } @NeedsIndex.ForStandardLibrary public void testPreferExpectedMethodTypeArg() { checkPreferredItems(0, "String", "Usage"); int typeArgOffset = myFixture.getEditor().getCaretModel().getOffset(); // increase usage stats of ArrayList LookupManager.getInstance(getProject()).hideActiveLookup(); myFixture.performEditorAction(IdeActions.ACTION_EDITOR_MOVE_LINE_END); myFixture.type("\nArrayLi"); myFixture.completeBasic(); myFixture.type(" l"); myFixture.completeBasic(); myFixture.type("\n;"); assertTrue(myFixture.getEditor().getDocument().getText().contains("ArrayList list;")); // check String is still preferred myFixture.getEditor().getCaretModel().moveToOffset(typeArgOffset); myFixture.completeBasic(); myFixture.assertPreferredCompletionItems(0, "String", "Usage"); } @NeedsIndex.ForStandardLibrary public void testMethodStatisticsPerQualifierType() { checkPreferredItems(0, "charAt"); myFixture.type("eq\n);\n"); assertTrue(myFixture.getEditor().getDocument().getText().contains("equals();\n")); myFixture.type("this."); myFixture.completeBasic(); assertPreferredItems(0, "someMethod"); } @NeedsIndex.Full public void testPreferImportedClassesAmongstSameNamed() { myFixture.addClass("package foo; public class String {}"); // use foo.String myFixture.configureByText("a.java", "class Foo { Stri<caret> }"); myFixture.completeBasic(); myFixture.getLookup().setCurrentItem(ContainerUtil.find(myFixture.getLookupElements(), it -> it.getLookupString().equals("String") && NormalCompletionTestCase.renderElement(it) .getTailText().contains("foo"))); myFixture.type("\n"); myFixture.checkResult("import foo.String;\n\nclass Foo { String<caret>\n}"); // assert non-imported String is not preselected when completing in another file myFixture.configureByText("b.java", "class Bar { Stri<caret> }"); myFixture.completeBasic(); myFixture.assertPreferredCompletionItems(0, "String", "String"); assertTrue(NormalCompletionTestCase.renderElement(myFixture.getLookupElements()[0]).getTailText().contains("java.lang")); } public void testClassNameStatisticsDoesntDependOnExpectedType() { checkPreferredItems(0, "ConflictsUtil", "ContainerUtil"); myFixture.getLookup().setCurrentItem(myFixture.getLookupElements()[1]); myFixture.type("\n.foo();\nlong l = ConUt"); myFixture.completeBasic(); assertPreferredItems(0, "ContainerUtil", "ConflictsUtil"); } @NeedsIndex.ForStandardLibrary public void testPreferListAddWithoutIndex() { checkPreferredItems(0, "add", "add", "addAll", "addAll"); assertTrue(NormalCompletionTestCase.renderElement(myFixture.getLookupElements()[1]).getTailText().contains("int index")); assertTrue(NormalCompletionTestCase.renderElement(myFixture.getLookupElements()[3]).getTailText().contains("int index")); } @NeedsIndex.Full public void testPreferExpectedTypeConstantOverSameNamedClass() { myFixture.addClass("package another; public class JSON {}"); checkPreferredItems(0, "Point.JSON", "JSON"); } public void testPreferExpectedEnumConstantInAnnotationAttribute() { checkPreferredItems(0, "MyEnum.bar", "MyEnum", "MyEnum.foo"); int unrelatedItem = ContainerUtil.indexOf(myFixture.getLookupElementStrings(), it -> it.contains("Throwable")); incUseCount(unrelatedItem); //nothing should change assertPreferredItems(0, "MyEnum.bar", "MyEnum", "MyEnum.foo"); } public void testPreferExpectedTypeFieldOverUnexpectedLocalVariables() { checkPreferredItems(0, "field", "local"); } public void testPreferConflictingFieldAfterThis() { checkPreferredItems(0, "text"); } public void testDoNotPreselectShorterDeprecatedClasses() { checkPreferredItems(1, "XLong", "XLonger"); } public void testSignatureBeforeStats() { myFixture.configureByText("a.java", "\nclass Foo { \n void foo(int a, int b) {\n Stri<caret>\n bar();\n }\n \n void bar(int a, int b) {} \n}"); myFixture.completeBasic(); myFixture.assertPreferredCompletionItems(0, "String"); myFixture.type("\n");// increase String statistics myFixture.type("foo;\nbar("); myFixture.completeBasic(); myFixture.assertPreferredCompletionItems(0, "a", "b", "a, b"); } @NeedsIndex.ForStandardLibrary public void test_selecting_static_field_after_static_method() { myFixture.configureByText("a.java", "class Foo { { System.<caret> } }"); myFixture.completeBasic(); myFixture.type("ex\n2);\n");// select 'exit' myFixture.type("System."); myFixture.completeBasic(); myFixture.assertPreferredCompletionItems(0, "exit"); myFixture.type("ou\n;\n");// select 'out' myFixture.type("System."); myFixture.completeBasic(); myFixture.assertPreferredCompletionItems(0, "out", "exit"); } @NeedsIndex.SmartMode(reason = "JavaGenerateMemberCompletionContributor.fillCompletionVariants works in smart mode only (for getters)") public void testPreferTypeToGeneratedMethod() { checkPreferredItems(0, "SomeClass", "public SomeClass getZoo"); } @NeedsIndex.SmartMode(reason = "JavaGenerateMemberCompletionContributor.fillCompletionVariants works in smart mode only (for getters and equals())") public void testPreferPrimitiveTypeToGeneratedMethod() { checkPreferredItems(0, "boolean", "public boolean isZoo", "public boolean equals"); } @NeedsIndex.ForStandardLibrary public void testPreferExceptionsInCatch() { myFixture.configureByText("a.java", "class Foo { { Enu<caret> } }"); myFixture.completeBasic(); myFixture.type("m\n");// select 'Enum' myFixture.type("; try {} catch(E"); myFixture.completeBasic(); myFixture.assertPreferredCompletionItems(0, "Exception", "Error"); } @NeedsIndex.ForStandardLibrary public void testPreferExceptionsInThrowsList() { checkPreferredItems(0, "IllegalStateException", "IllegalAccessException", "IllegalArgumentException"); } @NeedsIndex.ForStandardLibrary public void testPreferExceptionsInJavadocThrows() { checkPreferredItems(0, "IllegalArgumentException", "IllegalAccessException", "IllegalStateException"); } @NeedsIndex.ForStandardLibrary public void testPreferExpectedTypeArguments() { checkPreferredItems(0, "BlaOperation"); } public void testPreferFinalBeforeVariable() { checkPreferredItems(0, "final", "find1"); } @NeedsIndex.SmartMode(reason = "AbstractExpectedTypeSkipper works in smart mode only") public void testDispreferMultiMethodInterfaceAfterNew() { checkPreferredItems(1, "Intf", "IntfImpl"); } public void testDispreferAlreadyCalledBuilderMethods() { checkPreferredItems(0, "addInt", "append", "c", "d", "mayCallManyTimes", "putLong"); } public void testSelectAbstractClassWithNoAbstractMethods() { checkPreferredItems(0, "AbstractListener", "Listener"); } @NeedsIndex.ForStandardLibrary public void testPreferPrintln() { myFixture.configureByText("a.java", "class Foo { { System.out.pri<caret>x } }"); myFixture.completeBasic(); myFixture.assertPreferredCompletionItems(0, "println", "print"); myFixture.type("\t"); myFixture.checkResult("class Foo { { System.out.println(<caret>); } }"); } public void testPreferLocalArrayVariableToItsChains() { checkPreferredItems(0, "arrayVariable"); } @NeedsIndex.ForStandardLibrary public void test_void_method_in_nonvoid_context() { myFixture.configureByText("a.java", "class X { String getName() {return \"\";} void test() {System.out.println(this.n<caret>);}}"); myFixture.completeBasic(); assertStringItems("getName", "clone", "toString", "notify", "notifyAll", "finalize"); } @NeedsIndex.ForStandardLibrary public void test_void_context_replace() { myFixture.configureByText("a.java", "class X { String getName() {return \"\";} void test() {this.n<caret>otify();}}"); myFixture.completeBasic(); assertStringItems("notify", "notifyAll", "getName", "clone", "toString", "finalize"); } @NeedsIndex.Full public void test_import_nested_classes_order() { myFixture.addClass("public class Cls { public static class TestImport {}}"); myFixture.addClass("public class Cls2 { public static class TestImport {}}"); myFixture.addClass("package demo;public class TestImport {}"); myFixture.addClass("public class TestImport {}"); myFixture.configureByText("a.java", "import demo.*;import static Cls2.*; class Test {TestIm<caret>}"); myFixture.completeBasic(); LookupElement[] elements = myFixture.getLookupElements(); assertEquals(4, elements.length); List<String> weights = DumpLookupElementWeights.getLookupElementWeights(getLookup(), false); assertEquals("TestImport", elements[0].as(JavaPsiClassReferenceElement.class).getQualifiedName()); assertTrue(weights.get(0).contains("explicitlyImported=CLASS_DECLARED_IN_SAME_PACKAGE_TOP_LEVEL,"));// same package assertEquals("demo.TestImport", elements[1].as(JavaPsiClassReferenceElement.class).getQualifiedName()); assertTrue(weights.get(1).contains("explicitlyImported=CLASS_ON_DEMAND_TOP_LEVEL,"));// on-demand import assertEquals("Cls2.TestImport", elements[2].as(JavaPsiClassReferenceElement.class).getQualifiedName()); assertTrue(weights.get(2).contains("explicitlyImported=CLASS_ON_DEMAND_NESTED,"));// same package, nested class imported assertEquals("Cls.TestImport", elements[3].as(JavaPsiClassReferenceElement.class).getQualifiedName()); assertTrue(weights.get(3) .contains("explicitlyImported=CLASS_DECLARED_IN_SAME_PACKAGE_NESTED,"));// same package but nested class not imported } @NeedsIndex.SmartMode(reason = "Ordering requires smart mode") public void test_discourage_experimental() { myFixture.addClass("package org.jetbrains.annotations;public class ApiStatus{public @interface Experimental {}}"); myFixture.addClass("class Cls {@org.jetbrains.annotations.ApiStatus.Experimental public void methodA() {} public void methodB() {}}"); myFixture.configureByText("a.java", "class Test {void t(Cls cls) {cls.me<caret>}}"); myFixture.completeBasic(); assertEquals(myFixture.getLookupElementStrings(), List.of("methodB", "methodA")); } @NeedsIndex.Full public void testCompletionRuleCaseOrdering() { IdeaTestUtil.withLevel(myFixture.getModule(), LanguageLevel.JDK_21, () -> { myFixture.configureByFile(getTestName(false) + ".java"); myFixture.completeBasic(); assertPreferredItems(0, "case null", "case", "case null, default", "case A", "case B"); }); } @NeedsIndex.Full public void testCompletionCaseOrdering() { IdeaTestUtil.withLevel(myFixture.getModule(), LanguageLevel.JDK_21, () -> { myFixture.configureByFile(getTestName(false) + ".java"); myFixture.completeBasic(); assertPreferredItems(0, "case A", "case", "case B", "casecase", "case null", "case null, default"); }); } private static final String BASE_PATH = "/codeInsight/completion/normalSorting"; }