[java-import] IDEA-367539 auto-import. try to show packages

GitOrigin-RevId: 536bd4376056b948cee7ae932c7c72dc1837f29c
This commit is contained in:
Mikhail Pyltsin
2025-02-14 17:55:46 +00:00
committed by intellij-monorepo-bot
parent 639d17a13d
commit bc97ae6bb8
4 changed files with 48 additions and 2 deletions
@@ -32,6 +32,7 @@ import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
@@ -556,9 +557,13 @@ public class JavaMethodCallElement extends LookupItem<PsiMethod> implements Type
presentation.setIcon(DefaultLookupItemRenderer.getRawIcon(this));
presentation.setStrikeout(JavaElementLookupRenderer.isToStrikeout(this));
boolean isAutoImportName = myContainingClass != null &&
myMethod != null &&
JavaCodeStyleManager.getInstance(myMethod.getProject())
.isStaticAutoImportName(myContainingClass.getQualifiedName() + "." + myMethod.getName());
MemberLookupHelper helper = myHelper != null ? myHelper : new MemberLookupHelper(myMethod, myContainingClass, false, false);
helper.renderElement(presentation, myHelper != null, myHelper != null && !myHelper.willBeImported(), getSubstitutor());
boolean showPackage = myHelper != null && (!myHelper.willBeImported() || isAutoImportName);
helper.renderElement(presentation, myHelper != null, showPackage, getSubstitutor());
if (!myForcedQualifier.isEmpty()) {
presentation.setItemText(myForcedQualifier + presentation.getItemText());
}
@@ -0,0 +1,8 @@
package org;
class ClassTest {
void m() {
ba<caret>
}
}
@@ -0,0 +1,8 @@
package org;
class ClassTest {
void m() {
ba<caret>
}
}
@@ -4,6 +4,7 @@ package com.intellij.java.codeInsight.completion;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.JavaProjectCodeInsightSettings;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
@@ -338,6 +339,30 @@ public class AutoStaticImportCompletionTest extends NormalCompletionTestCase {
}
}
@NeedsIndex.Full
public void testShowPackage() {
addStaticAutoImport("org.example.Foo.bar");
myFixture.addClass("""
package org.example;
public final class Foo {
public static void bar() {}
}
""");
configure();
LookupElement[] elements = myFixture.getLookupElements();
if (elements != null) {
LookupElement element = ContainerUtil.find(elements, e ->
e.getLookupString().equals("bar") &&
e.getPsiElement() instanceof PsiMethod method &&
method.getContainingClass().getQualifiedName().equals("org.example.Foo"));
assertNotNull(element);
LookupElementPresentation presentation = new LookupElementPresentation();
element.renderElement(presentation);
assertTrue(presentation.getTailText().contains("org.example"));
}
checkResult();
}
private void addStaticAutoImport(@NotNull String name) {
JavaProjectCodeInsightSettings.getSettings(getProject()).includedAutoStaticNames.add(name);
}