Repository: JetBrains/kotlin
File: idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/AddWhenElseBranchFix.kt (Kotlin)

Code before:
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType

class AddWhenElseBranchFix(element: KtWhenExpression) : KotlinPsiOnlyQuickFixAction<KtWhenExpression>(element) {
    override fun getFamilyName() = KotlinBundle.message("fix.add.else.branch.when")
    override fun getText() = familyName

    override fun isAvailable(project: Project, editor: Editor?, file: KtFile): Boolean {
        val element = element ?: return false
        return element.closeBrace != null
    }

    override fun invoke(project: Project, editor: Editor?, file: KtFile) {
        val element = element ?: return
        val psiFactory = KtPsiFactory(file)
        val entry = psiFactory.createWhenEntry("else ->")
        val whenCloseBrace = element.closeBrace ?: error("isAvailable should check if close brace exist")
        val insertedWhenEntry =
            CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(element.addBefore(entry, whenCloseBrace)) as KtWhenEntry
        val endOffset = insertedWhenEntry.endOffset
        editor?.document?.insertString(endOffset, " ")
        editor?.caretModel?.moveToOffset(endOffset + 1)
    }

    companion object {
        val factory: QuickFixesPsiBasedFactory<PsiElement> = quickFixesPsiBasedFactory { psiElement ->
            listOfNotNull(psiElement.getNonStrictParentOfType<KtWhenExpression>()?.let(::AddWhenElseBranchFix))
        }
    }
}


Review comment (suggestion):
Can you put this directly on the companion object? Just to make it consistent with a few others (see`ReplaceWithDotCallFix`)

Code after:
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType

class AddWhenElseBranchFix(element: KtWhenExpression) : KotlinPsiOnlyQuickFixAction<KtWhenExpression>(element) {
    override fun getFamilyName() = KotlinBundle.message("fix.add.else.branch.when")
    override fun getText() = familyName

    override fun isAvailable(project: Project, editor: Editor?, file: KtFile): Boolean {
        val element = element ?: return false
        return element.closeBrace != null
    }

    override fun invoke(project: Project, editor: Editor?, file: KtFile) {
        val element = element ?: return
        val psiFactory = KtPsiFactory(file)
        val entry = psiFactory.createWhenEntry("else ->")
        val whenCloseBrace = element.closeBrace ?: error("isAvailable should check if close brace exist")
        val insertedWhenEntry =
            CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(element.addBefore(entry, whenCloseBrace)) as KtWhenEntry
        val endOffset = insertedWhenEntry.endOffset
        editor?.document?.insertString(endOffset, " ")
        editor?.caretModel?.moveToOffset(endOffset + 1)
    }

    companion object : QuickFixesPsiBasedFactory<PsiElement>(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) {
        override fun doCreateQuickFix(psiElement: PsiElement): List<IntentionAction> {
            return listOfNotNull(psiElement.getNonStrictParentOfType<KtWhenExpression>()?.let(::AddWhenElseBranchFix))
        }
    }
}