Change Signature: Fix TypeInfo comparison
#KT-13437 Fixed
This commit is contained in:
@@ -18,18 +18,22 @@ package org.jetbrains.kotlin.idea.refactoring.changeSignature
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.TypeUtils
|
|
||||||
|
|
||||||
data class KotlinTypeInfo(val isCovariant: Boolean, val type: KotlinType? = null, val text: String? = null)
|
data class KotlinTypeInfo(val isCovariant: Boolean, val type: KotlinType? = null, val text: String? = null)
|
||||||
|
|
||||||
fun KotlinTypeInfo.render(): String {
|
fun KotlinTypeInfo.render(): String {
|
||||||
return when {
|
return when {
|
||||||
text != null -> text
|
text != null -> text
|
||||||
type != null -> (if (isCovariant) IdeDescriptorRenderers.SOURCE_CODE else IdeDescriptorRenderers.SOURCE_CODE_NOT_NULL_TYPE_APPROXIMATION).renderType(type)
|
type != null -> renderType()
|
||||||
else -> ""
|
else -> ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun KotlinTypeInfo.renderType(): String {
|
||||||
|
val renderer = if (isCovariant) IdeDescriptorRenderers.SOURCE_CODE else IdeDescriptorRenderers.SOURCE_CODE_NOT_NULL_TYPE_APPROXIMATION
|
||||||
|
return renderer.renderType(type!!)
|
||||||
|
}
|
||||||
|
|
||||||
fun KotlinTypeInfo.isEquivalentTo(other: KotlinTypeInfo): Boolean {
|
fun KotlinTypeInfo.isEquivalentTo(other: KotlinTypeInfo): Boolean {
|
||||||
return if (type != null && other.type != null) TypeUtils.equalTypes(type, other.type) else render() == other.render()
|
return if (type != null && other.type != null) renderType() == other.renderType() else text == other.text
|
||||||
}
|
}
|
||||||
+2
-2
@@ -407,7 +407,7 @@ class KotlinChangeSignatureDialog(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getTypeCodeFragmentContext(startFrom: PsiElement): KtElement {
|
fun getTypeCodeFragmentContext(startFrom: PsiElement): KtElement {
|
||||||
return startFrom.parentsWithSelf.mapNotNull {
|
return startFrom.parentsWithSelf.mapNotNull {
|
||||||
when {
|
when {
|
||||||
it is KtNamedFunction -> it.bodyExpression ?: it.valueParameterList
|
it is KtNamedFunction -> it.bodyExpression ?: it.valueParameterList
|
||||||
@@ -440,7 +440,7 @@ class KotlinChangeSignatureDialog(
|
|||||||
return KotlinChangeSignatureProcessor(project, changeInfo, commandName)
|
return KotlinChangeSignatureProcessor(project, changeInfo, commandName)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun PsiCodeFragment?.getTypeInfo(isCovariant: Boolean, forPreview: Boolean): KotlinTypeInfo {
|
fun PsiCodeFragment?.getTypeInfo(isCovariant: Boolean, forPreview: Boolean): KotlinTypeInfo {
|
||||||
if (this !is KtTypeCodeFragment) return KotlinTypeInfo(isCovariant)
|
if (this !is KtTypeCodeFragment) return KotlinTypeInfo(isCovariant)
|
||||||
|
|
||||||
val typeRef = getContentElement()
|
val typeRef = getContentElement()
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
class A<T, U>
|
||||||
|
|
||||||
|
interface I {
|
||||||
|
fun <T, U> bar(): A<T, U>
|
||||||
|
}
|
||||||
|
|
||||||
|
class C : I {
|
||||||
|
override fun <V, W> bar() = A<V, W>()
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
class A<T, U>
|
||||||
|
|
||||||
|
interface I {
|
||||||
|
fun <T, U> <caret>foo(): A<T, U>
|
||||||
|
}
|
||||||
|
|
||||||
|
class C : I {
|
||||||
|
override fun <V, W> foo() = A<V, W>()
|
||||||
|
}
|
||||||
+12
@@ -38,6 +38,8 @@ import org.jetbrains.kotlin.builtins.DefaultBuiltIns
|
|||||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.ui.KotlinMethodNode
|
import org.jetbrains.kotlin.idea.refactoring.changeSignature.ui.KotlinMethodNode
|
||||||
|
import org.jetbrains.kotlin.idea.refactoring.changeSignature.ui.KotlinChangeSignatureDialog.Companion.getTypeInfo
|
||||||
|
import org.jetbrains.kotlin.idea.refactoring.changeSignature.ui.KotlinChangeSignatureDialog.Companion.getTypeCodeFragmentContext
|
||||||
import org.jetbrains.kotlin.idea.search.allScope
|
import org.jetbrains.kotlin.idea.search.allScope
|
||||||
import org.jetbrains.kotlin.idea.stubindex.KotlinFullClassNameIndex
|
import org.jetbrains.kotlin.idea.stubindex.KotlinFullClassNameIndex
|
||||||
import org.jetbrains.kotlin.idea.stubindex.KotlinTopLevelFunctionFqnNameIndex
|
import org.jetbrains.kotlin.idea.stubindex.KotlinTopLevelFunctionFqnNameIndex
|
||||||
@@ -246,6 +248,11 @@ class KotlinChangeSignatureTest : KotlinLightCodeInsightFixtureTestCase() {
|
|||||||
setNewParameter(j, temp)
|
setNewParameter(j, temp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun KotlinChangeInfo.resolveType(text: String, isCovariant: Boolean, forPreview: Boolean): KotlinTypeInfo {
|
||||||
|
val codeFragment = KtPsiFactory(project).createTypeCodeFragment(text, getTypeCodeFragmentContext(context))
|
||||||
|
return codeFragment.getTypeInfo(isCovariant, forPreview)
|
||||||
|
}
|
||||||
|
|
||||||
// --------------------------------- Tests ---------------------------------
|
// --------------------------------- Tests ---------------------------------
|
||||||
|
|
||||||
fun testBadSelection() {
|
fun testBadSelection() {
|
||||||
@@ -944,4 +951,9 @@ class KotlinChangeSignatureTest : KotlinLightCodeInsightFixtureTestCase() {
|
|||||||
fun testReceiverInSafeCall() = doTestConflict { receiverParameterInfo = null }
|
fun testReceiverInSafeCall() = doTestConflict { receiverParameterInfo = null }
|
||||||
|
|
||||||
fun testRemoveParameterKeepOtherComments() = doTest { removeParameter(1) }
|
fun testRemoveParameterKeepOtherComments() = doTest { removeParameter(1) }
|
||||||
|
|
||||||
|
fun testReturnTypeViaCodeFragment() = doTest {
|
||||||
|
newName = "bar"
|
||||||
|
newReturnTypeInfo = resolveType("A<T, U>", true, true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user