if then to elvis: propose transformation to 'let' #KT-26653 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
539c55c5b2
commit
1c8e75eb34
+9
-7
@@ -93,12 +93,14 @@ class IfThenToSafeAccessInspection : AbstractApplicabilityBasedInspection<KtIfEx
|
|||||||
else -> false
|
else -> false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtSafeQualifiedExpression.renameLetParameter(editor: Editor) {
|
companion object {
|
||||||
val callExpression = selectorExpression as? KtCallExpression ?: return
|
internal fun KtSafeQualifiedExpression.renameLetParameter(editor: Editor) {
|
||||||
if (callExpression.calleeExpression?.text != "let") return
|
val callExpression = selectorExpression as? KtCallExpression ?: return
|
||||||
val parameter = callExpression.lambdaArguments.singleOrNull()?.getLambdaExpression()?.valueParameters?.singleOrNull() ?: return
|
if (callExpression.calleeExpression?.text != "let") return
|
||||||
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document)
|
val parameter = callExpression.lambdaArguments.singleOrNull()?.getLambdaExpression()?.valueParameters?.singleOrNull() ?: return
|
||||||
editor.caretModel.moveToOffset(parameter.startOffset)
|
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.document)
|
||||||
KotlinVariableInplaceRenameHandler().doRename(parameter, editor, null)
|
editor.caretModel.moveToOffset(parameter.startOffset)
|
||||||
|
KotlinVariableInplaceRenameHandler().doRename(parameter, editor, null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
|||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.core.replaced
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||||
|
import org.jetbrains.kotlin.idea.inspections.branchedTransformations.IfThenToSafeAccessInspection
|
||||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingOffsetIndependentIntention
|
import org.jetbrains.kotlin.idea.intentions.SelfTargetingOffsetIndependentIntention
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.*
|
||||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||||
@@ -54,6 +55,8 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
|
|||||||
false
|
false
|
||||||
baseClause.evaluatesTo(receiverExpression) ->
|
baseClause.evaluatesTo(receiverExpression) ->
|
||||||
true
|
true
|
||||||
|
baseClause.anyArgumentEvaluatesTo(receiverExpression) ->
|
||||||
|
true
|
||||||
hasImplicitReceiverReplaceableBySafeCall() || baseClause.hasFirstReceiverOf(receiverExpression) ->
|
hasImplicitReceiverReplaceableBySafeCall() || baseClause.hasFirstReceiverOf(receiverExpression) ->
|
||||||
!baseClause.hasNullableType(context)
|
!baseClause.hasNullableType(context)
|
||||||
else ->
|
else ->
|
||||||
@@ -95,6 +98,9 @@ class IfThenToElvisIntention : SelfTargetingOffsetIndependentIntention<KtIfExpre
|
|||||||
|
|
||||||
if (editor != null) {
|
if (editor != null) {
|
||||||
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
|
elvis.inlineLeftSideIfApplicableWithPrompt(editor)
|
||||||
|
with(IfThenToSafeAccessInspection) {
|
||||||
|
(elvis.left as? KtSafeQualifiedExpression)?.renameLetParameter(editor)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo(s: String?) {
|
||||||
|
val x = <caret>if (s != null) {
|
||||||
|
bar(s)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
13
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bar(s: String): Int = 42
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo(s: String?) {
|
||||||
|
val x = s?.let { bar(it) } ?: 13
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bar(s: String): Int = 42
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo(it: String?) {
|
||||||
|
val x = <caret>if (it != null) {
|
||||||
|
bar(it)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
13
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bar(s: String): Int = 42
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo(it: String?) {
|
||||||
|
val x = it?.let { it1 -> bar(it1) } ?: 13
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bar(s: String): Int = 42
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
// IS_APPLICABLE: false
|
|
||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|
||||||
class Test {
|
class Test {
|
||||||
@@ -6,9 +5,8 @@ class Test {
|
|||||||
return param1
|
return param1
|
||||||
}
|
}
|
||||||
|
|
||||||
fun doAThingIfPresent(param1: String?) {
|
fun doAThingIfPresent(param1: String?): String {
|
||||||
// In theory could propose transformation to 'let'
|
return <caret>if (param1 != null) {
|
||||||
<caret>if (param1 != null) {
|
|
||||||
doAThing(param1)
|
doAThing(param1)
|
||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
|
|||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
fun doAThing(param1: String): String {
|
||||||
|
return param1
|
||||||
|
}
|
||||||
|
|
||||||
|
fun doAThingIfPresent(param1: String?): String {
|
||||||
|
return param1?.let { doAThing(it) } ?: ""
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-4
@@ -1,4 +1,3 @@
|
|||||||
// IS_APPLICABLE: false
|
|
||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
|
|
||||||
class Test {
|
class Test {
|
||||||
@@ -6,9 +5,8 @@ class Test {
|
|||||||
return param1
|
return param1
|
||||||
}
|
}
|
||||||
|
|
||||||
fun doAThingIfPresent(param1: String?) {
|
fun doAThingIfPresent(param1: Any?): String {
|
||||||
// In theory could propose transformation to 'let'
|
return <caret>if (param1 is String) {
|
||||||
<caret>if (param1 is String) {
|
|
||||||
doAThing(param1)
|
doAThing(param1)
|
||||||
} else {
|
} else {
|
||||||
""
|
""
|
||||||
|
|||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
fun doAThing(param1: String): String {
|
||||||
|
return param1
|
||||||
|
}
|
||||||
|
|
||||||
|
fun doAThingIfPresent(param1: Any?): String {
|
||||||
|
return (param1 as? String)?.let { doAThing(it) } ?: ""
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2441,6 +2441,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/branched/ifThenToElvis/otherBlockHasMoreThanOneStatement.kt");
|
runTest("idea/testData/intentions/branched/ifThenToElvis/otherBlockHasMoreThanOneStatement.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("replaceWithLet.kt")
|
||||||
|
public void testReplaceWithLet() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/branched/ifThenToElvis/replaceWithLet.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("replaceWithLetAndRenameIt.kt")
|
||||||
|
public void testReplaceWithLetAndRenameIt() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/branched/ifThenToElvis/replaceWithLetAndRenameIt.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("replaceWithLetInMember.kt")
|
@TestMetadata("replaceWithLetInMember.kt")
|
||||||
public void testReplaceWithLetInMember() throws Exception {
|
public void testReplaceWithLetInMember() throws Exception {
|
||||||
runTest("idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMember.kt");
|
runTest("idea/testData/intentions/branched/ifThenToElvis/replaceWithLetInMember.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user