FIR IDE: Add HLAddWhenRemainingBranchesIntention

This commit is contained in:
Tianyu Geng
2021-06-15 15:46:52 -07:00
committed by Ilya Kirillov
parent 63c65edda2
commit 45ccec3b64
7 changed files with 68 additions and 0 deletions
@@ -1092,6 +1092,7 @@ fun main(args: Array<String>) {
model("intentions/importAllMembers", pattern = pattern)
model("intentions/importMember", pattern = pattern)
model("intentions/convertToBlockBody", pattern = pattern)
model("intentions/addWhenRemainingBranches", pattern = pattern)
}
testClass<AbstractFirShortenRefsTest> {
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt 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)
}
}
@@ -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>
@@ -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>
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.fir.intentions.HLAddWhenRemainingBranchesIntention