Function <-> Property Intention: Fix conversion of recursive functions/property accessors

#KT-7823 Fixed
This commit is contained in:
Alexey Sedunov
2015-05-26 14:25:06 +03:00
parent 5afc6235cc
commit cb9b1019b2
7 changed files with 27 additions and 5 deletions
@@ -160,14 +160,14 @@ public class ConvertFunctionToPropertyIntention : JetSelfTargetingIntention<JetN
project.executeWriteCommand(getText()) {
val psiFactory = JetPsiFactory(project)
kotlinCalls.forEach { it.replace(it.getCalleeExpression()!!) }
foreignRefs.forEach { it.handleElementRename(getterName) }
callables.forEach {
when (it) {
is JetNamedFunction -> convertFunction(it, psiFactory)
is PsiMethod -> it.setName(getterName)
}
}
kotlinCalls.forEach { it.replace(it.getCalleeExpression()) }
foreignRefs.forEach { it.handleElementRename(getterName) }
ShortenReferences.DEFAULT.process(elementsToShorten)
}
@@ -153,15 +153,15 @@ public class ConvertPropertyToFunctionIntention : JetSelfTargetingIntention<JetP
val kotlinPsiFactory = JetPsiFactory(project)
val javaPsiFactory = PsiElementFactory.SERVICE.getInstance(project)
kotlinRefs.forEach { it.replace(kotlinPsiFactory.createExpressionByPattern("$0()", it)) }
foreignRefsToRename.forEach { it.handleElementRename(propertyName) }
javaRefsToReplaceWithCall.forEach { it.replace(javaPsiFactory.createExpressionFromText(it.getText() + "()", null)) }
callables.forEach {
when (it) {
is JetProperty -> convertProperty(it, kotlinPsiFactory)
is PsiMethod -> it.setName(propertyName)
}
}
kotlinRefs.forEach { it.replace(kotlinPsiFactory.createExpressionByPattern("$0()", it)) }
foreignRefsToRename.forEach { it.handleElementRename(propertyName) }
javaRefsToReplaceWithCall.forEach { it.replace(javaPsiFactory.createExpressionFromText(it.getText() + "()", null)) }
}
}
}
@@ -0,0 +1,2 @@
// WITH_RUNTIME
fun String.<caret>foo(): String = if (isEmpty()) "" else substring(1).foo()
@@ -0,0 +1,3 @@
// WITH_RUNTIME
val String.foo: String
get() = if (isEmpty()) "" else substring(1).foo
@@ -0,0 +1,3 @@
// WITH_RUNTIME
val String.<caret>foo: String
get() = if (isEmpty()) "" else substring(1).foo
@@ -0,0 +1,2 @@
// WITH_RUNTIME
fun String.foo(): String = if (isEmpty()) "" else substring(1).foo()
@@ -2926,6 +2926,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("recursiveFunction.kt")
public void testRecursiveFunction() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertFunctionToProperty/recursiveFunction.kt");
doTest(fileName);
}
@TestMetadata("typeArgumentsConflict.kt")
public void testTypeArgumentsConflict() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertFunctionToProperty/typeArgumentsConflict.kt");
@@ -3349,6 +3355,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("recursiveAccessor.kt")
public void testRecursiveAccessor() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyToFunction/recursiveAccessor.kt");
doTest(fileName);
}
@TestMetadata("unchangedElements.kt")
public void testUnchangedElements() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyToFunction/unchangedElements.kt");