Refactoring: "introduce when subject" is now an inspection

This commit is contained in:
Mikhail Glukhikh
2017-12-19 16:58:04 +03:00
parent a8b01a6b00
commit 6d4b5bc48f
29 changed files with 136 additions and 151 deletions
@@ -1,11 +0,0 @@
when (n) {
1 -> {
res = "one"
}
2 -> {
res = "two"
}
else -> {
res = "???"
}
}
@@ -1,11 +0,0 @@
when {
n == 1 -> {
res = "one"
}
n == 2 -> {
res = "two"
}
else -> {
res = "???"
}
}
@@ -1,5 +0,0 @@
<html>
<body>
This intention introduces argument into 'when' expression simplifying conditions of its branches
</body>
</html>
+1 -6
View File
@@ -902,11 +902,6 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IntroduceWhenSubjectIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.EliminateWhenSubjectIntention</className>
<category>Kotlin</category>
@@ -1675,7 +1670,7 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IntroduceWhenSubjectInspection"
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.branchedTransformations.IntroduceWhenSubjectInspection"
displayName="'when' that can be simplified by introducing an argument"
groupPath="Kotlin"
groupName="Style issues"
@@ -0,0 +1,56 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.inspections.branchedTransformations
import com.intellij.codeInspection.LocalInspectionToolSession
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.inspections.AbstractApplicabilityBasedInspection
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.getSubjectToIntroduce
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceSubject
import org.jetbrains.kotlin.psi.KtVisitorVoid
import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
class IntroduceWhenSubjectInspection : AbstractApplicabilityBasedInspection<KtWhenExpression>() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): KtVisitorVoid =
object : KtVisitorVoid() {
override fun visitWhenExpression(expression: KtWhenExpression) {
super.visitWhenExpression(expression)
visitTargetElement(expression, holder, isOnTheFly)
}
}
override fun isApplicable(element: KtWhenExpression) = element.getSubjectToIntroduce() != null
override fun inspectionTarget(element: KtWhenExpression) = element.whenKeyword
override fun inspectionText(element: KtWhenExpression) = "'when' with argument should be used"
override val defaultFixText = "Introduce 'when' argument"
override fun fixText(element: KtWhenExpression): String {
val subject = element.getSubjectToIntroduce() ?: return ""
return "Introduce '${subject.text}' as argument to 'when'"
}
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
element.getParentOfType<KtWhenExpression>(strict = true)?.introduceSubject()
}
}
@@ -1,39 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.getSubjectToIntroduce
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceSubject
import org.jetbrains.kotlin.psi.KtWhenExpression
class IntroduceWhenSubjectInspection : IntentionBasedInspection<KtWhenExpression>(IntroduceWhenSubjectIntention::class)
class IntroduceWhenSubjectIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenExpression::class.java, "Introduce argument to 'when'") {
override fun applicabilityRange(element: KtWhenExpression): TextRange? {
val subject = element.getSubjectToIntroduce() ?: return null
text = "Introduce '${subject.text}' as argument to 'when'"
return element.whenKeyword.textRange
}
override fun applyTo(element: KtWhenExpression, editor: Editor?) {
element.introduceSubject()
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.branchedTransformations.IntroduceWhenSubjectInspection
@@ -1,4 +1,4 @@
//IS_APPLICABLE: false
// PROBLEM: none
fun test(n: Int): String {
return <caret>when {
n in 0..10 -> "small"
@@ -1,4 +1,4 @@
//IS_APPLICABLE: false
// PROBLEM: none
fun test(n: Int): String {
return <caret>when(n) {
in 0..10 -> "small"
@@ -1,4 +1,4 @@
//IS_APPLICABLE: false
// PROBLEM: none
fun test(n: Int): String {
return <caret>when {
n in 0..10 -> "n is small"
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IntroduceWhenSubjectIntention
@@ -349,6 +349,81 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
doTest(fileName);
}
}
@TestMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class IntroduceWhenSubject extends AbstractLocalInspectionTest {
public void testAllFilesPresentInIntroduceWhenSubject() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/branched/introduceWhenSubject"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("lineBreaksAndComments.kt")
public void testLineBreaksAndComments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/lineBreaksAndComments.kt");
doTest(fileName);
}
@TestMetadata("whenWithEqualityTests.kt")
public void testWhenWithEqualityTests() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithEqualityTests.kt");
doTest(fileName);
}
@TestMetadata("whenWithMultipleConditionTypes.kt")
public void testWhenWithMultipleConditionTypes() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithMultipleConditionTypes.kt");
doTest(fileName);
}
@TestMetadata("whenWithNegativePatterns.kt")
public void testWhenWithNegativePatterns() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithNegativePatterns.kt");
doTest(fileName);
}
@TestMetadata("whenWithNegativeRangeTests.kt")
public void testWhenWithNegativeRangeTests() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithNegativeRangeTests.kt");
doTest(fileName);
}
@TestMetadata("whenWithNondivisibleConditions.kt")
public void testWhenWithNondivisibleConditions() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithNondivisibleConditions.kt");
doTest(fileName);
}
@TestMetadata("whenWithPatterns.kt")
public void testWhenWithPatterns() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithPatterns.kt");
doTest(fileName);
}
@TestMetadata("whenWithRangeTests.kt")
public void testWhenWithRangeTests() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithRangeTests.kt");
doTest(fileName);
}
@TestMetadata("whenWithSubject.kt")
public void testWhenWithSubject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithSubject.kt");
doTest(fileName);
}
@TestMetadata("whenWithSwappedEqualityTests.kt")
public void testWhenWithSwappedEqualityTests() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithSwappedEqualityTests.kt");
doTest(fileName);
}
@TestMetadata("whenWithUnmatchedCandidateSubjects.kt")
public void testWhenWithUnmatchedCandidateSubjects() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/branched/introduceWhenSubject/whenWithUnmatchedCandidateSubjects.kt");
doTest(fileName);
}
}
}
@TestMetadata("idea/testData/inspectionsLocal/canBeVal")
@@ -2593,81 +2593,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/branched/when/introduceSubject")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class IntroduceSubject extends AbstractIntentionTest {
public void testAllFilesPresentInIntroduceSubject() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/branched/when/introduceSubject"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("lineBreaksAndComments.kt")
public void testLineBreaksAndComments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/lineBreaksAndComments.kt");
doTest(fileName);
}
@TestMetadata("whenWithEqualityTests.kt")
public void testWhenWithEqualityTests() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithEqualityTests.kt");
doTest(fileName);
}
@TestMetadata("whenWithMultipleConditionTypes.kt")
public void testWhenWithMultipleConditionTypes() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithMultipleConditionTypes.kt");
doTest(fileName);
}
@TestMetadata("whenWithNegativePatterns.kt")
public void testWhenWithNegativePatterns() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithNegativePatterns.kt");
doTest(fileName);
}
@TestMetadata("whenWithNegativeRangeTests.kt")
public void testWhenWithNegativeRangeTests() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithNegativeRangeTests.kt");
doTest(fileName);
}
@TestMetadata("whenWithNondivisibleConditions.kt")
public void testWhenWithNondivisibleConditions() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithNondivisibleConditions.kt");
doTest(fileName);
}
@TestMetadata("whenWithPatterns.kt")
public void testWhenWithPatterns() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithPatterns.kt");
doTest(fileName);
}
@TestMetadata("whenWithRangeTests.kt")
public void testWhenWithRangeTests() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithRangeTests.kt");
doTest(fileName);
}
@TestMetadata("whenWithSubject.kt")
public void testWhenWithSubject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithSubject.kt");
doTest(fileName);
}
@TestMetadata("whenWithSwappedEqualityTests.kt")
public void testWhenWithSwappedEqualityTests() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithSwappedEqualityTests.kt");
doTest(fileName);
}
@TestMetadata("whenWithUnmatchedCandidateSubjects.kt")
public void testWhenWithUnmatchedCandidateSubjects() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/when/introduceSubject/whenWithUnmatchedCandidateSubjects.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/branched/when/merge")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)