[FIR IDE] Refactor functions for qualifiers traversing in KtFirReferenceShortener
The code is more coherent when `qualifiedTypesWithSelf` and `qualifiedExpressionsWithSelf` both do not contain unqualified psi elements
This commit is contained in:
+18
-8
@@ -335,7 +335,7 @@ private class ElementsToShortenCollector(
|
||||
private fun findTypeToShorten(wholeClassifierId: ClassId, wholeTypeElement: KtUserType): ElementToShorten? {
|
||||
val positionScopes = shorteningContext.findScopesAtPosition(wholeTypeElement, namesToImport, towerContextProvider) ?: return null
|
||||
val allClassIds = wholeClassifierId.outerClassesWithSelf
|
||||
val allQualifiedTypeElements = wholeTypeElement.qualifiersWithSelf.takeWhile { it.qualifier != null }
|
||||
val allQualifiedTypeElements = wholeTypeElement.qualifiedTypesWithSelf
|
||||
return findClassifierElementsToShorten(
|
||||
positionScopes,
|
||||
allClassIds,
|
||||
@@ -346,8 +346,7 @@ private class ElementsToShortenCollector(
|
||||
}
|
||||
|
||||
private fun findFakePackageToShorten(typeElement: KtUserType): ShortenType? {
|
||||
val deepestTypeWithQualifier = typeElement.qualifiersWithSelf.takeWhile { it.qualifier != null }.lastOrNull()
|
||||
?: error("Type element should have at least one qualifier, instead it was ${typeElement.text}")
|
||||
val deepestTypeWithQualifier = typeElement.qualifiedTypesWithSelf.last()
|
||||
|
||||
return if (deepestTypeWithQualifier.hasFakeRootPrefix()) ShortenType(deepestTypeWithQualifier) else null
|
||||
}
|
||||
@@ -372,7 +371,7 @@ private class ElementsToShortenCollector(
|
||||
val positionScopes: List<FirScope> =
|
||||
shorteningContext.findScopesAtPosition(wholeQualifierElement, namesToImport, towerContextProvider) ?: return null
|
||||
val allClassIds: Sequence<ClassId> = wholeClassQualifier.outerClassesWithSelf
|
||||
val allQualifiers: Sequence<KtDotQualifiedExpression> = wholeQualifierElement.qualifiersWithSelf
|
||||
val allQualifiers: Sequence<KtDotQualifiedExpression> = wholeQualifierElement.qualifiedExpressionsWithSelf
|
||||
return findClassifierElementsToShorten(
|
||||
positionScopes,
|
||||
allClassIds,
|
||||
@@ -538,7 +537,7 @@ private class ElementsToShortenCollector(
|
||||
}
|
||||
|
||||
private fun findFakePackageToShorten(wholeQualifiedExpression: KtDotQualifiedExpression): ShortenQualifier? {
|
||||
val deepestQualifier = wholeQualifiedExpression.qualifiersWithSelf.last()
|
||||
val deepestQualifier = wholeQualifiedExpression.qualifiedExpressionsWithSelf.last()
|
||||
return if (deepestQualifier.hasFakeRootPrefix()) ShortenQualifier(deepestQualifier) else null
|
||||
}
|
||||
|
||||
@@ -561,10 +560,21 @@ private class ElementsToShortenCollector(
|
||||
private val ClassId.outerClassesWithSelf: Sequence<ClassId>
|
||||
get() = generateSequence(this) { it.outerClassId }
|
||||
|
||||
private val KtUserType.qualifiersWithSelf: Sequence<KtUserType>
|
||||
get() = generateSequence(this) { it.qualifier }
|
||||
/**
|
||||
* Note: The resulting sequence does not contain non-qualified types!
|
||||
*
|
||||
* For type `A.B.C.D` it will return sequence of [`A.B.C.D`, `A.B.C`, `A.B`] (**without** `A`).
|
||||
*/
|
||||
private val KtUserType.qualifiedTypesWithSelf: Sequence<KtUserType>
|
||||
get() {
|
||||
require(qualifier != null) {
|
||||
"Type element should have at least one qualifier, instead it was $text"
|
||||
}
|
||||
|
||||
private val KtDotQualifiedExpression.qualifiersWithSelf: Sequence<KtDotQualifiedExpression>
|
||||
return generateSequence(this) { it.qualifier }.takeWhile { it.qualifier != null }
|
||||
}
|
||||
|
||||
private val KtDotQualifiedExpression.qualifiedExpressionsWithSelf: Sequence<KtDotQualifiedExpression>
|
||||
get() = generateSequence(this) { it.receiverExpression as? KtDotQualifiedExpression }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user