diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt index 71b6a739b80..03a1b9e0e2e 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt @@ -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 + } } } } diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/after/b/boo.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/after/b/boo.kt new file mode 100644 index 00000000000..4ec38afc298 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/after/b/boo.kt @@ -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 } +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/after/b/foo.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/after/b/foo.kt new file mode 100644 index 00000000000..7fd9c9fc037 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/after/b/foo.kt @@ -0,0 +1,4 @@ +package b + +import java.util.function.IntPredicate + diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/before/b/boo.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/before/b/boo.kt new file mode 100644 index 00000000000..e6228fcebd5 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/before/b/boo.kt @@ -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) + } + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/before/b/foo.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/before/b/foo.kt new file mode 100644 index 00000000000..ef1af469f98 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/before/b/foo.kt @@ -0,0 +1,7 @@ +package b + +import java.util.function.IntPredicate + +fun foo(): Factory = Factory { k -> + IntPredicate { n -> n % k == 0 } +} diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/shortenCompanionObject.test b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/shortenCompanionObject.test new file mode 100644 index 00000000000..fe939b98eeb --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/shortenCompanionObject2/shortenCompanionObject.test @@ -0,0 +1,5 @@ +{ + "mainFile": "b/foo.kt", + "type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS", + "targetFile": "b/boo.kt" +} diff --git a/idea/testData/shortenRefs/removeCompanionRefInCalleeExpression.kt b/idea/testData/shortenRefs/removeCompanionRefInCalleeExpression.kt new file mode 100644 index 00000000000..0e7866eda32 --- /dev/null +++ b/idea/testData/shortenRefs/removeCompanionRefInCalleeExpression.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) + } + } +} + +fun foo(): Factory = Factory.Companion { k -> IntPredicate { n -> n % k == 0 } } \ No newline at end of file diff --git a/idea/testData/shortenRefs/removeCompanionRefInCalleeExpression.kt.after b/idea/testData/shortenRefs/removeCompanionRefInCalleeExpression.kt.after new file mode 100644 index 00000000000..85c0344a70f --- /dev/null +++ b/idea/testData/shortenRefs/removeCompanionRefInCalleeExpression.kt.after @@ -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 } } \ No newline at end of file diff --git a/idea/testData/shortenRefs/removeCompanionRefWithQualifiedReceiverInCalleeExpression.kt b/idea/testData/shortenRefs/removeCompanionRefWithQualifiedReceiverInCalleeExpression.kt new file mode 100644 index 00000000000..420f1450552 --- /dev/null +++ b/idea/testData/shortenRefs/removeCompanionRefWithQualifiedReceiverInCalleeExpression.kt @@ -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.Companion { k -> IntPredicate { n -> n % k == 0 } } \ No newline at end of file diff --git a/idea/testData/shortenRefs/removeCompanionRefWithQualifiedReceiverInCalleeExpression.kt.after b/idea/testData/shortenRefs/removeCompanionRefWithQualifiedReceiverInCalleeExpression.kt.after new file mode 100644 index 00000000000..482542121be --- /dev/null +++ b/idea/testData/shortenRefs/removeCompanionRefWithQualifiedReceiverInCalleeExpression.kt.after @@ -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 } } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java index a2cb2752bb7..22ec1f50f6f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java @@ -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"); diff --git a/idea/tests/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java b/idea/tests/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java index abb98d1c75b..2228d01dfd0 100644 --- a/idea/tests/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/shortenRefs/ShortenRefsTestGenerated.java @@ -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)