Java: Quick fix for merging duplicate statements in module-info - don't offer to merge the statements where merging is equivalent to deletion (IDEA-169211, IDEA-CR-19927)

This commit is contained in:
Pavel Dolgov
2017-04-14 15:42:27 +03:00
parent 24618eb197
commit 9a6fa90298
5 changed files with 27 additions and 27 deletions

View File

@@ -88,13 +88,21 @@ public class MergePackageAccessibilityStatementsFix
if (packageName != null) {
final List<String> moduleNames = statement.getModuleNames();
if (!moduleNames.isEmpty()) {
PsiPackageAccessibilityStatement targetStatement = null;
for (PsiPackageAccessibilityStatement candidate : getStatements(javaModule, statement.getRole())) {
if (candidate != statement &&
packageName.equals(candidate.getPackageName()) &&
candidate.getModuleNames().iterator().hasNext()) {
return new MergePackageAccessibilityStatementsFix(statement, packageName, moduleNames, candidate);
if (candidate != statement && packageName.equals(candidate.getPackageName())) {
if (candidate.getModuleNames().isEmpty()) {
// merging with a statement that has no target modules is equivalent to deletion; deletion is a different fix
return null;
}
if (targetStatement == null) {
targetStatement = candidate;
}
}
}
if (targetStatement != null) {
return new MergePackageAccessibilityStatementsFix(statement, packageName, moduleNames, targetStatement);
}
}
}
}

View File

@@ -1,5 +1,5 @@
module M {
exports my.api;
exports <caret>my.api to M2, M4;
exports my.api to M6;
exports my.api;
}

View File

@@ -1,4 +0,0 @@
module M {
exports my.api;
exports my.api to M6, M2, M4;
}

View File

@@ -1,4 +0,0 @@
module M {
opens my.api;
opens my.api to M6, M2, M4;
}

View File

@@ -17,7 +17,6 @@ package com.intellij.codeInsight.daemon.quickFix
import com.intellij.JavaTestUtil.getRelativeJavaTestDataPath
import com.intellij.codeInsight.daemon.QuickFixBundle
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.testFramework.fixtures.LightJava9ModulesCodeInsightFixtureTestCase
import com.intellij.testFramework.fixtures.MultiModuleJava9ProjectDescriptor.ModuleDescriptor.*
@@ -29,14 +28,14 @@ class MergeModuleStatementsFixTest : LightJava9ModulesCodeInsightFixtureTestCase
override fun getBasePath() = getRelativeJavaTestDataPath() + "/codeInsight/daemonCodeAnalyzer/quickFix/mergeModuleStatementsFix"
fun testExports1() = doTest("exports", "my.api")
fun testExports2() = doTest("exports", "my.api")
fun testExports2() = doTest("exports", "my.api", false)
fun testProvides1() = doTest("provides", "my.api.MyService")
fun testProvides2() = doTest("provides", "my.api.MyService")
fun testProvides3() = doTest("provides", "my.api.MyService")
fun testOpens1() = doTest("opens", "my.api")
fun testOpens2() = doTest("opens", "my.api")
fun testOpens2() = doTest("opens", "my.api", false)
override fun setUp() {
@@ -51,22 +50,23 @@ class MergeModuleStatementsFixTest : LightJava9ModulesCodeInsightFixtureTestCase
addFile("my/impl/MyServiceImpl2.java", "package my.impl; public class MyServiceImpl2 extends my.api.MyService {}")
}
private fun doTest(type: String, name: String) {
private fun doTest(type: String, name: String, expected: Boolean = true) {
val testName = getTestName(false)
val virtualFile = myFixture.copyFileToProject("${testName}.java", "module-info.java")
myFixture.configureFromExistingVirtualFile(virtualFile)
val action = findActionWithText(QuickFixBundle.message("java.9.merge.module.statements.fix.name", type, name))
myFixture.doHighlighting()
val actions = LightQuickFixTestCase.getAvailableActions(editor, file)
val actionText = QuickFixBundle.message("java.9.merge.module.statements.fix.name", type, name)
val action = LightQuickFixTestCase.findActionWithText(actions, actionText)
if (!expected) {
assertNull("Action \"$actionText\" is not expected", action)
return
}
assertNotNull("No action \"$actionText\" in ${actions.map { it.text }}", action)
myFixture.launchAction(action)
myFixture.checkResultByFile("${testName}_after.java")
}
private fun findActionWithText(actionText: String): IntentionAction {
myFixture.doHighlighting()
val actions = LightQuickFixTestCase.getAvailableActions(editor, file)
val action = LightQuickFixTestCase.findActionWithText(actions, actionText)
assertNotNull("No action [$actionText] in ${actions.map { it.text }}", action)
return action
}
}