From 40de063ebd874614e0822f1da3e52dc306612b9e Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 3 Sep 2015 19:35:19 +0300 Subject: [PATCH] Smart completion for generic class instantiation to not insert explicit type arguments (for they'll probably be inferred from the expected type) --- .../completion/smart/TypeInstantiationItems.kt | 15 ++++++++------- .../smart/ConstructorForGenericType.kt.after | 2 +- .../smart/ConstructorInsertsImport.kt.after | 3 +-- .../smart/ConstructorInsertsImport2.kt.after | 4 +--- .../smart/ConstructorWithKeywordName.kt.after | 2 +- .../smart/constructor/GenericJavaClass.kt | 2 +- .../testData/smart/constructor/GenericType.kt | 2 +- .../testData/smart/inheritors/2.kt | 2 +- .../GenericInheritors1/GenericInheritors1.kt | 4 ++-- .../GenericInheritors2/GenericInheritors2.kt | 2 +- .../GenericInheritors4/GenericInheritors4.kt | 2 +- .../InheritorsAndMultipleExpectedTypes.kt | 3 +-- 12 files changed, 20 insertions(+), 23 deletions(-) diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/TypeInstantiationItems.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/TypeInstantiationItems.kt index 064fe4e7236..4fe6003ded8 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/TypeInstantiationItems.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/TypeInstantiationItems.kt @@ -146,19 +146,20 @@ class TypeInstantiationItems( var lookupString = lookupElement.getLookupString() var allLookupStrings = setOf(lookupString) - - // drop "in" and "out" from type arguments - they cannot be used in constructor call - val typeArgsToUse = typeArgs.map { TypeProjectionImpl(Variance.INVARIANT, it.getType()) } - - var itemText = lookupString + IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderTypeArguments(typeArgsToUse) + var itemText = lookupString var signatureText: String? = null + var typeText = IdeDescriptorRenderers.SOURCE_CODE.renderClassifierName(classifier) val insertHandler: InsertHandler - val typeText = IdeDescriptorRenderers.SOURCE_CODE.renderClassifierName(classifier) + IdeDescriptorRenderers.SOURCE_CODE.renderTypeArguments(typeArgsToUse) if (isAbstract) { + // drop "in" and "out" from type arguments - they cannot be used in constructor call + val typeArgsToUse = typeArgs.map { TypeProjectionImpl(Variance.INVARIANT, it.getType()) } + itemText += IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderTypeArguments(typeArgsToUse) + typeText += IdeDescriptorRenderers.SOURCE_CODE.renderTypeArguments(typeArgsToUse) + val constructorParenthesis = if (classifier.getKind() != ClassKind.INTERFACE) "()" else "" itemText += constructorParenthesis - itemText = "object: " + itemText + "{...}" + itemText = "object: $itemText{...}" lookupString = "object" allLookupStrings = setOf(lookupString, lookupElement.getLookupString()) insertHandler = InsertHandler { context, item -> diff --git a/idea/idea-completion/testData/handlers/smart/ConstructorForGenericType.kt.after b/idea/idea-completion/testData/handlers/smart/ConstructorForGenericType.kt.after index 9114c35bef2..7a85152b714 100644 --- a/idea/idea-completion/testData/handlers/smart/ConstructorForGenericType.kt.after +++ b/idea/idea-completion/testData/handlers/smart/ConstructorForGenericType.kt.after @@ -4,7 +4,7 @@ import java.util.List fun foo(p: HashMap>){} fun f(){ - foo(HashMap>()) + foo(HashMap()) } // ELEMENT: HashMap diff --git a/idea/idea-completion/testData/handlers/smart/ConstructorInsertsImport.kt.after b/idea/idea-completion/testData/handlers/smart/ConstructorInsertsImport.kt.after index 62d61a039c6..2afd1b064de 100644 --- a/idea/idea-completion/testData/handlers/smart/ConstructorInsertsImport.kt.after +++ b/idea/idea-completion/testData/handlers/smart/ConstructorInsertsImport.kt.after @@ -1,11 +1,10 @@ -import java.io.File import java.util.HashMap class X { fun foo(p: java.util.HashMap){} fun f(){ - foo(HashMap()) + foo(HashMap()) } } diff --git a/idea/idea-completion/testData/handlers/smart/ConstructorInsertsImport2.kt.after b/idea/idea-completion/testData/handlers/smart/ConstructorInsertsImport2.kt.after index 97de61531cf..2550cbe96fe 100644 --- a/idea/idea-completion/testData/handlers/smart/ConstructorInsertsImport2.kt.after +++ b/idea/idea-completion/testData/handlers/smart/ConstructorInsertsImport2.kt.after @@ -1,12 +1,10 @@ -import java.io.File -import java.util.AbstractMap import java.util.HashMap class X { fun foo(p: java.util.HashMap>){} fun f(){ - foo(HashMap>()) + foo(HashMap()) } } diff --git a/idea/idea-completion/testData/handlers/smart/ConstructorWithKeywordName.kt.after b/idea/idea-completion/testData/handlers/smart/ConstructorWithKeywordName.kt.after index d49d43bd168..7771576246b 100644 --- a/idea/idea-completion/testData/handlers/smart/ConstructorWithKeywordName.kt.after +++ b/idea/idea-completion/testData/handlers/smart/ConstructorWithKeywordName.kt.after @@ -5,5 +5,5 @@ trait `trait` fun foo(p: `class`<`trait`>){} fun f(){ - foo(`class`<`trait`>()) + foo(`class`()) } diff --git a/idea/idea-completion/testData/smart/constructor/GenericJavaClass.kt b/idea/idea-completion/testData/smart/constructor/GenericJavaClass.kt index 800440d0b37..593af094239 100644 --- a/idea/idea-completion/testData/smart/constructor/GenericJavaClass.kt +++ b/idea/idea-completion/testData/smart/constructor/GenericJavaClass.kt @@ -2,4 +2,4 @@ import java.util.ArrayList var a : ArrayList = -// EXIST: { lookupString: "ArrayList", itemText: "ArrayList", tailText: "(...) (java.util)" } +// EXIST: { lookupString: "ArrayList", itemText: "ArrayList", tailText: "(...) (java.util)" } diff --git a/idea/idea-completion/testData/smart/constructor/GenericType.kt b/idea/idea-completion/testData/smart/constructor/GenericType.kt index 851177494a6..5d601a106d9 100644 --- a/idea/idea-completion/testData/smart/constructor/GenericType.kt +++ b/idea/idea-completion/testData/smart/constructor/GenericType.kt @@ -4,4 +4,4 @@ fun foo(p : Any){ var a : Foo = } -// EXIST: { lookupString:"Foo", itemText:"Foo", tailText:"() ()" } +// EXIST: { lookupString:"Foo", itemText:"Foo", tailText:"() ()" } diff --git a/idea/idea-completion/testData/smart/inheritors/2.kt b/idea/idea-completion/testData/smart/inheritors/2.kt index c6dfb12bcdb..d23a58201a7 100644 --- a/idea/idea-completion/testData/smart/inheritors/2.kt +++ b/idea/idea-completion/testData/smart/inheritors/2.kt @@ -2,4 +2,4 @@ fun foo(): List { return } -// EXIST: { lookupString: "ArrayList", itemText: "ArrayList", tailText: "(...) (java.util)" } +// EXIST: { lookupString: "ArrayList", itemText: "ArrayList", tailText: "(...) (java.util)" } diff --git a/idea/idea-completion/testData/smartMultiFile/GenericInheritors1/GenericInheritors1.kt b/idea/idea-completion/testData/smartMultiFile/GenericInheritors1/GenericInheritors1.kt index 7d248e1ce64..436e994aee0 100644 --- a/idea/idea-completion/testData/smartMultiFile/GenericInheritors1/GenericInheritors1.kt +++ b/idea/idea-completion/testData/smartMultiFile/GenericInheritors1/GenericInheritors1.kt @@ -5,11 +5,11 @@ fun foo(): KotlinTrait { } // EXIST: { lookupString: "object", itemText: "object: KotlinTrait{...}" } -// EXIST: { lookupString: "KotlinInheritor1", itemText: "KotlinInheritor1", tailText: "() (p)" } +// EXIST: { lookupString: "KotlinInheritor1", itemText: "KotlinInheritor1", tailText: "() (p)" } // EXIST: { lookupString: "KotlinInheritor2", itemText: "KotlinInheritor2", tailText: "() (p)" } // ABSENT: KotlinInheritor3 // EXIST: { lookupString: "object", itemText: "object: KotlinInheritor4(){...}" } // ABSENT: KotlinInheritor5 -// EXIST: { lookupString: "KotlinInheritor6", itemText: "KotlinInheritor6", tailText: "() (p)" } +// EXIST: { lookupString: "KotlinInheritor6", itemText: "KotlinInheritor6", tailText: "() (p)" } // EXIST: { lookupString: "JavaInheritor1", itemText: "JavaInheritor1", tailText: "() ()" } // ABSENT: JavaInheritor2 diff --git a/idea/idea-completion/testData/smartMultiFile/GenericInheritors2/GenericInheritors2.kt b/idea/idea-completion/testData/smartMultiFile/GenericInheritors2/GenericInheritors2.kt index 219f665fddc..ed1d3260e09 100644 --- a/idea/idea-completion/testData/smartMultiFile/GenericInheritors2/GenericInheritors2.kt +++ b/idea/idea-completion/testData/smartMultiFile/GenericInheritors2/GenericInheritors2.kt @@ -5,4 +5,4 @@ fun foo(): KotlinTrait { } // EXIST: { lookupString: "object", itemText: "object: KotlinTrait{...}" } -// EXIST: { lookupString: "KotlinInheritor", itemText: "KotlinInheritor", tailText: "() (p)" } +// EXIST: { lookupString: "KotlinInheritor", itemText: "KotlinInheritor", tailText: "() (p)" } diff --git a/idea/idea-completion/testData/smartMultiFile/GenericInheritors4/GenericInheritors4.kt b/idea/idea-completion/testData/smartMultiFile/GenericInheritors4/GenericInheritors4.kt index dc3f5e7e3e9..69b80cc1f32 100644 --- a/idea/idea-completion/testData/smartMultiFile/GenericInheritors4/GenericInheritors4.kt +++ b/idea/idea-completion/testData/smartMultiFile/GenericInheritors4/GenericInheritors4.kt @@ -5,4 +5,4 @@ fun foo(): KotlinTrait> { } // EXIST: { lookupString: "object", itemText: "object: KotlinTrait>{...}" } -// EXIST: { lookupString: "KotlinInheritor", itemText: "KotlinInheritor", tailText: "() (p)" } +// EXIST: { lookupString: "KotlinInheritor", itemText: "KotlinInheritor", tailText: "() (p)" } diff --git a/idea/idea-completion/testData/smartMultiFile/InheritorsAndMultipleExpectedTypes/InheritorsAndMultipleExpectedTypes.kt b/idea/idea-completion/testData/smartMultiFile/InheritorsAndMultipleExpectedTypes/InheritorsAndMultipleExpectedTypes.kt index 131fdb81cdb..1dbbbc7345e 100644 --- a/idea/idea-completion/testData/smartMultiFile/InheritorsAndMultipleExpectedTypes/InheritorsAndMultipleExpectedTypes.kt +++ b/idea/idea-completion/testData/smartMultiFile/InheritorsAndMultipleExpectedTypes/InheritorsAndMultipleExpectedTypes.kt @@ -6,7 +6,6 @@ fun bar() { // EXIST: { lookupString: "object", itemText: "object: T2{...}" } // EXIST: { lookupString: "object", itemText: "object: T3{...}" } // EXIST: { lookupString: "C1", itemText: "C1" } -// EXIST: { lookupString: "C2", itemText: "C2" } -// EXIST: { lookupString: "C2", itemText: "C2" } +// EXIST: { lookupString: "C2", itemText: "C2" } // EXIST: { lookupString: "C3", itemText: "C3" } // NOTHING_ELSE