Handling safe calls of multi-statement function
This commit is contained in:
+21
-9
@@ -101,17 +101,29 @@ internal fun MutableReplacementCode.introduceValue(
|
||||
}
|
||||
}
|
||||
else {
|
||||
//TODO: handle mainExpression == null and statementsBefore!
|
||||
val useIt = !isNameUsed("it")
|
||||
val name = if (useIt) Name.identifier("it") else suggestName { !isNameUsed(it) }
|
||||
replaceUsages(name)
|
||||
|
||||
mainExpression = if (!isNameUsed("it")) {
|
||||
replaceUsages(Name.identifier("it"))
|
||||
psiFactory.createExpressionByPattern("$0?.let { $1 }", value, mainExpression!!)
|
||||
}
|
||||
else {
|
||||
val name = suggestName { !isNameUsed(it) }
|
||||
replaceUsages(name)
|
||||
psiFactory.createExpressionByPattern("$0?.let { $1 -> $2 }", value, name, mainExpression!!)
|
||||
mainExpression = psiFactory.buildExpression {
|
||||
appendExpression(value)
|
||||
appendFixedText("?.let{")
|
||||
|
||||
if (!useIt) {
|
||||
appendName(name)
|
||||
appendFixedText("->")
|
||||
}
|
||||
|
||||
for (statement in statementsBefore) {
|
||||
appendExpression(statement)
|
||||
appendFixedText("\n")
|
||||
}
|
||||
|
||||
appendExpression(mainExpression!!) //TODO: mainExpression == null
|
||||
|
||||
appendFixedText("}")
|
||||
}
|
||||
statementsBefore.clear() //TODO: postInsertActions!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fun String.<caret>f(p: Int): Int {
|
||||
println(p)
|
||||
return hashCode() * p
|
||||
}
|
||||
|
||||
fun f(s: String?) {
|
||||
s?.f(1)
|
||||
s?.substring(1)?.f(2)
|
||||
val s1 = s?.f(3)
|
||||
val s2 = s?.substring(1)?.f(4)
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fun f(s: String?) {
|
||||
if (s != null) {
|
||||
println(1)
|
||||
s.hashCode() * 1
|
||||
}
|
||||
val substring = s?.substring(1)
|
||||
if (substring != null) {
|
||||
println(2)
|
||||
substring.hashCode() * 2
|
||||
}
|
||||
val s1 = s?.let {
|
||||
println(3)
|
||||
it.hashCode() * 3
|
||||
}
|
||||
val s2 = s?.substring(1)?.let {
|
||||
println(4)
|
||||
it.hashCode() * 4
|
||||
}
|
||||
}
|
||||
@@ -138,6 +138,12 @@ public class InlineTestGenerated extends AbstractInlineTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("SafeCall.kt")
|
||||
public void testSafeCall() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/returnAtEnd/SafeCall.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("SingleStatement.kt")
|
||||
public void testSingleStatement() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/function/returnAtEnd/SingleStatement.kt");
|
||||
|
||||
Reference in New Issue
Block a user