OrderEntryFix: filter suggestions by qualified name

This commit is contained in:
Dmitry Avdeev
2018-03-16 17:37:34 +03:00
parent fbb302943b
commit ff7343e295
6 changed files with 64 additions and 1 deletions

View File

@@ -145,6 +145,26 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix {
if (allowedDependencies.isEmpty()) {
return result;
}
if (reference instanceof PsiJavaCodeReferenceElement) {
String qualifiedName = null;
if (((PsiJavaCodeReferenceElement)reference).isQualified()) {
qualifiedName = ((PsiJavaCodeReferenceElement)reference).getQualifiedName();
}
else if (containingFile instanceof PsiJavaFile) {
PsiImportList list = ((PsiJavaFile)containingFile).getImportList();
if (list != null) {
PsiImportStatementBase statement = list.findSingleImportStatement(shortReferenceName);
if (statement != null) {
//noinspection ConstantConditions
qualifiedName = statement.getImportReference().getQualifiedName();
}
}
}
if (qualifiedName != null) {
String finalQualifiedName = qualifiedName;
allowedDependencies.removeIf(aClass -> !finalQualifiedName.equals(aClass.getQualifiedName()));
}
}
OrderEntryFix moduleDependencyFix = new AddModuleDependencyFix(reference, currentModule, scope, allowedDependencies);
registrar.register(moduleDependencyFix);

View File

@@ -0,0 +1,8 @@
// "Add dependency on module 'A'" "true"
package y;
import x.InA;
public class AddAmbiguous {
InA<caret> a;
}

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4" relativePaths="true" type="JAVA_MODULE">
<component name="ModuleRootManager" />
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Lib" level="project" />
<orderEntryProperties />
</component>
</module>

View File

@@ -0,0 +1,4 @@
package z;
public class InA {
}

View File

@@ -237,6 +237,7 @@
<modules>
<module fileurl="file://$PROJECT_DIR$/A/A.iml" filepath="$PROJECT_DIR$/A/A.iml" />
<module fileurl="file://$PROJECT_DIR$/B/B.iml" filepath="$PROJECT_DIR$/B/B.iml" />
<module fileurl="file://$PROJECT_DIR$/C/C.iml" filepath="$PROJECT_DIR$/C/C.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" assert-keyword="true" jdk-15="true" project-jdk-name="1.5" project-jdk-type="JavaSDK">

View File

@@ -21,8 +21,10 @@ import com.intellij.codeInsight.daemon.quickFix.ActionHint;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.module.ModifiableModuleModel;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.ex.ProjectManagerEx;
@@ -93,7 +95,7 @@ public class OrderEntryTest extends DaemonAnalyzerTestCase {
if (afterAction != null) {
fail("Action '" + text + "' is still available after its invocation in test " + testFullPath);
}
assertEquals(infosBefore.size() - 1, infosAfter.size());
assertTrue(infosBefore.size() > infosAfter.size());
}
}
@@ -108,9 +110,21 @@ public class OrderEntryTest extends DaemonAnalyzerTestCase {
}
public void testAddDependency() {
removeModule();
doTest("B/src/y/AddDependency.java");
}
public void testAddAmbiguousDependency() {
doTest("B/src/y/AddAmbiguous.java");
}
private void removeModule() {
ModuleManager manager = ModuleManager.getInstance(getProject());
ModifiableModuleModel model = manager.getModifiableModel();
model.disposeModule(manager.findModuleByName("C"));
WriteAction.run(() -> model.commit());
}
public void testAddLibrary() {
doTest("B/src/y/AddLibrary.java");
}
@@ -119,6 +133,7 @@ public class OrderEntryTest extends DaemonAnalyzerTestCase {
final Module a = ModuleManager.getInstance(getProject()).findModuleByName("A");
final Module b = ModuleManager.getInstance(getProject()).findModuleByName("B");
ModuleRootModificationUtil.addDependency(a, b);
removeModule();
try {
doTest("B/src/y/AddDependency.java");