From df4c80f43a8de0980b78b7f13600e61d2c9daebe Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 27 Feb 2018 19:15:14 +0300 Subject: [PATCH] Shorten References: Allow import insertion when function and property have the same name #KT-22745 Fixed --- .../kotlin/idea/core/ShortenReferences.kt | 15 +++++++++++---- .../after/foo/dummy.txt | 0 .../after/foo/name.kt | 3 +++ .../functionAndPropertyWithSameName/after/test.kt | 8 ++++++++ .../before/foo/dummy.txt | 0 .../before/test.kt | 7 +++++++ .../functionAndPropertyWithSameName.test | 5 +++++ .../idea/refactoring/move/MoveTestGenerated.java | 6 ++++++ 8 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/foo/dummy.txt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/foo/name.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/test.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/before/foo/dummy.txt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/before/test.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/functionAndPropertyWithSameName.test diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt index 8037c470d0e..8ecae87ef17 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/ShortenReferences.kt @@ -514,13 +514,20 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT } return when { - targetsMatch || resolvedCallsMatch -> AnalyzeQualifiedElementResult.ShortenNow + targetsMatch || resolvedCallsMatch -> + AnalyzeQualifiedElementResult.ShortenNow - // it makes no sense to insert import when there is a conflict with function, property etc - targetsWhenShort.any { it !is ClassifierDescriptorWithTypeParameters && it !is PackageViewDescriptor } -> AnalyzeQualifiedElementResult.Skip + // Function doesn't conflict with property + targets.all { it is FunctionDescriptor } && targetsWhenShort.all { it is PropertyDescriptor } -> + AnalyzeQualifiedElementResult.ImportDescriptors(targets) + + // In other cases it makes no sense to insert import when there is a conflict with function, property etc + targetsWhenShort.any { it !is ClassifierDescriptorWithTypeParameters && it !is PackageViewDescriptor } -> + AnalyzeQualifiedElementResult.Skip - else -> AnalyzeQualifiedElementResult.ImportDescriptors(targets) + else -> + AnalyzeQualifiedElementResult.ImportDescriptors(targets) } } diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/foo/dummy.txt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/foo/dummy.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/foo/name.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/foo/name.kt new file mode 100644 index 00000000000..7447de6decf --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/foo/name.kt @@ -0,0 +1,3 @@ +package foo + +fun name() {} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/test.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/test.kt new file mode 100644 index 00000000000..162701b8e7a --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/after/test.kt @@ -0,0 +1,8 @@ +import foo.name + +val name = "" + +fun main(args: Array) { + name + name() +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/before/foo/dummy.txt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/before/foo/dummy.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/before/test.kt b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/before/test.kt new file mode 100644 index 00000000000..eeda52d26a9 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/before/test.kt @@ -0,0 +1,7 @@ +val name = "" +fun name() {} + +fun main(args: Array) { + name + name() +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/functionAndPropertyWithSameName.test b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/functionAndPropertyWithSameName.test new file mode 100644 index 00000000000..7d7dd2b8452 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/functionAndPropertyWithSameName.test @@ -0,0 +1,5 @@ +{ + "mainFile": "test.kt", + "type": "MOVE_KOTLIN_TOP_LEVEL_DECLARATIONS", + "targetPackage": "foo" +} 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 e7c696837ef..3811f8f2028 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java @@ -613,6 +613,12 @@ public class MoveTestGenerated extends AbstractMoveTest { doTest(fileName); } + @TestMetadata("kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/functionAndPropertyWithSameName.test") + public void testKotlin_moveTopLevelDeclarations_misc_functionAndPropertyWithSameName_FunctionAndPropertyWithSameName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/functionAndPropertyWithSameName/functionAndPropertyWithSameName.test"); + doTest(fileName); + } + @TestMetadata("kotlin/moveTopLevelDeclarations/misc/internalReferences/internalReferences.test") public void testKotlin_moveTopLevelDeclarations_misc_internalReferences_InternalReferences() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/move/kotlin/moveTopLevelDeclarations/misc/internalReferences/internalReferences.test");