KT-62675 [AA] Refactor ElementsToShortenCollector.createElementToShorten
Use it more instead of the constructor calls
This commit is contained in:
+22
-22
@@ -539,10 +539,10 @@ private class ElementsToShortenCollector(
|
|||||||
yieldAll(qualifiersToShorten)
|
yieldAll(qualifiersToShorten)
|
||||||
}.filter { starImport == it.importAllInParent }.mapNotNull { it.nameToImport }.distinct()
|
}.filter { starImport == it.importAllInParent }.mapNotNull { it.nameToImport }.distinct()
|
||||||
|
|
||||||
private fun findFakePackageToShorten(typeElement: KtUserType): ShortenType? {
|
private fun findFakePackageToShorten(typeElement: KtUserType): ElementToShorten? {
|
||||||
val deepestTypeWithQualifier = typeElement.qualifiedTypesWithSelf.last()
|
val deepestTypeWithQualifier = typeElement.qualifiedTypesWithSelf.last()
|
||||||
|
|
||||||
return if (deepestTypeWithQualifier.hasFakeRootPrefix()) ShortenType(deepestTypeWithQualifier) else null
|
return if (deepestTypeWithQualifier.hasFakeRootPrefix()) createElementToShorten(deepestTypeWithQualifier) else null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun processTypeQualifier(resolvedQualifier: FirResolvedQualifier) {
|
fun processTypeQualifier(resolvedQualifier: FirResolvedQualifier) {
|
||||||
@@ -783,13 +783,9 @@ private class ElementsToShortenCollector(
|
|||||||
val importDirectiveForReferencedSymbol = referenceExpression.containingKtFile.importDirectives.firstOrNull {
|
val importDirectiveForReferencedSymbol = referenceExpression.containingKtFile.importDirectives.firstOrNull {
|
||||||
it.importedFqName == referencedSymbolFqName && it.alias != null
|
it.importedFqName == referencedSymbolFqName && it.alias != null
|
||||||
} ?: return null
|
} ?: return null
|
||||||
return when (referenceExpression) {
|
|
||||||
is KtUserType -> ShortenType(referenceExpression, shortenedRef = importDirectiveForReferencedSymbol.alias?.name)
|
val aliasedName = importDirectiveForReferencedSymbol.alias?.name
|
||||||
is KtDotQualifiedExpression -> ShortenQualifier(
|
return createElementToShorten(referenceExpression, shortenedRef = aliasedName)
|
||||||
referenceExpression, shortenedRef = importDirectiveForReferencedSymbol.alias?.name
|
|
||||||
)
|
|
||||||
else -> error("Unexpected ${referenceExpression::class}")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findClassifierElementsToShorten(
|
private fun findClassifierElementsToShorten(
|
||||||
@@ -808,7 +804,7 @@ private class ElementsToShortenCollector(
|
|||||||
shortenIfAlreadyImportedAsAlias(element, classId.asSingleFqName())?.let { return it }
|
shortenIfAlreadyImportedAsAlias(element, classId.asSingleFqName())?.let { return it }
|
||||||
|
|
||||||
if (shortenClassifierIfAlreadyImported(classId, element, classSymbol, positionScopes)) {
|
if (shortenClassifierIfAlreadyImported(classId, element, classSymbol, positionScopes)) {
|
||||||
return createElementToShorten(element, null, false)
|
return createElementToShorten(element)
|
||||||
}
|
}
|
||||||
if (option == ShortenStrategy.SHORTEN_IF_ALREADY_IMPORTED) continue
|
if (option == ShortenStrategy.SHORTEN_IF_ALREADY_IMPORTED) continue
|
||||||
|
|
||||||
@@ -836,7 +832,7 @@ private class ElementsToShortenCollector(
|
|||||||
createElementToShorten(element, classId.asSingleFqName(), importAllInParent)
|
createElementToShorten(element, classId.asSingleFqName(), importAllInParent)
|
||||||
}
|
}
|
||||||
// Otherwise, just shorten it and don't alter import statements
|
// Otherwise, just shorten it and don't alter import statements
|
||||||
else -> createElementToShorten(element, null, false)
|
else -> createElementToShorten(element)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
importedClassifierOverwritesAvailableClassifier(availableClassifier, importAllInParent) -> {
|
importedClassifierOverwritesAvailableClassifier(availableClassifier, importAllInParent) -> {
|
||||||
@@ -847,10 +843,15 @@ private class ElementsToShortenCollector(
|
|||||||
return findFakePackageToShorten(allQualifiedElements.last())
|
return findFakePackageToShorten(allQualifiedElements.last())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createElementToShorten(element: KtElement, nameToImport: FqName?, importAllInParent: Boolean): ElementToShorten {
|
private fun createElementToShorten(
|
||||||
|
element: KtElement,
|
||||||
|
nameToImport: FqName? = null,
|
||||||
|
importAllInParent: Boolean = false,
|
||||||
|
shortenedRef: String? = null,
|
||||||
|
): ElementToShorten {
|
||||||
return when (element) {
|
return when (element) {
|
||||||
is KtUserType -> ShortenType(element, shortenedRef = null, nameToImport, importAllInParent)
|
is KtUserType -> ShortenType(element, shortenedRef, nameToImport, importAllInParent)
|
||||||
is KtDotQualifiedExpression -> ShortenQualifier(element, shortenedRef = null, nameToImport, importAllInParent)
|
is KtDotQualifiedExpression -> ShortenQualifier(element, shortenedRef, nameToImport, importAllInParent)
|
||||||
else -> error("Unexpected ${element::class}")
|
else -> error("Unexpected ${element::class}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1067,7 +1068,7 @@ private class ElementsToShortenCollector(
|
|||||||
val scopes = shorteningContext.findScopesAtPosition(qualifiedProperty, getNamesToImport(), towerContextProvider) ?: return
|
val scopes = shorteningContext.findScopesAtPosition(qualifiedProperty, getNamesToImport(), towerContextProvider) ?: return
|
||||||
val availableCallables = shorteningContext.findPropertiesInScopes(scopes, propertySymbol.name)
|
val availableCallables = shorteningContext.findPropertiesInScopes(scopes, propertySymbol.name)
|
||||||
if (availableCallables.isNotEmpty() && shortenIfAlreadyImported(firPropertyAccess, propertySymbol, qualifiedProperty)) {
|
if (availableCallables.isNotEmpty() && shortenIfAlreadyImported(firPropertyAccess, propertySymbol, qualifiedProperty)) {
|
||||||
addElementToShorten(ShortenQualifier(qualifiedProperty))
|
addElementToShorten(createElementToShorten(qualifiedProperty))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (option == ShortenStrategy.SHORTEN_IF_ALREADY_IMPORTED) return
|
if (option == ShortenStrategy.SHORTEN_IF_ALREADY_IMPORTED) return
|
||||||
@@ -1118,7 +1119,7 @@ private class ElementsToShortenCollector(
|
|||||||
val scopes = shorteningContext.findScopesAtPosition(callExpression, getNamesToImport(), towerContextProvider) ?: return
|
val scopes = shorteningContext.findScopesAtPosition(callExpression, getNamesToImport(), towerContextProvider) ?: return
|
||||||
val availableCallables = shorteningContext.findFunctionsInScopes(scopes, calledSymbol.name)
|
val availableCallables = shorteningContext.findFunctionsInScopes(scopes, calledSymbol.name)
|
||||||
if (availableCallables.isNotEmpty() && shortenIfAlreadyImported(functionCall, calledSymbol, callExpression)) {
|
if (availableCallables.isNotEmpty() && shortenIfAlreadyImported(functionCall, calledSymbol, callExpression)) {
|
||||||
addElementToShorten(ShortenQualifier(qualifiedCallExpression))
|
addElementToShorten(createElementToShorten(qualifiedCallExpression))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (option == ShortenStrategy.SHORTEN_IF_ALREADY_IMPORTED) return
|
if (option == ShortenStrategy.SHORTEN_IF_ALREADY_IMPORTED) return
|
||||||
@@ -1151,17 +1152,16 @@ private class ElementsToShortenCollector(
|
|||||||
when {
|
when {
|
||||||
matchedCallables.isEmpty() -> {
|
matchedCallables.isEmpty() -> {
|
||||||
if (nameToImport == null || option == ShortenStrategy.SHORTEN_IF_ALREADY_IMPORTED) return
|
if (nameToImport == null || option == ShortenStrategy.SHORTEN_IF_ALREADY_IMPORTED) return
|
||||||
ShortenQualifier(
|
createElementToShorten(
|
||||||
qualifiedCallExpression,
|
qualifiedCallExpression,
|
||||||
shortenedRef = null,
|
|
||||||
nameToImport,
|
nameToImport,
|
||||||
importAllInParent = option == ShortenStrategy.SHORTEN_AND_STAR_IMPORT
|
importAllInParent = option == ShortenStrategy.SHORTEN_AND_STAR_IMPORT
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// Respect caller's request to star import this symbol.
|
// Respect caller's request to star import this symbol.
|
||||||
matchedCallables.any { it.importKind == ImportKind.EXPLICIT } && option == ShortenStrategy.SHORTEN_AND_STAR_IMPORT ->
|
matchedCallables.any { it.importKind == ImportKind.EXPLICIT } && option == ShortenStrategy.SHORTEN_AND_STAR_IMPORT ->
|
||||||
ShortenQualifier(qualifiedCallExpression, null, nameToImport, importAllInParent = true)
|
createElementToShorten(qualifiedCallExpression, nameToImport, importAllInParent = true)
|
||||||
else -> ShortenQualifier(qualifiedCallExpression)
|
else -> createElementToShorten(qualifiedCallExpression)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> findFakePackageToShorten(qualifiedCallExpression)
|
else -> findFakePackageToShorten(qualifiedCallExpression)
|
||||||
@@ -1207,9 +1207,9 @@ private class ElementsToShortenCollector(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findFakePackageToShorten(wholeQualifiedExpression: KtDotQualifiedExpression): ShortenQualifier? {
|
private fun findFakePackageToShorten(wholeQualifiedExpression: KtDotQualifiedExpression): ElementToShorten? {
|
||||||
val deepestQualifier = wholeQualifiedExpression.qualifiedExpressionsWithSelf.last()
|
val deepestQualifier = wholeQualifiedExpression.qualifiedExpressionsWithSelf.last()
|
||||||
return if (deepestQualifier.hasFakeRootPrefix()) ShortenQualifier(deepestQualifier) else null
|
return if (deepestQualifier.hasFakeRootPrefix()) createElementToShorten(deepestQualifier) else null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtElement.isInsideOf(another: KtElement): Boolean = another.textRange.contains(textRange)
|
private fun KtElement.isInsideOf(another: KtElement): Boolean = another.textRange.contains(textRange)
|
||||||
|
|||||||
Reference in New Issue
Block a user