Add KtTypeElement.unwrapNullability() extension function

Replace similar functions with its usage
This commit is contained in:
Roman Golyshev
2021-04-30 19:42:17 +03:00
committed by Space
parent cc41592969
commit 24642a7c9f
3 changed files with 12 additions and 9 deletions
@@ -676,3 +676,11 @@ fun getTrailingCommaByElementsList(elementList: PsiElement?): PsiElement? {
val KtNameReferenceExpression.isUnderscoreInBackticks val KtNameReferenceExpression.isUnderscoreInBackticks
get() = getReferencedName() == "`_`" get() = getReferencedName() == "`_`"
tailrec fun KtTypeElement.unwrapNullability(): KtTypeElement? {
return when (this) {
is KtNullableType -> this.innerType?.unwrapNullability()
is KtDefinitelyNotNullType -> this.innerType?.unwrapNullability()
else -> this
}
}
@@ -57,6 +57,7 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.psi.psiUtil.unwrapNullability
internal class KtFirReferenceShortener( internal class KtFirReferenceShortener(
override val analysisSession: KtFirAnalysisSession, override val analysisSession: KtFirAnalysisSession,
@@ -218,7 +219,7 @@ private class ElementsToShortenCollector(private val shorteningContext: FirShort
val wholeTypeReference = resolvedTypeRef.psi as? KtTypeReference ?: return val wholeTypeReference = resolvedTypeRef.psi as? KtTypeReference ?: return
val wholeClassifierId = resolvedTypeRef.type.lowerBoundIfFlexible().classId ?: return val wholeClassifierId = resolvedTypeRef.type.lowerBoundIfFlexible().classId ?: return
val wholeTypeElement = wholeTypeReference.typeElement.unwrapNullable() as? KtUserType ?: return val wholeTypeElement = wholeTypeReference.typeElement?.unwrapNullability() as? KtUserType ?: return
if (wholeTypeElement.qualifier == null) return if (wholeTypeElement.qualifier == null) return
@@ -456,9 +457,6 @@ private fun CallableId.asImportableFqName(): FqName? = if (classId == null) pack
private fun KtElement.getDotQualifiedExpressionForSelector(): KtDotQualifiedExpression? = private fun KtElement.getDotQualifiedExpressionForSelector(): KtDotQualifiedExpression? =
getQualifiedExpressionForSelector() as? KtDotQualifiedExpression getQualifiedExpressionForSelector() as? KtDotQualifiedExpression
private tailrec fun KtTypeElement?.unwrapNullable(): KtTypeElement? =
if (this is KtNullableType) this.innerType.unwrapNullable() else this
private fun KtDotQualifiedExpression.deleteQualifier(): KtExpression? { private fun KtDotQualifiedExpression.deleteQualifier(): KtExpression? {
val selectorExpression = selectorExpression ?: return null val selectorExpression = selectorExpression ?: return null
return this.replace(selectorExpression) as KtExpression return this.replace(selectorExpression) as KtExpression
@@ -41,6 +41,7 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.psi.psiUtil.unwrapNullability
import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
internal object FirReferenceResolveHelper { internal object FirReferenceResolveHelper {
@@ -420,7 +421,7 @@ internal object FirReferenceResolveHelper {
// FIXME make it work with generics in functional types (like () -> AA.BB<CC, AA.DD>) // FIXME make it work with generics in functional types (like () -> AA.BB<CC, AA.DD>)
val wholeType = when (val psi = wholeTypeFir.psi) { val wholeType = when (val psi = wholeTypeFir.psi) {
is KtUserType -> psi is KtUserType -> psi
is KtTypeReference -> psi.typeElement?.unwrapNullable() as? KtUserType is KtTypeReference -> psi.typeElement?.unwrapNullability() as? KtUserType
else -> null else -> null
} ?: return null } ?: return null
@@ -447,9 +448,5 @@ internal object FirReferenceResolveHelper {
return qualifierIndex return qualifierIndex
} }
private tailrec fun KtTypeElement.unwrapNullable(): KtTypeElement? {
return if (this is KtNullableType) innerType?.unwrapNullable() else this
}
private val syntheticTokenTypes = TokenSet.create(KtTokens.ELVIS, KtTokens.EXCLEXCL) private val syntheticTokenTypes = TokenSet.create(KtTokens.ELVIS, KtTokens.EXCLEXCL)
} }