mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-02-05 08:06:56 +07:00
[java, complete, import-module] Add support for the completion order of the "import module declarations"
GitOrigin-RevId: 395229f6e5ef536219e26fed01c9e6ee953e12e6
This commit is contained in:
committed by
intellij-monorepo-bot
parent
8b5888f3ab
commit
bf4e754b12
@@ -24,6 +24,17 @@ public final class ExplicitlyImportedWeigher extends ProximityWeigher {
|
||||
PsiElement position = location.getPosition();
|
||||
return position == null ? null : getContextPackage(position);
|
||||
});
|
||||
private static final NullableLazyKey<List<String>, ProximityLocation> PLACE_IMPORTED_MODULE_NAMES = NullableLazyKey.create("importedModuleNames", location -> {
|
||||
final PsiJavaFile psiJavaFile = PsiTreeUtil.getContextOfType(location.getPosition(), PsiJavaFile.class, false);
|
||||
final PsiImportList importList = psiJavaFile == null ? null : psiJavaFile.getImportList();
|
||||
if (importList == null) return Collections.emptyList();
|
||||
|
||||
List<String> importedModuleNames = new ArrayList<>();
|
||||
for (PsiImportModuleStatement statement : importList.getImportModuleStatements()) {
|
||||
ContainerUtil.addIfNotNull(importedModuleNames, statement.getReferenceName());
|
||||
}
|
||||
return importedModuleNames;
|
||||
});
|
||||
private static final NotNullLazyKey<List<String>, ProximityLocation> PLACE_IMPORTED_NAMES =
|
||||
NotNullLazyKey.createLazyKey("importedNames", location -> {
|
||||
final PsiJavaFile psiJavaFile = PsiTreeUtil.getContextOfType(location.getPosition(), PsiJavaFile.class, false);
|
||||
@@ -95,6 +106,12 @@ public final class ExplicitlyImportedWeigher extends ProximityWeigher {
|
||||
return ImportWeight.CLASS_ON_DEMAND_NESTED;
|
||||
}
|
||||
|
||||
List<String> moduleNames = PLACE_IMPORTED_MODULE_NAMES.getValue(location);
|
||||
if (moduleNames != null && !moduleNames.isEmpty()) {
|
||||
PsiJavaModule psiJavaModule = JavaModuleGraphHelper.getInstance().findDescriptorByElement(element);
|
||||
if (psiJavaModule != null && moduleNames.contains(psiJavaModule.getName())) return ImportWeight.MODULE_IMPORTED;
|
||||
}
|
||||
|
||||
final PsiPackage placePackage = PLACE_PACKAGE.getValue(location);
|
||||
if (placePackage != null) {
|
||||
Module elementModule = ModuleUtilCore.findModuleForPsiElement(element);
|
||||
@@ -134,6 +151,7 @@ public final class ExplicitlyImportedWeigher extends ProximityWeigher {
|
||||
CLASS_HAS_SAME_PACKAGE_IMPORT,
|
||||
CLASS_DECLARED_IN_SAME_PACKAGE_NESTED,
|
||||
CLASS_ON_DEMAND_NESTED,
|
||||
MODULE_IMPORTED,
|
||||
CLASS_ON_DEMAND_TOP_LEVEL,
|
||||
CLASS_JAVA_LANG,
|
||||
MEMBER_SAME_PACKAGE,
|
||||
|
||||
@@ -207,6 +207,48 @@ class ModuleCompletionTest : LightJava9ModulesCodeInsightFixtureTestCase() {
|
||||
module current.module.name { }
|
||||
""".trimIndent())
|
||||
|
||||
@NeedsIndex.Full
|
||||
fun testModuleImportDeclarationsOrder() {
|
||||
addFile("module-info.java", """
|
||||
module first.module.name {
|
||||
exports first.module.name;
|
||||
}
|
||||
""".trimIndent(), M2)
|
||||
addFile("MyClassA.java", """
|
||||
package first.module.name;
|
||||
public class MyClassA { }
|
||||
""".trimIndent(), M2)
|
||||
|
||||
addFile("module-info.java", """
|
||||
module second.module.name {
|
||||
exports second.module.name;
|
||||
}
|
||||
""".trimIndent(), M4)
|
||||
addFile("MyClassB.java", """
|
||||
package second.module.name;
|
||||
public class MyClassB { }
|
||||
""".trimIndent(), M4)
|
||||
|
||||
addFile("MyClassC.java", """
|
||||
package current.pkg.name;
|
||||
public class MyClassC { }
|
||||
""".trimIndent())
|
||||
myFixture.configureByText("Main.java", """
|
||||
import module second.module.name;
|
||||
import current.pkg.name.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
MyCla<caret>
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
|
||||
myFixture.complete(CompletionType.BASIC)
|
||||
myFixture.getLookup()
|
||||
myFixture.assertPreferredCompletionItems(0, "MyClassC", "MyClassB", "MyClassA")
|
||||
}
|
||||
|
||||
//<editor-fold desc="Helpers.">
|
||||
private fun complete(text: String, expected: String) = fileComplete("module-info.java", text, expected)
|
||||
private fun fileComplete(fileName: String, text: String, expected: String) {
|
||||
|
||||
Reference in New Issue
Block a user