Add quick-fix for 'is' absence in when #KT-18810 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-05-17 17:51:42 +03:00
committed by Mikhail Glukhikh
parent 2427406a8f
commit c344354395
6 changed files with 90 additions and 0 deletions
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtWhenConditionWithExpression
import org.jetbrains.kotlin.psi.psiUtil.endOffset
class AddIsToWhenConditionFix(
expression: KtWhenConditionWithExpression,
private val referenceText: String
) : KotlinQuickFixAction<KtWhenConditionWithExpression>(expression) {
override fun getText(): String = "Add 'is' before '$referenceText'"
override fun getFamilyName(): String = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val expression = element ?: return
val replaced = expression.replaced(KtPsiFactory(expression).createWhenCondition("is ${expression.text}"))
editor?.caretModel?.moveToOffset(replaced.endOffset)
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtWhenConditionWithExpression>? {
val element = diagnostic.psiElement.parent as? KtWhenConditionWithExpression ?: return null
return AddIsToWhenConditionFix(element, element.text)
}
}
}
@@ -559,5 +559,7 @@ class QuickFixRegistrar : QuickFixContributor {
TYPE_VARIANCE_CONFLICT.registerFactory(RemoveTypeVarianceFix)
CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT.registerFactory(MoveMemberToCompanionObjectIntention)
NO_COMPANION_OBJECT.registerFactory(AddIsToWhenConditionFix)
}
}
+9
View File
@@ -0,0 +1,9 @@
// "Add 'is' before 'Foo'" "true"
class Foo
fun test(a: Any) {
when (a) {
<caret>Foo -> {}
}
}
@@ -0,0 +1,9 @@
// "Add 'is' before 'Foo'" "true"
class Foo
fun test(a: Any) {
when (a) {
is Foo<caret> -> {}
}
}
@@ -151,6 +151,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
}
}
@TestMetadata("idea/testData/quickfix/addIsToWhenCondition")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddIsToWhenCondition extends AbstractQuickFixMultiFileTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInAddIsToWhenCondition() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addIsToWhenCondition"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
}
}
@TestMetadata("idea/testData/quickfix/addJvmDefault")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -686,6 +686,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/addIsToWhenCondition")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddIsToWhenCondition extends AbstractQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInAddIsToWhenCondition() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addIsToWhenCondition"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("class.kt")
public void testClass() throws Exception {
runTest("idea/testData/quickfix/addIsToWhenCondition/class.kt");
}
}
@TestMetadata("idea/testData/quickfix/addJvmDefault")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)