[kotlin] k2 change signature: hide ERROR from empty type presentation

^KTIJ-29074 fixed

GitOrigin-RevId: a0ccdee851f445cddfad57e9d27180d38dc4e6c7
This commit is contained in:
Anna Kozlova
2024-03-12 21:14:23 +01:00
committed by intellij-monorepo-bot
parent 276414e3e4
commit 536cb20e0d
5 changed files with 25 additions and 6 deletions

View File

@@ -53,5 +53,6 @@
<orderEntry type="module" module-name="kotlin.code-insight.utils" />
<orderEntry type="module" module-name="kotlin.fir.frontend-independent" />
<orderEntry type="module" module-name="kotlin.searching.base" />
<orderEntry type="library" name="kotlinc.high-level-api-impl-base" level="project" />
</component>
</module>

View File

@@ -53,7 +53,7 @@ class KotlinMethodDescriptor(private val callable: KtNamedDeclaration) :
}.firstOrNull() ?: "receiver"
KotlinParameterInfo(
0, KotlinTypeInfo(ktType.render(position = Variance.INVARIANT), callable), receiverName,
0, KotlinTypeInfo(ktType, callable), receiverName,
KotlinValVar.None, null, false, null, callable
)
}
@@ -71,7 +71,7 @@ class KotlinMethodDescriptor(private val callable: KtNamedDeclaration) :
(callable as? KtCallableDeclaration)
?.valueParameters?.forEach { p ->
val parameterInfo = KotlinParameterInfo(
params.size, KotlinTypeInfo(p.getReturnKtType().render(position = Variance.INVARIANT), callable),
params.size, KotlinTypeInfo(p.getReturnKtType(), callable),
p.name ?: "",
p.valOrVarKeyword.toValVar(),
p.defaultValue, p.defaultValue != null, p.defaultValue, callable

View File

@@ -12,15 +12,33 @@ import org.jetbrains.kotlin.analysis.api.components.buildSubstitutor
import org.jetbrains.kotlin.analysis.api.components.buildTypeParameterType
import org.jetbrains.kotlin.analysis.api.lifetime.allowAnalysisFromWriteAction
import org.jetbrains.kotlin.analysis.api.lifetime.allowAnalysisOnEdt
import org.jetbrains.kotlin.analysis.api.renderer.types.KtTypeRenderer
import org.jetbrains.kotlin.analysis.api.renderer.types.impl.KtTypeRendererForSource
import org.jetbrains.kotlin.analysis.api.renderer.types.renderers.KtTypeErrorTypeRenderer
import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol
import org.jetbrains.kotlin.analysis.api.types.KtDefinitelyNotNullType
import org.jetbrains.kotlin.analysis.api.types.KtFunctionalType
import org.jetbrains.kotlin.analysis.api.types.KtSubstitutor
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.analysis.api.types.KtTypeErrorType
import org.jetbrains.kotlin.analysis.utils.printer.PrettyPrinter
import org.jetbrains.kotlin.asJava.toLightMethods
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.types.Variance
data class KotlinTypeInfo(var text: String?, val context: KtElement)
data class KotlinTypeInfo(var text: String?, val context: KtElement) {
constructor(ktType: KtType, context: KtElement): this(analyze(context) { ktType.render(errorIgnoringRenderer, Variance.INVARIANT) }, context)
}
private val errorIgnoringRenderer: KtTypeRenderer = KtTypeRendererForSource.WITH_QUALIFIED_NAMES.with {
typeErrorTypeRenderer = object : KtTypeErrorTypeRenderer {
context(KtAnalysisSession, KtTypeRenderer)
override fun renderType(type: KtTypeErrorType, printer: PrettyPrinter) {
type.tryRenderAsNonErrorType()?.let {
printer.append(it)
}
}
}
}
@OptIn(KtAllowAnalysisFromWriteAction::class, KtAllowAnalysisOnEdt::class)
internal fun KtPsiFactory.createType(

View File

@@ -166,7 +166,7 @@ object ChangeSignatureFixFactory {
val argumentExpression = currentArgument.getArgumentExpression()
val ktType = getKtType(argumentExpression) ?: return null
return KotlinParameterInfo(
originalType = KotlinTypeInfo(ktType.render(position = Variance.IN_VARIANCE), callElement),
originalType = KotlinTypeInfo(ktType, callElement),
name = getNewArgumentName(currentArgument, validator),
originalIndex = -1,
valOrVar = defaultValOrVar(ktCallableDeclaration),

View File

@@ -85,7 +85,7 @@ internal class KotlinChangeSignatureDialog(
val psiFactory = KtPsiFactory(myDefaultValueContext.project)
val contentElement = psiFactory.createTypeCodeFragment(resultParameterInfo.typeText, typeContext).getContentElement()
val presentableText = if (contentElement != null) {
val presentableText = if (resultParameterInfo.typeText.isNotEmpty() && contentElement != null) {
analyzeInModalWindow(contentElement, KotlinBundle.message("fix.change.signature.prepare")) {
contentElement.getKtType().getPresentableText()
}