KT-6393 Smart completion of anonymous object inserts type with "!" for type parameter

#KT-6393 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-05-08 11:36:30 +03:00
parent 833d474876
commit ce3492f270
14 changed files with 104 additions and 58 deletions
@@ -485,7 +485,7 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
if (typeProjection.getProjectionKind() != Variance.INVARIANT) {
builder.append(typeProjection.getProjectionKind()).append(" ");
}
builder.append(renderNormalizedType(typeProjection.getType()));
builder.append(renderType(typeProjection.getType()));
}
if (iterator.hasNext()) {
builder.append(", ");
@@ -47,13 +47,21 @@ public fun approximateFlexibleTypes(jetType: JetType, outermost: Boolean = true)
// Foo<(Mutable)Collection<T>!>! -> Foo<Collection<T>>?
// Foo! -> Foo?
// Foo<Bar!>! -> Foo<Bar>?
val approximation =
var approximation =
if (isCollection)
TypeUtils.makeNullableAsSpecified(if (jetType.isMarkedReadOnly()) flexible.upperBound else flexible.lowerBound, outermost)
else
if (outermost) flexible.upperBound else flexible.lowerBound
val approximated = approximateFlexibleTypes(approximation)
return if (jetType.isMarkedNotNull()) approximated.makeNotNullable() else approximated
approximation = approximateFlexibleTypes(approximation)
approximation = if (jetType.isMarkedNotNull()) approximation.makeNotNullable() else approximation
if (approximation.isMarkedNullable() && !flexible.lowerBound.isMarkedNullable() && TypeUtils.isTypeParameter(approximation) && TypeUtils.hasNullableSuperType(approximation)) {
approximation = approximation.makeNotNullable()
}
return approximation
}
return JetTypeImpl(
jetType.getAnnotations(),
@@ -455,7 +455,7 @@ class SmartCompletion(
val lookupString = lookupElement.getLookupString()
val typeArgs = jetType.getArguments()
var itemText = lookupString + DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderTypeArguments(typeArgs)
var itemText = lookupString + IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderTypeArguments(typeArgs)
val typeText = DescriptorUtils.getFqName(classifier).toString() + IdeDescriptorRenderers.SOURCE_CODE.renderTypeArguments(typeArgs)
val insertHandler: InsertHandler<LookupElement> = object : InsertHandler<LookupElement> {
@@ -147,7 +147,7 @@ class TypeInstantiationItems(
// 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 + DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderTypeArguments(typeArgsToUse)
var itemText = lookupString + IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderTypeArguments(typeArgsToUse)
var signatureText: String? = null
val insertHandler: InsertHandler<LookupElement>
@@ -0,0 +1,3 @@
fun<T> f(){
JavaClass<T>(obj<caret>)
}
@@ -0,0 +1,3 @@
public trait Comparator<T> {
public fun compare(var1: T, var2: T): Int
}
@@ -0,0 +1,4 @@
class JavaClass<T> {
JavaClass(Comparator<T> comparator) {
}
}
@@ -0,0 +1,7 @@
fun<T> f(){
JavaClass<T>(object: Comparator<T> {
override fun compare(var1: T, var2: T): Int {
throw UnsupportedOperationException()
}
})
}
@@ -0,0 +1,4 @@
class JavaClass<T> {
JavaClass(Comparator<T> comparator) {
}
}
@@ -0,0 +1,5 @@
fun f(){
JavaClass<String>(<caret>)
}
// EXIST: { itemText: "object: Comparator<String?>{...}" }
@@ -0,0 +1,3 @@
public trait Comparator<T> {
public fun compare(var1: T, var2: T): Int
}
@@ -35,6 +35,12 @@ public class MultiFileSmartCompletionTestGenerated extends AbstractMultiFileSmar
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/smartMultiFile"), Pattern.compile("^([^\\.]+)$"), false);
}
@TestMetadata("AnonymousObjectGenericJava")
public void testAnonymousObjectGenericJava() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/smartMultiFile/AnonymousObjectGenericJava/");
doTest(fileName);
}
@TestMetadata("FunctionFromAnotherPackage")
public void testFunctionFromAnotherPackage() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/smartMultiFile/FunctionFromAnotherPackage/");
@@ -1,52 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.completion.test.handlers;
import com.intellij.codeInsight.completion.CompletionType;
import org.jetbrains.kotlin.idea.completion.test.KotlinCompletionTestCase;
import org.jetbrains.kotlin.idea.completion.test.TestPackage;
import java.io.File;
public class SmartCompletionMultifileHandlerTest extends KotlinCompletionTestCase {
public void testImportExtensionFunction() throws Exception {
doTest();
}
public void testImportExtensionProperty() throws Exception {
doTest();
}
@Override
protected void setUp() throws Exception {
setType(CompletionType.SMART);
super.setUp();
}
public void doTest() throws Exception {
String fileName = getTestName(false);
configureByFiles(null, fileName + "-1.kt", fileName + "-2.kt");
complete(1);
checkResultByFile(fileName + ".kt.after");
}
@Override
protected String getTestDataPath() {
return new File(TestPackage.getCOMPLETION_TEST_DATA_BASE_PATH(), "/handlers/multifile/smart/").getPath() + File.separator;
}
}
@@ -0,0 +1,55 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.completion.test.handlers
import com.intellij.codeInsight.completion.CompletionType
import junit.framework.TestCase
import org.jetbrains.kotlin.idea.completion.test.COMPLETION_TEST_DATA_BASE_PATH
import org.jetbrains.kotlin.idea.completion.test.KotlinCompletionTestCase
import java.io.File
import kotlin.test.assertTrue
public class SmartCompletionMultifileHandlerTest : KotlinCompletionTestCase() {
public fun testImportExtensionFunction() { doTest() }
public fun testImportExtensionProperty() { doTest() }
public fun testAnonymousObjectGenericJava() { doTest() }
override fun setUp() {
setType(CompletionType.SMART)
super.setUp()
}
public fun doTest() {
val fileName = getTestName(false)
val fileNames = listOf(fileName + "-1.kt", fileName + "-2.kt", fileName + ".java")
configureByFiles(null, *fileNames.filter { File(getTestDataPath() + it).exists() }.toTypedArray())
complete(1)
if (myItems != null) {
assertTrue(myItems.size() == 1, "Multiple items in completion")
selectItem(myItems[0])
}
checkResultByFile(fileName + ".kt.after")
}
override fun getTestDataPath() = File(COMPLETION_TEST_DATA_BASE_PATH, "/handlers/multifile/smart/").getPath() + File.separator
}