Shorten References: Fix removal of explicit companion references in the position of callee expression

#KT-10102
This commit is contained in:
Alexey Sedunov
2015-11-27 16:04:53 +03:00
parent 2dcc38b92f
commit 44bc937499
12 changed files with 131 additions and 1 deletions
@@ -464,7 +464,25 @@ public class ShortenReferences(val options: (KtElement) -> Options = { Options.D
}
override fun shortenElement(element: KtDotQualifiedExpression): KtElement {
return element.replace(element.getReceiverExpression()) as KtElement
val receiver = element.receiverExpression
val selector = element.selectorExpression ?: return element
return when (receiver) {
is KtSimpleNameExpression -> {
val identifier = receiver.getIdentifier() ?: return element
(selector.getCalleeExpressionIfAny() as? KtSimpleNameExpression)?.getIdentifier()?.replace(identifier)
element.replace(selector) as KtExpression
}
is KtQualifiedExpression -> {
val identifier = (receiver.selectorExpression as? KtSimpleNameExpression)?.getIdentifier() ?: return element
(selector.getCalleeExpressionIfAny() as? KtSimpleNameExpression)?.getIdentifier()?.replace(identifier)
receiver.selectorExpression?.replace(selector)
element.replace(receiver) as KtExpression
}
else -> element
}
}
}
}
@@ -0,0 +1,17 @@
package b
import java.util.function.IntPredicate
interface Factory {
operator fun invoke(i: Int): IntPredicate
companion object {
inline operator fun invoke(crossinline f: (Int) -> IntPredicate) = object : Factory {
override fun invoke(i: Int) = f(i)
}
}
}
fun foo(): Factory = Factory { k ->
IntPredicate { n -> n % k == 0 }
}
@@ -0,0 +1,4 @@
package b
import java.util.function.IntPredicate
@@ -0,0 +1,13 @@
package b
import java.util.function.IntPredicate
interface Factory {
operator fun invoke(i: Int): IntPredicate
companion object {
inline operator fun invoke(crossinline f: (Int) -> IntPredicate) = object : Factory {
override fun invoke(i: Int) = f(i)
}
}
}
@@ -0,0 +1,7 @@
package b
import java.util.function.IntPredicate
fun <caret>foo(): Factory = Factory { k ->
IntPredicate { n -> n % k == 0 }
}
@@ -0,0 +1,5 @@
{
"mainFile": "b/foo.kt",
"type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS",
"targetFile": "b/boo.kt"
}
@@ -0,0 +1,11 @@
interface Factory {
operator fun invoke(i: Int): IntPredicate
companion object {
inline operator fun invoke(crossinline f: (Int) -> IntPredicate) = object : Factory {
override fun invoke(i: Int) = f(i)
}
}
}
<selection>fun foo(): Factory = Factory.Companion { k -> IntPredicate { n -> n % k == 0 } }</selection>
@@ -0,0 +1,11 @@
interface Factory {
operator fun invoke(i: Int): IntPredicate
companion object {
inline operator fun invoke(crossinline f: (Int) -> IntPredicate) = object : Factory {
override fun invoke(i: Int) = f(i)
}
}
}
fun foo(): Factory = Factory { k -> IntPredicate { n -> n % k == 0 } }
@@ -0,0 +1,13 @@
class T {
interface Factory {
operator fun invoke(i: Int): IntPredicate
companion object {
inline operator fun invoke(crossinline f: (Int) -> IntPredicate) = object : Factory {
override fun invoke(i: Int) = f(i)
}
}
}
}
<selection>fun foo(): T.Factory = T.Factory.Companion { k -> IntPredicate { n -> n % k == 0 } }</selection>
@@ -0,0 +1,13 @@
class T {
interface Factory {
operator fun invoke(i: Int): IntPredicate
companion object {
inline operator fun invoke(crossinline f: (Int) -> IntPredicate) = object : Factory {
override fun invoke(i: Int) = f(i)
}
}
}
}
fun foo(): T.Factory = T.Factory { k -> IntPredicate { n -> n % k == 0 } }
@@ -329,6 +329,12 @@ public class MoveTestGenerated extends AbstractMoveTest {
doTest(fileName);
}
@TestMetadata("kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/shortenCompanionObject.test")
public void testKotlin_moveTopLevelDeclarations_misc_shortenCompanionObject2_ShortenCompanionObject() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/shortenCompanionObject.test");
doTest(fileName);
}
@TestMetadata("kotlin/moveTopLevelDeclarations/misc/singletonsAndStatics/singletonsAndStatics.test")
public void testKotlin_moveTopLevelDeclarations_misc_singletonsAndStatics_SingletonsAndStatics() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/singletonsAndStatics/singletonsAndStatics.test");
@@ -83,6 +83,18 @@ public class ShortenRefsTestGenerated extends AbstractShortenRefsTest {
doTest(fileName);
}
@TestMetadata("removeCompanionRefInCalleeExpression.kt")
public void testRemoveCompanionRefInCalleeExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/removeCompanionRefInCalleeExpression.kt");
doTest(fileName);
}
@TestMetadata("removeCompanionRefWithQualifiedReceiverInCalleeExpression.kt")
public void testRemoveCompanionRefWithQualifiedReceiverInCalleeExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/shortenRefs/removeCompanionRefWithQualifiedReceiverInCalleeExpression.kt");
doTest(fileName);
}
@TestMetadata("idea/testData/shortenRefs/constructor")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)