diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/RemoveExplicitTypeArgsInspection.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/RemoveExplicitTypeArgsInspection.kt deleted file mode 100644 index 6a5743ddbd9..00000000000 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/RemoveExplicitTypeArgsInspection.kt +++ /dev/null @@ -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(RemoveExplicitTypeArguments()) { - override val problemHighlightType: ProblemHighlightType - get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL -} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/SimplifyBinaryNegationInspection.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/SimplifyBinaryNegationInspection.kt deleted file mode 100644 index 54b7fb9426e..00000000000 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/SimplifyBinaryNegationInspection.kt +++ /dev/null @@ -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(SimplifyNegatedBinaryExpressionIntention()) diff --git a/idea/resources/intentionDescriptions/IfNullToElvisIntention/after.kt.template b/idea/resources/intentionDescriptions/IfNullToElvisIntention/after.kt.template new file mode 100644 index 00000000000..0bd54fa1404 --- /dev/null +++ b/idea/resources/intentionDescriptions/IfNullToElvisIntention/after.kt.template @@ -0,0 +1 @@ +val result = ... ?: return diff --git a/idea/resources/intentionDescriptions/IfNullToElvisIntention/before.kt.template b/idea/resources/intentionDescriptions/IfNullToElvisIntention/before.kt.template new file mode 100644 index 00000000000..8b869ce9222 --- /dev/null +++ b/idea/resources/intentionDescriptions/IfNullToElvisIntention/before.kt.template @@ -0,0 +1,2 @@ +val result = ... +if (result == null) return diff --git a/idea/resources/intentionDescriptions/IfNullToElvisIntention/description.html b/idea/resources/intentionDescriptions/IfNullToElvisIntention/description.html new file mode 100644 index 00000000000..d4cd9342ff0 --- /dev/null +++ b/idea/resources/intentionDescriptions/IfNullToElvisIntention/description.html @@ -0,0 +1,5 @@ + + + Converts an if expression checking variable being null right after initializing it to an elvis operator in the initializer (if applicable) + + diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 9e6f4a48cd7..ef4a420d259 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -868,35 +868,42 @@ Kotlin - - - - + + - (ReplaceGetIntention()) diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/IfThenToElvisInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/IfThenToElvisInspection.kt deleted file mode 100644 index 180c777671e..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/IfThenToElvisInspection.kt +++ /dev/null @@ -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(IfThenToElvisIntention()) diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/IfThenToSafeAccessInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/IfThenToSafeAccessInspection.kt deleted file mode 100644 index e4762f4a9b1..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/IfThenToSafeAccessInspection.kt +++ /dev/null @@ -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(IfThenToSafeAccessIntention()) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/IfNullToElvisIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/IfNullToElvisIntention.kt index 5f4f576b94a..9b8bd431fe5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/IfNullToElvisIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/IfNullToElvisIntention.kt @@ -19,25 +19,26 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiComment import com.intellij.psi.PsiElement -import com.intellij.psi.PsiExpression import com.intellij.psi.PsiRecursiveElementVisitor import org.jetbrains.kotlin.JetNodeTypes import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.util.isNothing +import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.replaced import org.jetbrains.kotlin.psi.psiUtil.siblings import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import java.util.ArrayList -public class IfNullToElvisIntention : JetSelfTargetingIntention(javaClass(), "Replace 'if' with elvis operator"){ - override fun isApplicableTo(element: JetIfExpression, caretOffset: Int): Boolean { - //TODO: range! +public class IfNullToElvisIntention : JetSelfTargetingOffsetIndependentIntention(javaClass(), "Replace 'if' with elvis operator"){ + override fun isApplicableTo(element: JetIfExpression): Boolean { val data = calcData(element) ?: return false val type = data.ifNullExpression.analyze().getType(data.ifNullExpression) ?: return false if (!type.isNothing()) return false + //TODO: what if implicit var's type will change? + return true } @@ -72,6 +73,7 @@ public class IfNullToElvisIntention : JetSelfTargetingIntention if (ifExpression.getElse() != null) 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 val left = binaryExpression.getLeft() as? JetSimpleNameExpression ?: return null diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/inspections.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/inspections.kt new file mode 100644 index 00000000000..09430dce144 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/inspections.kt @@ -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(ReplaceGetIntention()) + +public class IfThenToElvisInspection : IntentionBasedInspection(IfThenToElvisIntention()) + +public class IfThenToSafeAccessInspection : IntentionBasedInspection(IfThenToSafeAccessIntention()) + +public class IfNullToElvisInspection : IntentionBasedInspection(IfNullToElvisIntention()) + +public class RemoveExplicitTypeArgsInspection : IntentionBasedInspection(RemoveExplicitTypeArguments()) { + override val problemHighlightType: ProblemHighlightType + get() = ProblemHighlightType.LIKE_UNUSED_SYMBOL +} + +public class SimplifyBinaryNegationInspection : IntentionBasedInspection(SimplifyNegatedBinaryExpressionIntention()) + diff --git a/idea/testData/intentions/attributeCallReplacements/replaceGetIntention/inspectionData/inspections.test b/idea/testData/intentions/attributeCallReplacements/replaceGetIntention/inspectionData/inspections.test index 5cee3368289..4b166399990 100644 --- a/idea/testData/intentions/attributeCallReplacements/replaceGetIntention/inspectionData/inspections.test +++ b/idea/testData/intentions/attributeCallReplacements/replaceGetIntention/inspectionData/inspections.test @@ -1 +1 @@ -// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.ExplicitGetInspection +// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.ExplicitGetInspection diff --git a/idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test index 438b9ee2032..29b97c8b3ab 100644 --- a/idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test +++ b/idea/testData/intentions/branched/ifThenToElvis/inspectionData/inspections.test @@ -1 +1 @@ -// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.IfThenToElvisInspection +// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.IfThenToElvisInspection diff --git a/idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test b/idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test index 6281142229e..7e465eb8430 100644 --- a/idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test +++ b/idea/testData/intentions/branched/ifThenToSafeAccess/inspectionData/inspections.test @@ -1 +1 @@ -// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.IfThenToSafeAccessInspection +// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.IfThenToSafeAccessInspection diff --git a/idea/testData/intentions/ifNullToElvis/IfNotNull.kt b/idea/testData/intentions/ifNullToElvis/IfNotNull.kt new file mode 100644 index 00000000000..ca93bd96744 --- /dev/null +++ b/idea/testData/intentions/ifNullToElvis/IfNotNull.kt @@ -0,0 +1,6 @@ +// IS_APPLICABLE: false +fun foo(p: List): Int { + val v = p[0] + if (v != null) return -1 + return 0 +} \ No newline at end of file diff --git a/idea/testData/intentions/removeExplicitTypeArguments/inspectionData/inspections.test b/idea/testData/intentions/removeExplicitTypeArguments/inspectionData/inspections.test index c614fb083cc..ffe5a260518 100644 --- a/idea/testData/intentions/removeExplicitTypeArguments/inspectionData/inspections.test +++ b/idea/testData/intentions/removeExplicitTypeArguments/inspectionData/inspections.test @@ -1 +1 @@ -// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.RemoveExplicitTypeArgsInspection +// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArgsInspection diff --git a/idea/testData/intentions/simplifyNegatedBinaryExpression/inspectionData/inspections.test b/idea/testData/intentions/simplifyNegatedBinaryExpression/inspectionData/inspections.test index 345cf87a012..9b60db23f62 100644 --- a/idea/testData/intentions/simplifyNegatedBinaryExpression/inspectionData/inspections.test +++ b/idea/testData/intentions/simplifyNegatedBinaryExpression/inspectionData/inspections.test @@ -1 +1 @@ -// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.SimplifyBinaryNegationInspection +// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.SimplifyBinaryNegationInspection diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 67441fd1573..af30eb109ed 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -4132,6 +4132,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { 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") public void testMultiStatementBlock() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/ifNullToElvis/MultiStatementBlock.kt");