From 63191ad02bc6c8432e9fa54f09b34600c5edec4c Mon Sep 17 00:00:00 2001 From: aleksandrina-streltsova Date: Wed, 10 Jul 2024 10:05:17 +0300 Subject: [PATCH] [kotlin] Fix rendering of missing branches in `AddRemainingWhenBranches` KTIJ-30496 GitOrigin-RevId: 4e33efd8dc48ca0f51fddd95d3b70d7df4a82a60 --- .../fixes/HighLevelQuickFixTestGenerated.java | 10 ++++++++++ .../AddRemainingWhenBranchesUtils.kt | 14 +------------- .../quickfix/K1QuickFixTestGenerated.java | 10 ++++++++++ .../when/addRemainingBranchesGeneric.kt | 17 +++++++++++++++++ .../when/addRemainingBranchesGeneric.kt.after | 18 ++++++++++++++++++ .../when/addRemainingBranchesGeneric2.kt | 18 ++++++++++++++++++ .../addRemainingBranchesGeneric2.kt.after | 19 +++++++++++++++++++ 7 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric.kt create mode 100644 plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric2.kt create mode 100644 plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric2.kt.after diff --git a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java index 1bc4397d04de..791f12a973c6 100644 --- a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java +++ b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java @@ -3514,6 +3514,16 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu runTest("../../../idea/tests/testData/quickfix/when/addRemainingBranchesEnumStatement.kt"); } + @TestMetadata("addRemainingBranchesGeneric.kt") + public void testAddRemainingBranchesGeneric() throws Exception { + runTest("../../../idea/tests/testData/quickfix/when/addRemainingBranchesGeneric.kt"); + } + + @TestMetadata("addRemainingBranchesGeneric2.kt") + public void testAddRemainingBranchesGeneric2() throws Exception { + runTest("../../../idea/tests/testData/quickfix/when/addRemainingBranchesGeneric2.kt"); + } + @TestMetadata("addRemainingBranchesInNonDefaultPackage.kt") public void testAddRemainingBranchesInNonDefaultPackage() throws Exception { runTest("../../../idea/tests/testData/quickfix/when/addRemainingBranchesInNonDefaultPackage.kt"); diff --git a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeinsights/impl/base/intentions/AddRemainingWhenBranchesUtils.kt b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeinsights/impl/base/intentions/AddRemainingWhenBranchesUtils.kt index cd55e204c034..b8929281380f 100644 --- a/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeinsights/impl/base/intentions/AddRemainingWhenBranchesUtils.kt +++ b/plugins/kotlin/code-insight/impl-base/src/org/jetbrains/kotlin/idea/codeinsights/impl/base/intentions/AddRemainingWhenBranchesUtils.kt @@ -58,19 +58,7 @@ object AddRemainingWhenBranchesUtils { val elseBranch = element.entries.find { it.isElse } (whenCloseBrace.prevSibling as? PsiWhiteSpace)?.replace(psiFactory.createNewLine()) for (case in missingCases) { - val branchConditionText = when (case) { - WhenMissingCase.Unknown, - WhenMissingCase.NullIsMissing, - is WhenMissingCase.BooleanIsMissing, - is WhenMissingCase.ConditionTypeIsExpect -> case.branchConditionText - is WhenMissingCase.IsTypeCheckIsMissing -> - if (case.isSingleton) { - "" - } else { - "is " - } + case.classId.asSingleFqName().render() - is WhenMissingCase.EnumCheckIsMissing -> case.callableId.asSingleFqName().render() - } + val branchConditionText = case.branchConditionText val entry = psiFactory.createWhenEntry("$branchConditionText -> TODO()") if (elseBranch != null) { element.addBefore(entry, elseBranch) diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java index af3611056eca..bb8e2f70a16a 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java @@ -19780,6 +19780,16 @@ public abstract class K1QuickFixTestGenerated extends AbstractK1QuickFixTest { runTest("testData/quickfix/when/addRemainingBranchesEnumStatement.kt"); } + @TestMetadata("addRemainingBranchesGeneric.kt") + public void testAddRemainingBranchesGeneric() throws Exception { + runTest("testData/quickfix/when/addRemainingBranchesGeneric.kt"); + } + + @TestMetadata("addRemainingBranchesGeneric2.kt") + public void testAddRemainingBranchesGeneric2() throws Exception { + runTest("testData/quickfix/when/addRemainingBranchesGeneric2.kt"); + } + @TestMetadata("addRemainingBranchesInNonDefaultPackage.kt") public void testAddRemainingBranchesInNonDefaultPackage() throws Exception { runTest("testData/quickfix/when/addRemainingBranchesInNonDefaultPackage.kt"); diff --git a/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric.kt b/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric.kt new file mode 100644 index 000000000000..ac863f270440 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric.kt @@ -0,0 +1,17 @@ +// "Add remaining branches" "true" +// ERROR: Unresolved reference: TODO +// ERROR: Unresolved reference: TODO + +sealed class Base { + fun foo(it: Base) { + when (it) { + is Derived -> TODO() + } + } +} + +class Derived: Base() +class Generic: Base() + +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddWhenRemainingBranchesFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.AddWhenRemainingBranchFixFactories$AddRemainingWhenBranchesQuickFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric.kt.after new file mode 100644 index 000000000000..ee33bb949936 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric.kt.after @@ -0,0 +1,18 @@ +// "Add remaining branches" "true" +// ERROR: Unresolved reference: TODO +// ERROR: Unresolved reference: TODO + +sealed class Base { + fun foo(it: Base) { + when (it) { + is Derived -> TODO() + is Generic<*, *> -> TODO() + } + } +} + +class Derived: Base() +class Generic: Base() + +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddWhenRemainingBranchesFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.AddWhenRemainingBranchFixFactories$AddRemainingWhenBranchesQuickFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric2.kt b/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric2.kt new file mode 100644 index 000000000000..23616cb0d63b --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric2.kt @@ -0,0 +1,18 @@ +// "Add remaining branches" "true" +// ERROR: Unresolved reference: TODO +// ERROR: Unresolved reference: TODO + +sealed class Base { + fun bar(it: TA) { + when (it) { + is Derived -> TODO() + } + } +} +class Derived: Base() +class Generic: Base() + +typealias TA = Base + +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddWhenRemainingBranchesFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.AddWhenRemainingBranchFixFactories$AddRemainingWhenBranchesQuickFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric2.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric2.kt.after new file mode 100644 index 000000000000..1d8d7ccbf877 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/when/addRemainingBranchesGeneric2.kt.after @@ -0,0 +1,19 @@ +// "Add remaining branches" "true" +// ERROR: Unresolved reference: TODO +// ERROR: Unresolved reference: TODO + +sealed class Base { + fun bar(it: TA) { + when (it) { + is Derived -> TODO() + is Generic<*, *> -> TODO() + } + } +} +class Derived: Base() +class Generic: Base() + +typealias TA = Base + +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddWhenRemainingBranchesFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.AddWhenRemainingBranchFixFactories$AddRemainingWhenBranchesQuickFix \ No newline at end of file