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
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.test.services.assertions
* Note that it tests shortening only a single expression between <expr> and </expr> in the first file.
*/
abstract class AbstractReferenceShortenerTest : AbstractAnalysisApiBasedSingleModuleTest() {
override fun doTestByFileStructure(ktFiles: List<KtFile>, module: TestModule, testServices: TestServices) {
val element = testServices.expressionMarkerProvider.getSelectedElementOfType<KtElement>(ktFiles.first())
@@ -14,14 +14,14 @@ internal object ShorteningResultsRenderer {
return
}
shortening.typesToShorten.forEach { userType ->
shortening.listOfTypeToShortenInfo.forEach { (userType, shortenedRef) ->
userType.element?.text?.let {
appendLine("[type] $it")
appendLine("[type] $it${shortenedRef?.let { ref -> " -> $ref" } ?: ""}")
}
}
shortening.qualifiersToShorten.forEach { qualifier ->
shortening.listOfQualifierToShortenInfo.forEach { (qualifier, shortenedRef) ->
qualifier.element?.text?.let {
appendLine("[qualifier] $it")
appendLine("[qualifier] $it${shortenedRef?.let { ref -> " -> $ref" } ?: ""}")
}
}
shortening.kDocQualifiersToShorten.forEach { kdoc ->