Add when branches: include auto-import
Before this commit, enum / sealed class to add was not imported so user had to import them himself. Now everything is auto-imported. Separate "add with import" is kept but * import is now in mind there. #KT-22330 Fixed #KT-22354 Fixed EA-108090 Fixed
This commit is contained in:
@@ -24,10 +24,13 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
|||||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
||||||
import org.jetbrains.kotlin.idea.core.quoteIfNeeded
|
import org.jetbrains.kotlin.idea.core.quoteIfNeeded
|
||||||
import org.jetbrains.kotlin.idea.intentions.ImportAllMembersIntention
|
import org.jetbrains.kotlin.idea.intentions.ImportAllMembersIntention
|
||||||
|
import org.jetbrains.kotlin.idea.refactoring.isMultiLine
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||||
|
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
|
import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
|
||||||
|
|
||||||
class AddWhenRemainingBranchesFix(
|
class AddWhenRemainingBranchesFix(
|
||||||
@@ -77,16 +80,20 @@ class AddWhenRemainingBranchesFix(
|
|||||||
val elseBranch = element.entries.find { it.isElse }
|
val elseBranch = element.entries.find { it.isElse }
|
||||||
val psiFactory = KtPsiFactory(element)
|
val psiFactory = KtPsiFactory(element)
|
||||||
|
|
||||||
|
if (missingCases.isNotEmpty() && !element.isMultiLine()) {
|
||||||
|
element.addBefore(psiFactory.createNewLine(), whenCloseBrace)
|
||||||
|
}
|
||||||
|
|
||||||
for (case in missingCases) {
|
for (case in missingCases) {
|
||||||
val branchConditionText = when (case) {
|
val branchConditionText = when (case) {
|
||||||
UnknownMissingCase, NullMissingCase, is BooleanMissingCase ->
|
UnknownMissingCase, NullMissingCase, is BooleanMissingCase ->
|
||||||
case.branchConditionText
|
case.branchConditionText
|
||||||
is ClassMissingCase ->
|
is ClassMissingCase ->
|
||||||
if (case.classIsSingleton) {
|
if (case.classIsSingleton) {
|
||||||
case.classFqName.quoteIfNeeded().asString()
|
""
|
||||||
} else {
|
} else {
|
||||||
"is " + case.classFqName.quoteIfNeeded().asString()
|
"is "
|
||||||
}
|
} + case.descriptor.fqNameSafe.quoteIfNeeded().asString()
|
||||||
}
|
}
|
||||||
val entry = psiFactory.createWhenEntry("$branchConditionText -> TODO()")
|
val entry = psiFactory.createWhenEntry("$branchConditionText -> TODO()")
|
||||||
if (elseBranch != null) {
|
if (elseBranch != null) {
|
||||||
@@ -96,6 +103,8 @@ class AddWhenRemainingBranchesFix(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ShortenReferences.DEFAULT.process(element)
|
||||||
|
|
||||||
if (withImport) {
|
if (withImport) {
|
||||||
importAllEntries(element)
|
importAllEntries(element)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
// "Add remaining branches" "true"
|
||||||
|
|
||||||
|
package u
|
||||||
|
|
||||||
|
import e.OwnEnum
|
||||||
|
import e.getOwnEnum
|
||||||
|
|
||||||
|
fun mainContext() {
|
||||||
|
val ownLocal = getOwnEnum()
|
||||||
|
when (ownLocal) {
|
||||||
|
OwnEnum.RED -> TODO()
|
||||||
|
OwnEnum.GREEN -> TODO()
|
||||||
|
OwnEnum.BLUE -> TODO()
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package e
|
||||||
|
|
||||||
|
enum class OwnEnum { RED, GREEN, BLUE }
|
||||||
|
|
||||||
|
fun getOwnEnum(): OwnEnum = OwnEnum.RED
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// "Add remaining branches" "true"
|
||||||
|
|
||||||
|
package u
|
||||||
|
|
||||||
|
import e.getOwnEnum
|
||||||
|
|
||||||
|
fun mainContext() {
|
||||||
|
val ownLocal = getOwnEnum()
|
||||||
|
<caret>when (ownLocal) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
// "Add remaining branches with import" "true"
|
||||||
|
|
||||||
|
package u
|
||||||
|
|
||||||
|
import e.OwnEnum
|
||||||
|
import e.OwnEnum.*
|
||||||
|
import e.getOwnEnum
|
||||||
|
|
||||||
|
fun mainContext() {
|
||||||
|
val ownLocal = getOwnEnum()
|
||||||
|
when (ownLocal) {
|
||||||
|
RED -> TODO()
|
||||||
|
GREEN -> TODO()
|
||||||
|
BLUE -> TODO()
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package e
|
||||||
|
|
||||||
|
enum class OwnEnum { RED, GREEN, BLUE }
|
||||||
|
|
||||||
|
fun getOwnEnum(): OwnEnum = OwnEnum.RED
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// "Add remaining branches with import" "true"
|
||||||
|
|
||||||
|
package u
|
||||||
|
|
||||||
|
import e.getOwnEnum
|
||||||
|
|
||||||
|
fun mainContext() {
|
||||||
|
val ownLocal = getOwnEnum()
|
||||||
|
<caret>when (ownLocal) {}
|
||||||
|
}
|
||||||
+10
@@ -4203,6 +4203,16 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
|||||||
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
|
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("addRemainingBranchesAnotherPackage.before.Main.kt")
|
||||||
|
public void testAddRemainingBranchesAnotherPackage() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/when/addRemainingBranchesAnotherPackage.before.Main.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("addRemainingBranchesAnotherPackageAll.before.Main.kt")
|
||||||
|
public void testAddRemainingBranchesAnotherPackageAll() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/when/addRemainingBranchesAnotherPackageAll.before.Main.kt");
|
||||||
|
}
|
||||||
|
|
||||||
public void testAllFilesPresentInWhen() throws Exception {
|
public void testAllFilesPresentInWhen() throws Exception {
|
||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/when"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/when"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user