K2: Update reference shortener to handle import alias

The existing reference shortener does not use import alias when it
shortens a symbol. Instead, it adds a new import directive for the
symbol that is already imported. This commit updates reference shortener
to let it reuse the existing import alias rather than adding a new one:

 1. When shortening a symbol, check whether the symbol is already
    imported.
 2. If it is already imported by an import alias, keep the symbol
    reference expression and the import alias as a string together in
    `ShortenCommand`.

The actual PSI update (shortening) based on the ShortenCommand is done
by IntelliJ.

^KTIJ-27205
This commit is contained in:
Jaebaek Seo
2023-10-03 12:19:24 -07:00
committed by teamcity
parent 02c12ae26f
commit e80f044847
24 changed files with 379 additions and 39 deletions
@@ -8,10 +8,7 @@ package org.jetbrains.kotlin.analysis.api.descriptors.components
import com.intellij.openapi.util.TextRange
import com.intellij.psi.SmartPointerManager
import com.intellij.psi.SmartPsiElementPointer
import org.jetbrains.kotlin.analysis.api.components.KtReferenceShortener
import org.jetbrains.kotlin.analysis.api.components.ShortenCommand
import org.jetbrains.kotlin.analysis.api.components.ShortenOptions
import org.jetbrains.kotlin.analysis.api.components.ShortenStrategy
import org.jetbrains.kotlin.analysis.api.components.*
import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession
import org.jetbrains.kotlin.analysis.api.descriptors.components.base.Fe10KtAnalysisSessionComponent
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
@@ -19,9 +16,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol
import org.jetbrains.kotlin.kdoc.psi.impl.KDocName
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtUserType
internal class KtFe10ReferenceShortener(
override val analysisSession: KtFe10AnalysisSession,
@@ -44,8 +39,8 @@ internal class KtFe10ReferenceShortener(
override val targetFile: SmartPsiElementPointer<KtFile> get() = ktFilePointer
override val importsToAdd: Set<FqName> get() = emptySet()
override val starImportsToAdd: Set<FqName> get() = emptySet()
override val typesToShorten: List<SmartPsiElementPointer<KtUserType>> get() = emptyList()
override val qualifiersToShorten: List<SmartPsiElementPointer<KtDotQualifiedExpression>> get() = emptyList()
override val listOfTypeToShortenInfo: List<TypeToShortenInfo> get() = emptyList()
override val listOfQualifierToShortenInfo: List<QualifierToShortenInfo> get() = emptyList()
override val kDocQualifiersToShorten: List<SmartPsiElementPointer<KDocName>> get() = emptyList()
override val isEmpty: Boolean get() = true