From 0c1d25d5ac1aa6bbcd2ce5c8dc036f4fac663ab3 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Tue, 7 Aug 2018 20:46:13 +0200 Subject: [PATCH] Completion: when Unit is expected do not prioritize by return type Decide on completion order by other factors Previously would prefer callable that return Unit if Unit is the expected type leading to strange completion order #KT-25588 Fixed --- .../kotlin/generators/tests/GenerateTests.kt | 2 +- .../generators/tests/GenerateTests.kt.as31 | 2 +- .../generators/tests/GenerateTests.kt.as32 | 2 +- .../generators/tests/GenerateTests.kt.as33 | 2 +- .../jetbrains/kotlin/idea/util/FuzzyType.kt | 1 + .../testData/weighers/basic/DslCalls.kt | 2 +- .../basic/DslCallsAnnotatedFunctionType.kt | 2 +- .../basic/DslCallsWithMultipleReceivers.kt | 2 +- .../testData/weighers/basic/DslMemberCalls.kt | 4 +-- .../testData/weighers/basic/KT-25588_1.kts | 31 ++++++++++++++++++ .../testData/weighers/basic/KT-25588_2.kts | 32 +++++++++++++++++++ .../testData/weighers/basic/NoExpectedType.kt | 29 +++++++++++++++++ .../BasicCompletionWeigherTestGenerated.java | 21 ++++++++++-- 13 files changed, 120 insertions(+), 12 deletions(-) create mode 100644 idea/idea-completion/testData/weighers/basic/KT-25588_1.kts create mode 100644 idea/idea-completion/testData/weighers/basic/KT-25588_2.kts create mode 100644 idea/idea-completion/testData/weighers/basic/NoExpectedType.kt diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 2237deae345..c5af4ded138 100755 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -833,7 +833,7 @@ fun main(args: Array) { } testClass { - model("weighers/basic", pattern = KT_WITHOUT_DOTS_IN_NAME) + model("weighers/basic", pattern = KT_OR_KTS_WITHOUT_DOTS_IN_NAME) } testClass { diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as31 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as31 index adf4a8a9436..198bb258ac1 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as31 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as31 @@ -833,7 +833,7 @@ fun main(args: Array) { } testClass { - model("weighers/basic", pattern = KT_WITHOUT_DOTS_IN_NAME) + model("weighers/basic", pattern = KT_OR_KTS_WITHOUT_DOTS_IN_NAME) } testClass { diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 index adf4a8a9436..198bb258ac1 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 @@ -833,7 +833,7 @@ fun main(args: Array) { } testClass { - model("weighers/basic", pattern = KT_WITHOUT_DOTS_IN_NAME) + model("weighers/basic", pattern = KT_OR_KTS_WITHOUT_DOTS_IN_NAME) } testClass { diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 index 37daaeed9c4..c0000ee47a4 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 @@ -825,7 +825,7 @@ fun main(args: Array) { } testClass { - model("weighers/basic", pattern = KT_WITHOUT_DOTS_IN_NAME) + model("weighers/basic", pattern = KT_OR_KTS_WITHOUT_DOTS_IN_NAME) } testClass { diff --git a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/FuzzyType.kt b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/FuzzyType.kt index 690dfc974ed..cceb1daeede 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/idea/util/FuzzyType.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/idea/util/FuzzyType.kt @@ -135,6 +135,7 @@ class FuzzyType( private fun matchedSubstitutor(otherType: FuzzyType, matchKind: MatchKind): TypeSubstitutor? { if (type.isError) return null if (otherType.type.isError) return null + if (otherType.type.isUnit() && matchKind == MatchKind.IS_SUBTYPE) return TypeSubstitutor.EMPTY fun KotlinType.checkInheritance(otherType: KotlinType): Boolean { return when (matchKind) { diff --git a/idea/idea-completion/testData/weighers/basic/DslCalls.kt b/idea/idea-completion/testData/weighers/basic/DslCalls.kt index 1fac007f663..e79c43b51dc 100644 --- a/idea/idea-completion/testData/weighers/basic/DslCalls.kt +++ b/idea/idea-completion/testData/weighers/basic/DslCalls.kt @@ -14,7 +14,7 @@ fun main() { // ORDER: foo2 // ORDER: foo4 // ORDER: fooloooooong -// ORDER: foo3 // ORDER: foo5 +// ORDER: foo3 // ORDER: fooval // ORDER: foo1 diff --git a/idea/idea-completion/testData/weighers/basic/DslCallsAnnotatedFunctionType.kt b/idea/idea-completion/testData/weighers/basic/DslCallsAnnotatedFunctionType.kt index 1fac007f663..e79c43b51dc 100644 --- a/idea/idea-completion/testData/weighers/basic/DslCallsAnnotatedFunctionType.kt +++ b/idea/idea-completion/testData/weighers/basic/DslCallsAnnotatedFunctionType.kt @@ -14,7 +14,7 @@ fun main() { // ORDER: foo2 // ORDER: foo4 // ORDER: fooloooooong -// ORDER: foo3 // ORDER: foo5 +// ORDER: foo3 // ORDER: fooval // ORDER: foo1 diff --git a/idea/idea-completion/testData/weighers/basic/DslCallsWithMultipleReceivers.kt b/idea/idea-completion/testData/weighers/basic/DslCallsWithMultipleReceivers.kt index 40d860f1e97..8c2e350abed 100644 --- a/idea/idea-completion/testData/weighers/basic/DslCallsWithMultipleReceivers.kt +++ b/idea/idea-completion/testData/weighers/basic/DslCallsWithMultipleReceivers.kt @@ -36,6 +36,6 @@ fun foo1(i: Int) { } // ORDER: foo2 +// ORDER: foo5 // ORDER: foo10 // ORDER: foo1 -// ORDER: foo5 diff --git a/idea/idea-completion/testData/weighers/basic/DslMemberCalls.kt b/idea/idea-completion/testData/weighers/basic/DslMemberCalls.kt index 44b67a3ee45..9062ee8d443 100644 --- a/idea/idea-completion/testData/weighers/basic/DslMemberCalls.kt +++ b/idea/idea-completion/testData/weighers/basic/DslMemberCalls.kt @@ -11,10 +11,10 @@ fun main() { } } +// ORDER: foo6 // ORDER: foo2 // ORDER: foo3 -// ORDER: foo6 // ORDER: foo4 -// ORDER: foo1 // ORDER: fooLocal // ORDER: foo5 +// ORDER: foo1 diff --git a/idea/idea-completion/testData/weighers/basic/KT-25588_1.kts b/idea/idea-completion/testData/weighers/basic/KT-25588_1.kts new file mode 100644 index 00000000000..bd1b1f4132a --- /dev/null +++ b/idea/idea-completion/testData/weighers/basic/KT-25588_1.kts @@ -0,0 +1,31 @@ +// RUNTIME_WITH_SCRIPT_RUNTIME + +@DslMarker +annotation class MyDsl + +project1 { + +} + +@MyDsl +fun project1(p: Project.() -> Unit) { + Project(p) +} + +@MyDsl +fun project2(p: Project.() -> Unit): Project { + return Project(p) +} + +@MyDsl +fun Project.buildType(init: BuildType.() -> Unit): BuildType { + return BuildType(this, init) +} + +@MyDsl +class Project(init: Project.() -> Unit) + +@MyDsl +class BuildType(p: Project, init: BuildType.() -> Unit) + +// ORDER: buildType, project1, project2 \ No newline at end of file diff --git a/idea/idea-completion/testData/weighers/basic/KT-25588_2.kts b/idea/idea-completion/testData/weighers/basic/KT-25588_2.kts new file mode 100644 index 00000000000..bd73daba9e1 --- /dev/null +++ b/idea/idea-completion/testData/weighers/basic/KT-25588_2.kts @@ -0,0 +1,32 @@ +// RUNTIME_WITH_SCRIPT_RUNTIME + +@DslMarker +annotation class MyDsl + +project1 { + +} + + +@MyDsl +fun project1(p: Project.() -> Unit): Project { + Project(p) +} + +@MyDsl +fun project2(p: Project.() -> Unit) { + Project(p) +} + +@MyDsl +class Project(init: Project.() -> Unit) { + @MyDsl + fun buildType(init: BuildType.() -> Unit): BuildType { + return BuildType(this, init) + } +} + +@MyDsl +class BuildType(p: Project, init: BuildType.() -> Unit) + +// ORDER: buildType, project1, project2 \ No newline at end of file diff --git a/idea/idea-completion/testData/weighers/basic/NoExpectedType.kt b/idea/idea-completion/testData/weighers/basic/NoExpectedType.kt new file mode 100644 index 00000000000..48c8643b9d8 --- /dev/null +++ b/idea/idea-completion/testData/weighers/basic/NoExpectedType.kt @@ -0,0 +1,29 @@ +// RUNTIME +class A { + fun fooA() { + + } +} + +class B { + fun fooB(): Int { + return 3 + } +} + + +fun myWith(t: T, init: T.() -> Unit) { + +} + + +fun main() { + myWith (A()) { + myWith (B()) { + foo + } + } +} +// fooB should be first because it's receiver is closer, expected return type is Unit and should be taken into account + +// ORDER: fooB, fooA \ No newline at end of file diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java index 84daf004ada..7b235105677 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java @@ -31,7 +31,7 @@ public class BasicCompletionWeigherTestGenerated extends AbstractBasicCompletion } public void testAllFilesPresentInBasic() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/weighers/basic"), Pattern.compile("^([^.]+)\\.kt$"), TargetBackend.ANY, true); + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/weighers/basic"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } @TestMetadata("CallableReference_NothingLast.kt") @@ -104,6 +104,16 @@ public class BasicCompletionWeigherTestGenerated extends AbstractBasicCompletion runTest("idea/idea-completion/testData/weighers/basic/ImportedOrder.kt"); } + @TestMetadata("KT-25588_1.kts") + public void testKT_25588_1() throws Exception { + runTest("idea/idea-completion/testData/weighers/basic/KT-25588_1.kts"); + } + + @TestMetadata("KT-25588_2.kts") + public void testKT_25588_2() throws Exception { + runTest("idea/idea-completion/testData/weighers/basic/KT-25588_2.kts"); + } + @TestMetadata("KeywordsLast.kt") public void testKeywordsLast() throws Exception { runTest("idea/idea-completion/testData/weighers/basic/KeywordsLast.kt"); @@ -149,6 +159,11 @@ public class BasicCompletionWeigherTestGenerated extends AbstractBasicCompletion runTest("idea/idea-completion/testData/weighers/basic/NamedParameters3.kt"); } + @TestMetadata("NoExpectedType.kt") + public void testNoExpectedType() throws Exception { + runTest("idea/idea-completion/testData/weighers/basic/NoExpectedType.kt"); + } + @TestMetadata("Packages.kt") public void testPackages() throws Exception { runTest("idea/idea-completion/testData/weighers/basic/Packages.kt"); @@ -203,7 +218,7 @@ public class BasicCompletionWeigherTestGenerated extends AbstractBasicCompletion } public void testAllFilesPresentInExpectedInfo() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/weighers/basic/expectedInfo"), Pattern.compile("^([^.]+)\\.kt$"), TargetBackend.ANY, true); + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/weighers/basic/expectedInfo"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } @TestMetadata("CompanionObjectMethod.kt") @@ -286,7 +301,7 @@ public class BasicCompletionWeigherTestGenerated extends AbstractBasicCompletion } public void testAllFilesPresentInParameterNameAndType() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/weighers/basic/parameterNameAndType"), Pattern.compile("^([^.]+)\\.kt$"), TargetBackend.ANY, true); + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/weighers/basic/parameterNameAndType"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true); } @TestMetadata("Deprecated.kt")