FIR IDE: Add HLAddWhenRemainingBranchesIntention

Original commit: 45ccec3b6472924bcf30c3e3fbdda2684a63e71b

GitOrigin-RevId: a419ae1a7d296de8fee55d446dfecd71268eb05a
This commit is contained in:
Tianyu Geng
2022-01-22 11:05:12 +00:00
committed by intellij-monorepo-bot
parent 85437991a9
commit 097eca7d97
7 changed files with 67 additions and 2 deletions
@@ -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, AddWhenRemainingBranchFixFactories.Input>(
KtWhenExpression::class,
AddWhenRemainingBranchFixFactories.applicator
) {
override val applicabilityRange: HLApplicabilityRange<KtWhenExpression> get() = ApplicabilityRanges.SELF
override val inputProvider: HLApplicatorInputProvider<KtWhenExpression, AddWhenRemainingBranchFixFactories.Input>
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)
}
}
@@ -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<AbstractFirShortenRefsTest> {
model("shortenRefsFir", pattern = KT_WITHOUT_DOTS, testMethodName = "doTestWithMuting")
@@ -0,0 +1,12 @@
enum class Entry {
FOO, BAR, BAZ
}
fun test(e: Entry): Int {
return when (e) {
<spot>Entry.FOO -> 1
Entry.BAR -> TODO()
Entry.BAZ -> TODO()
else -> 0</spot>
}
}
@@ -0,0 +1,10 @@
enum class Entry {
FOO, BAR, BAZ
}
fun test(e: Entry): Int {
return when (e) {
<spot>Entry.FOO -> 1
else -> 0</spot>
}
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention adds remaining branches on a <b>when</b> expression.
</body>
</html>
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.fir.intentions.HLAddWhenRemainingBranchesIntention
@@ -35,5 +35,10 @@
<className>org.jetbrains.kotlin.idea.fir.intentions.HLConvertToBlockBodyIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.fir.intentions.HLAddWhenRemainingBranchesIntention</className>
<category>Kotlin</category>
</intentionAction>
</extensions>
</idea-plugin>