Added inspection based on IfNullToElvisIntention + moved intention based inspections into one file

This commit is contained in:
Valentin Kipyatkov
2015-04-29 15:02:45 +03:00
parent 8d887f8780
commit 186dd5454d
18 changed files with 86 additions and 128 deletions
@@ -1,26 +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.inspections
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArguments
import org.jetbrains.kotlin.psi.JetTypeArgumentList
import com.intellij.codeInspection.ProblemHighlightType
public class RemoveExplicitTypeArgsInspection : IntentionBasedInspection<JetTypeArgumentList>(RemoveExplicitTypeArguments()) {
override val problemHighlightType: ProblemHighlightType
get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL
}
@@ -1,22 +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.inspections
import org.jetbrains.kotlin.idea.intentions.SimplifyNegatedBinaryExpressionIntention
import org.jetbrains.kotlin.psi.JetPrefixExpression
public class SimplifyBinaryNegationInspection : IntentionBasedInspection<JetPrefixExpression>(SimplifyNegatedBinaryExpressionIntention())
@@ -0,0 +1 @@
val result = ... ?: return
@@ -0,0 +1,2 @@
val result = ...
if (result == null) return
@@ -0,0 +1,5 @@
<html>
<body>
Converts an if expression checking variable being null right after initializing it to an elvis operator in the initializer (if applicable)
</body>
</html>
+12 -5
View File
@@ -868,35 +868,42 @@
<category>Kotlin</category> <category>Kotlin</category>
</intentionAction> </intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ExplicitGetInspection" <localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ExplicitGetInspection"
displayName="Explicit 'get'" displayName="Explicit 'get'"
groupName="Kotlin" groupName="Kotlin"
enabledByDefault="false" enabledByDefault="false"
level="WEAK WARNING" level="WEAK WARNING"
/> />
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.IfThenToElvisInspection" <localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.IfThenToElvisInspection"
displayName="If-Then foldable to '?:'" displayName="If-Then foldable to '?:'"
groupName="Kotlin" groupName="Kotlin"
enabledByDefault="true" enabledByDefault="true"
level="WEAK WARNING" level="WEAK WARNING"
/> />
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.IfThenToSafeAccessInspection" <localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.IfThenToSafeAccessInspection"
displayName="If-Then foldable to '?.'" displayName="If-Then foldable to '?.'"
groupName="Kotlin" groupName="Kotlin"
enabledByDefault="true" enabledByDefault="true"
level="WEAK WARNING" level="WEAK WARNING"
/> />
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.RemoveExplicitTypeArgsInspection" <localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.IfNullToElvisInspection"
displayName="If-Null return/break/... foldable to '?:'"
groupName="Kotlin"
enabledByDefault="true"
level="WEAK WARNING"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArgsInspection"
displayName="Type arguments are unnecessary" displayName="Type arguments are unnecessary"
groupName="Kotlin" groupName="Kotlin"
enabledByDefault="true" enabledByDefault="true"
level="WEAK WARNING" level="WEAK WARNING"
/> />
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.SimplifyBinaryNegationInspection" <localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.SimplifyBinaryNegationInspection"
displayName="Simplify Negated Binary Expression" displayName="Simplify Negated Binary Expression"
groupName="Kotlin" groupName="Kotlin"
enabledByDefault="true" enabledByDefault="true"
@@ -1,22 +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.inspections
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.idea.intentions.attributeCallReplacements.ReplaceGetIntention
public class ExplicitGetInspection : IntentionBasedInspection<JetDotQualifiedExpression>(ReplaceGetIntention())
@@ -1,22 +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.inspections
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToElvisIntention
import org.jetbrains.kotlin.psi.JetIfExpression
public class IfThenToElvisInspection : IntentionBasedInspection<JetIfExpression>(IfThenToElvisIntention())
@@ -1,22 +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.inspections
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToSafeAccessIntention
import org.jetbrains.kotlin.psi.JetIfExpression
public class IfThenToSafeAccessInspection : IntentionBasedInspection<JetIfExpression>(IfThenToSafeAccessIntention())
@@ -19,25 +19,26 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiComment import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.PsiExpression
import com.intellij.psi.PsiRecursiveElementVisitor import com.intellij.psi.PsiRecursiveElementVisitor
import org.jetbrains.kotlin.JetNodeTypes import org.jetbrains.kotlin.JetNodeTypes
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.util.isNothing import org.jetbrains.kotlin.idea.util.isNothing
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.replaced import org.jetbrains.kotlin.psi.psiUtil.replaced
import org.jetbrains.kotlin.psi.psiUtil.siblings import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import java.util.ArrayList import java.util.ArrayList
public class IfNullToElvisIntention : JetSelfTargetingIntention<JetIfExpression>(javaClass(), "Replace 'if' with elvis operator"){ public class IfNullToElvisIntention : JetSelfTargetingOffsetIndependentIntention<JetIfExpression>(javaClass(), "Replace 'if' with elvis operator"){
override fun isApplicableTo(element: JetIfExpression, caretOffset: Int): Boolean { override fun isApplicableTo(element: JetIfExpression): Boolean {
//TODO: range!
val data = calcData(element) ?: return false val data = calcData(element) ?: return false
val type = data.ifNullExpression.analyze().getType(data.ifNullExpression) ?: return false val type = data.ifNullExpression.analyze().getType(data.ifNullExpression) ?: return false
if (!type.isNothing()) return false if (!type.isNothing()) return false
//TODO: what if implicit var's type will change?
return true return true
} }
@@ -72,6 +73,7 @@ public class IfNullToElvisIntention : JetSelfTargetingIntention<JetIfExpression>
if (ifExpression.getElse() != null) return null if (ifExpression.getElse() != null) return null
val binaryExpression = ifExpression.getCondition() as? JetBinaryExpression ?: return null val binaryExpression = ifExpression.getCondition() as? JetBinaryExpression ?: return null
if (binaryExpression.getOperationToken() != JetTokens.EQEQ) return null
if (binaryExpression.getRight()?.getNode()?.getElementType() != JetNodeTypes.NULL) return null if (binaryExpression.getRight()?.getNode()?.getElementType() != JetNodeTypes.NULL) return null
val left = binaryExpression.getLeft() as? JetSimpleNameExpression ?: return null val left = binaryExpression.getLeft() as? JetSimpleNameExpression ?: return null
@@ -0,0 +1,43 @@
/*
* 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
import com.intellij.codeInspection.ProblemHighlightType
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.idea.intentions.attributeCallReplacements.ReplaceGetIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToElvisIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToSafeAccessIntention
import org.jetbrains.kotlin.psi.JetDotQualifiedExpression
import org.jetbrains.kotlin.psi.JetIfExpression
import org.jetbrains.kotlin.psi.JetPrefixExpression
import org.jetbrains.kotlin.psi.JetTypeArgumentList
public class ExplicitGetInspection : IntentionBasedInspection<JetDotQualifiedExpression>(ReplaceGetIntention())
public class IfThenToElvisInspection : IntentionBasedInspection<JetIfExpression>(IfThenToElvisIntention())
public class IfThenToSafeAccessInspection : IntentionBasedInspection<JetIfExpression>(IfThenToSafeAccessIntention())
public class IfNullToElvisInspection : IntentionBasedInspection<JetIfExpression>(IfNullToElvisIntention())
public class RemoveExplicitTypeArgsInspection : IntentionBasedInspection<JetTypeArgumentList>(RemoveExplicitTypeArguments()) {
override val problemHighlightType: ProblemHighlightType
get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL
}
public class SimplifyBinaryNegationInspection : IntentionBasedInspection<JetPrefixExpression>(SimplifyNegatedBinaryExpressionIntention())
@@ -1 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.ExplicitGetInspection // INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.ExplicitGetInspection
@@ -1 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.IfThenToElvisInspection // INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.IfThenToElvisInspection
@@ -1 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.IfThenToSafeAccessInspection // INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.IfThenToSafeAccessInspection
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
fun foo(p: List<String?>): Int {
val v = p[0]
<caret>if (v != null) return -1
return 0
}
@@ -1 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.RemoveExplicitTypeArgsInspection // INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArgsInspection
@@ -1 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.SimplifyBinaryNegationInspection // INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.SimplifyBinaryNegationInspection
@@ -4132,6 +4132,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("IfNotNull.kt")
public void testIfNotNull() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/ifNullToElvis/IfNotNull.kt");
doTest(fileName);
}
@TestMetadata("MultiStatementBlock.kt") @TestMetadata("MultiStatementBlock.kt")
public void testMultiStatementBlock() throws Exception { public void testMultiStatementBlock() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/ifNullToElvis/MultiStatementBlock.kt"); String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/ifNullToElvis/MultiStatementBlock.kt");