[FIR] Return K2 reference shortener result

This commit sets a list of `KtElement` as the return type of
`ShortenCommand::invokeShortening()`. It allows us to take the result
i.e., shortened PSIs. This can be used, for example, when we want to
run code-format only on the shortened PSI after running the reference
shortener.

^KT-57636 Fixed
This commit is contained in:
Jaebaek Seo
2023-03-23 18:19:51 -07:00
committed by Roman Golyshev
parent f810d435f4
commit 3fbd3d7e20
3 changed files with 13 additions and 8 deletions
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtUserType
@@ -41,7 +42,7 @@ internal class KtFe10ReferenceShortener(
override fun getQualifiersToShorten(): List<SmartPsiElementPointer<KtDotQualifiedExpression>> = emptyList()
override fun invokeShortening() {}
override fun invokeShortening(): List<KtElement> = emptyList()
}
}
}
@@ -667,8 +667,6 @@ private class ElementsToShortenCollector(
// No class with name `classId.shortClassName` is present in the scope. Hence, we can safely import the name and shorten
// the reference.
availableClassifier == null -> {
// Caller indicates don't shorten if doing that needs importing more names. Hence, we just skip.
if (option == ShortenOption.SHORTEN_IF_ALREADY_IMPORTED) continue
return createElementToShorten(
element,
classId.asSingleFqName(),
@@ -1070,9 +1068,9 @@ private class ShortenCommandImpl(
private val qualifiersToShorten: List<SmartPsiElementPointer<KtDotQualifiedExpression>>,
) : ShortenCommand {
override fun invokeShortening() {
override fun invokeShortening(): List<KtElement> {
// if the file has been invalidated, there's nothing we can shorten
val targetFile = targetFile.element ?: return
val targetFile = targetFile.element ?: return emptyList()
for (nameToImport in importsToAdd) {
addImportToFile(targetFile.project, targetFile, nameToImport)
@@ -1082,18 +1080,21 @@ private class ShortenCommandImpl(
addImportToFile(targetFile.project, targetFile, nameToImport, allUnder = true)
}
val shorteningResults = mutableListOf<KtElement>()
//todo
// PostprocessReformattingAspect.getInstance(targetFile.project).disablePostprocessFormattingInside {
for (typePointer in typesToShorten) {
val type = typePointer.element ?: continue
type.deleteQualifier()
shorteningResults.add(type)
}
for (callPointer in qualifiersToShorten) {
val call = callPointer.element ?: continue
call.deleteQualifier()
call.deleteQualifier()?.let { shorteningResults.add(it) }
}
// }
return shorteningResults
}
override val isEmpty: Boolean get() = typesToShorten.isEmpty() && qualifiersToShorten.isEmpty()
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtEnumEntrySymbol
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
@@ -119,7 +118,11 @@ public interface KtReferenceShortenerMixIn : KtAnalysisSessionMixIn {
}
public interface ShortenCommand {
public fun invokeShortening()
/**
* Shortens target elements, adds necessary import, and returns a list of the shortened result elements.
*/
public fun invokeShortening(): List<KtElement>
public val isEmpty: Boolean
public fun getTypesToShorten(): List<SmartPsiElementPointer<KtUserType>>
public fun getQualifiersToShorten(): List<SmartPsiElementPointer<KtDotQualifiedExpression>>