From 097eca7d97f0bd89ddb07a209841f718ba7800a4 Mon Sep 17 00:00:00 2001 From: Tianyu Geng Date: Tue, 15 Jun 2021 15:46:52 -0700 Subject: [PATCH] FIR IDE: Add HLAddWhenRemainingBranchesIntention Original commit: 45ccec3b6472924bcf30c3e3fbdda2684a63e71b GitOrigin-RevId: a419ae1a7d296de8fee55d446dfecd71268eb05a --- .../HLAddWhenRemainingBranchesIntention.kt | 31 +++++++++++++++++++ .../kotlin/testGenerator/GenerateTests.kt | 5 +-- .../after.kt.template | 12 +++++++ .../before.kt.template | 10 ++++++ .../description.html | 5 +++ .../addWhenRemainingBranches/.firIntention | 1 + .../resources/META-INF/firIntentions.xml | 5 +++ 7 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 plugins/kotlin/fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLAddWhenRemainingBranchesIntention.kt create mode 100644 plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/after.kt.template create mode 100644 plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/before.kt.template create mode 100644 plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/description.html create mode 100644 plugins/kotlin/idea/tests/testData/intentions/addWhenRemainingBranches/.firIntention diff --git a/plugins/kotlin/fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLAddWhenRemainingBranchesIntention.kt b/plugins/kotlin/fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLAddWhenRemainingBranchesIntention.kt new file mode 100644 index 000000000000..1c21eef4ff66 --- /dev/null +++ b/plugins/kotlin/fir/src/org/jetbrains/kotlin/idea/fir/intentions/HLAddWhenRemainingBranchesIntention.kt @@ -0,0 +1,31 @@ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + +package org.jetbrains.kotlin.idea.fir.intentions + +import org.jetbrains.kotlin.diagnostics.WhenMissingCase +import org.jetbrains.kotlin.idea.fir.api.AbstractHLIntention +import org.jetbrains.kotlin.idea.fir.api.applicator.HLApplicabilityRange +import org.jetbrains.kotlin.idea.fir.api.applicator.HLApplicatorInputProvider +import org.jetbrains.kotlin.idea.fir.api.applicator.inputProvider +import org.jetbrains.kotlin.idea.fir.applicators.ApplicabilityRanges +import org.jetbrains.kotlin.idea.quickfix.fixes.AddWhenRemainingBranchFixFactories +import org.jetbrains.kotlin.psi.KtWhenExpression + +class HLAddWhenRemainingBranchesIntention : AbstractHLIntention( + KtWhenExpression::class, + AddWhenRemainingBranchFixFactories.applicator +) { + override val applicabilityRange: HLApplicabilityRange get() = ApplicabilityRanges.SELF + + override val inputProvider: HLApplicatorInputProvider + get() = inputProvider { element -> + // TODO: consider removing the condition below so that this intention also works if there is no else. Currently we only offer + // this intention if there is an `else` branch so that it behaves identically with FE1.0 (because FE1.0 reports warnings for + // non-exhaustive when, which then results in a quickfix). + if (element.entries.none { it.isElse }) return@inputProvider null + val whenMissingCases = element.getMissingCases().takeIf { + it.isNotEmpty() && it.singleOrNull() != WhenMissingCase.Unknown + } ?: return@inputProvider null + AddWhenRemainingBranchFixFactories.Input(whenMissingCases, null) + } +} \ No newline at end of file diff --git a/plugins/kotlin/generators/test/org/jetbrains/kotlin/testGenerator/GenerateTests.kt b/plugins/kotlin/generators/test/org/jetbrains/kotlin/testGenerator/GenerateTests.kt index b756eb0a02ac..9cb86d1298ed 100644 --- a/plugins/kotlin/generators/test/org/jetbrains/kotlin/testGenerator/GenerateTests.kt +++ b/plugins/kotlin/generators/test/org/jetbrains/kotlin/testGenerator/GenerateTests.kt @@ -1121,8 +1121,9 @@ private fun assembleWorkspace(): TWorkspace = workspace { model("intentions/specifyTypeExplicitly", pattern = pattern) model("intentions/importAllMembers", pattern = pattern) model("intentions/importMember", pattern = pattern) - model("intentions/convertToBlockBody", pattern = pattern) - } + model("intentions/convertToBlockBody", pattern = pattern) + model("intentions/addWhenRemainingBranches", pattern = pattern) + } testClass { model("shortenRefsFir", pattern = KT_WITHOUT_DOTS, testMethodName = "doTestWithMuting") diff --git a/plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/after.kt.template b/plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/after.kt.template new file mode 100644 index 000000000000..18050671af06 --- /dev/null +++ b/plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/after.kt.template @@ -0,0 +1,12 @@ +enum class Entry { + FOO, BAR, BAZ +} + +fun test(e: Entry): Int { + return when (e) { + Entry.FOO -> 1 + Entry.BAR -> TODO() + Entry.BAZ -> TODO() + else -> 0 + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/before.kt.template b/plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/before.kt.template new file mode 100644 index 000000000000..1821a49ec2ce --- /dev/null +++ b/plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/before.kt.template @@ -0,0 +1,10 @@ +enum class Entry { + FOO, BAR, BAZ +} + +fun test(e: Entry): Int { + return when (e) { + Entry.FOO -> 1 + else -> 0 + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/description.html b/plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/description.html new file mode 100644 index 000000000000..4680d1b1d19c --- /dev/null +++ b/plugins/kotlin/idea/resources-en/intentionDescriptions/HLAddWhenRemainingBranchesIntention/description.html @@ -0,0 +1,5 @@ + + +This intention adds remaining branches on a when expression. + + diff --git a/plugins/kotlin/idea/tests/testData/intentions/addWhenRemainingBranches/.firIntention b/plugins/kotlin/idea/tests/testData/intentions/addWhenRemainingBranches/.firIntention new file mode 100644 index 000000000000..a7a4444043e0 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/intentions/addWhenRemainingBranches/.firIntention @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.fir.intentions.HLAddWhenRemainingBranchesIntention \ No newline at end of file diff --git a/plugins/kotlin/resources-fir/resources/META-INF/firIntentions.xml b/plugins/kotlin/resources-fir/resources/META-INF/firIntentions.xml index 202f474c8cd5..6ef8a1017472 100644 --- a/plugins/kotlin/resources-fir/resources/META-INF/firIntentions.xml +++ b/plugins/kotlin/resources-fir/resources/META-INF/firIntentions.xml @@ -35,5 +35,10 @@ org.jetbrains.kotlin.idea.fir.intentions.HLConvertToBlockBodyIntention Kotlin + + + org.jetbrains.kotlin.idea.fir.intentions.HLAddWhenRemainingBranchesIntention + Kotlin + \ No newline at end of file