KT-8574 'Convert to block body' inserts spurious < token
#KT-8574 Fixed
This commit is contained in:
@@ -16,17 +16,34 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.util
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRendererModifier
|
||||
import org.jetbrains.kotlin.renderer.NameShortness
|
||||
import org.jetbrains.kotlin.renderer.OverrideRenderingPolicy
|
||||
import org.jetbrains.kotlin.types.JetType
|
||||
import org.jetbrains.kotlin.types.isDynamic
|
||||
|
||||
public object IdeDescriptorRenderers {
|
||||
public val APPROXIMATE_FLEXIBLE_TYPES: (JetType) -> JetType = { approximateFlexibleTypes(it, true) }
|
||||
|
||||
public val APPROXIMATE_FLEXIBLE_TYPES_IN_ARGUMENTS: (JetType) -> JetType = { approximateFlexibleTypes(it, false) }
|
||||
|
||||
private fun unwrapAnonymousType(type: JetType): JetType {
|
||||
if (type.isDynamic()) return type
|
||||
|
||||
val classifier = type.constructor.declarationDescriptor
|
||||
if (classifier != null && !classifier.name.isSpecial) return type
|
||||
|
||||
type.constructor.supertypes.singleOrNull()?.let { return it }
|
||||
|
||||
val builtIns = KotlinBuiltIns.getInstance()
|
||||
return if (type.isMarkedNullable)
|
||||
builtIns.nullableAnyType
|
||||
else
|
||||
builtIns.anyType
|
||||
}
|
||||
|
||||
private val BASE: DescriptorRenderer = DescriptorRenderer.withOptions {
|
||||
normalizedVisibilities = true
|
||||
withDefinedIn = false
|
||||
@@ -38,16 +55,16 @@ public object IdeDescriptorRenderers {
|
||||
|
||||
public val SOURCE_CODE: DescriptorRenderer = BASE.withOptions {
|
||||
nameShortness = NameShortness.SOURCE_CODE_QUALIFIED
|
||||
typeNormalizer = APPROXIMATE_FLEXIBLE_TYPES
|
||||
typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES(unwrapAnonymousType(it)) }
|
||||
}
|
||||
|
||||
public val SOURCE_CODE_FOR_TYPE_ARGUMENTS: DescriptorRenderer = BASE.withOptions {
|
||||
nameShortness = NameShortness.SOURCE_CODE_QUALIFIED
|
||||
typeNormalizer = APPROXIMATE_FLEXIBLE_TYPES_IN_ARGUMENTS
|
||||
typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES_IN_ARGUMENTS(unwrapAnonymousType(it)) }
|
||||
}
|
||||
|
||||
public val SOURCE_CODE_SHORT_NAMES_IN_TYPES: DescriptorRenderer = BASE.withOptions {
|
||||
nameShortness = NameShortness.SHORT
|
||||
typeNormalizer = APPROXIMATE_FLEXIBLE_TYPES
|
||||
typeNormalizer = { APPROXIMATE_FLEXIBLE_TYPES(unwrapAnonymousType(it)) }
|
||||
}
|
||||
}
|
||||
|
||||
+6
-2
@@ -62,6 +62,7 @@ import org.jetbrains.kotlin.idea.util.approximateWithResolvableType
|
||||
import org.jetbrains.kotlin.idea.util.isResolvableInScope
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement
|
||||
@@ -87,8 +88,11 @@ private val DEFAULT_PARAMETER_TYPE = KotlinBuiltIns.getInstance().getNullableAny
|
||||
private fun DeclarationDescriptor.renderForMessage(): String =
|
||||
IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.render(this)
|
||||
|
||||
private fun JetType.renderForMessage(): String =
|
||||
IdeDescriptorRenderers.SOURCE_CODE.renderType(this)
|
||||
private val TYPE_RENDERER = DescriptorRenderer.FQ_NAMES_IN_TYPES.withOptions {
|
||||
typeNormalizer = IdeDescriptorRenderers.APPROXIMATE_FLEXIBLE_TYPES
|
||||
}
|
||||
|
||||
private fun JetType.renderForMessage(): String = TYPE_RENDERER.renderType(this)
|
||||
|
||||
private fun JetDeclaration.renderForMessage(bindingContext: BindingContext): String? =
|
||||
bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, this]?.renderForMessage()
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
fun f() = <caret>object { }
|
||||
@@ -0,0 +1,3 @@
|
||||
fun f(): Any {
|
||||
return object { }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
interface I
|
||||
|
||||
fun f() = <caret>object : I { }
|
||||
@@ -0,0 +1,5 @@
|
||||
interface I
|
||||
|
||||
fun f(): I {
|
||||
return object : I { }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
interface I1
|
||||
interface I2
|
||||
|
||||
fun f() {
|
||||
fun g() = <caret>object : I1, I2 { }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
interface I1
|
||||
interface I2
|
||||
|
||||
fun f() {
|
||||
fun g(): Any {
|
||||
return object : I1, I2 { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
interface I
|
||||
|
||||
fun f() = <caret>listOf(object : I { })
|
||||
@@ -0,0 +1,7 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
interface I
|
||||
|
||||
fun f(): List<I> {
|
||||
return listOf(object : I { })
|
||||
}
|
||||
@@ -3810,6 +3810,30 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToBlockBody/setter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("valueIsAnonymousObject.kt")
|
||||
public void testValueIsAnonymousObject() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToBlockBody/valueIsAnonymousObject.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("valueIsAnonymousObject2.kt")
|
||||
public void testValueIsAnonymousObject2() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToBlockBody/valueIsAnonymousObject2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("valueIsAnonymousObject3.kt")
|
||||
public void testValueIsAnonymousObject3() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToBlockBody/valueIsAnonymousObject3.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("valueIsAnonymousObject4.kt")
|
||||
public void testValueIsAnonymousObject4() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToBlockBody/valueIsAnonymousObject4.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/convertToConcatenatedString")
|
||||
|
||||
Reference in New Issue
Block a user