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
This commit is contained in:
Pavel V. Talanov
2018-08-07 20:46:13 +02:00
parent 51681d57c8
commit 0c1d25d5ac
13 changed files with 120 additions and 12 deletions
@@ -833,7 +833,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractBasicCompletionWeigherTest> {
model("weighers/basic", pattern = KT_WITHOUT_DOTS_IN_NAME)
model("weighers/basic", pattern = KT_OR_KTS_WITHOUT_DOTS_IN_NAME)
}
testClass<AbstractSmartCompletionWeigherTest> {
@@ -833,7 +833,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractBasicCompletionWeigherTest> {
model("weighers/basic", pattern = KT_WITHOUT_DOTS_IN_NAME)
model("weighers/basic", pattern = KT_OR_KTS_WITHOUT_DOTS_IN_NAME)
}
testClass<AbstractSmartCompletionWeigherTest> {
@@ -833,7 +833,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractBasicCompletionWeigherTest> {
model("weighers/basic", pattern = KT_WITHOUT_DOTS_IN_NAME)
model("weighers/basic", pattern = KT_OR_KTS_WITHOUT_DOTS_IN_NAME)
}
testClass<AbstractSmartCompletionWeigherTest> {
@@ -825,7 +825,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractBasicCompletionWeigherTest> {
model("weighers/basic", pattern = KT_WITHOUT_DOTS_IN_NAME)
model("weighers/basic", pattern = KT_OR_KTS_WITHOUT_DOTS_IN_NAME)
}
testClass<AbstractSmartCompletionWeigherTest> {
@@ -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) {
+1 -1
View File
@@ -14,7 +14,7 @@ fun main() {
// ORDER: foo2
// ORDER: foo4
// ORDER: fooloooooong
// ORDER: foo3
// ORDER: foo5
// ORDER: foo3
// ORDER: fooval
// ORDER: foo1
@@ -14,7 +14,7 @@ fun main() {
// ORDER: foo2
// ORDER: foo4
// ORDER: fooloooooong
// ORDER: foo3
// ORDER: foo5
// ORDER: foo3
// ORDER: fooval
// ORDER: foo1
@@ -36,6 +36,6 @@ fun foo1(i: Int) {
}
// ORDER: foo2
// ORDER: foo5
// ORDER: foo10
// ORDER: foo1
// ORDER: foo5
@@ -11,10 +11,10 @@ fun main() {
}
}
// ORDER: foo6
// ORDER: foo2
// ORDER: foo3
// ORDER: foo6
// ORDER: foo4
// ORDER: foo1
// ORDER: fooLocal
// ORDER: foo5
// ORDER: foo1
@@ -0,0 +1,31 @@
// RUNTIME_WITH_SCRIPT_RUNTIME
@DslMarker
annotation class MyDsl
project1 {
<caret>
}
@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
@@ -0,0 +1,32 @@
// RUNTIME_WITH_SCRIPT_RUNTIME
@DslMarker
annotation class MyDsl
project1 {
<caret>
}
@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
@@ -0,0 +1,29 @@
// RUNTIME
class A {
fun fooA() {
}
}
class B {
fun fooB(): Int {
return 3
}
}
fun <T> myWith(t: T, init: T.() -> Unit) {
}
fun main() {
myWith (A()) {
myWith (B()) {
foo<caret>
}
}
}
// fooB should be first because it's receiver is closer, expected return type is Unit and should be taken into account
// ORDER: fooB, fooA
@@ -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")