[Analysis API] extract the data from the ShortenCommandImpl to the base class

^KT-58992
This commit is contained in:
Ilya Kirillov
2023-05-30 18:16:42 +02:00
committed by Space Team
parent fffc921d42
commit 95cf892eec
4 changed files with 28 additions and 26 deletions
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.analysis.api.descriptors.components package org.jetbrains.kotlin.analysis.api.descriptors.components
import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.TextRange
import com.intellij.psi.SmartPointerManager
import com.intellij.psi.SmartPsiElementPointer import com.intellij.psi.SmartPsiElementPointer
import org.jetbrains.kotlin.analysis.api.components.KtReferenceShortener import org.jetbrains.kotlin.analysis.api.components.KtReferenceShortener
import org.jetbrains.kotlin.analysis.api.components.ShortenCommand import org.jetbrains.kotlin.analysis.api.components.ShortenCommand
@@ -15,13 +16,14 @@ import org.jetbrains.kotlin.analysis.api.descriptors.components.base.Fe10KtAnaly
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtUserType import org.jetbrains.kotlin.psi.KtUserType
internal class KtFe10ReferenceShortener( internal class KtFe10ReferenceShortener(
override val analysisSession: KtFe10AnalysisSession override val analysisSession: KtFe10AnalysisSession,
) : KtReferenceShortener(), Fe10KtAnalysisSessionComponent { ) : KtReferenceShortener(), Fe10KtAnalysisSessionComponent {
override val token: KtLifetimeToken override val token: KtLifetimeToken
get() = analysisSession.token get() = analysisSession.token
@@ -30,17 +32,20 @@ internal class KtFe10ReferenceShortener(
file: KtFile, file: KtFile,
selection: TextRange, selection: TextRange,
classShortenOption: (KtClassLikeSymbol) -> ShortenOption, classShortenOption: (KtClassLikeSymbol) -> ShortenOption,
callableShortenOption: (KtCallableSymbol) -> ShortenOption callableShortenOption: (KtCallableSymbol) -> ShortenOption,
): ShortenCommand { ): ShortenCommand {
// Compiler implementation does nothing. // Compiler implementation does nothing.
// Descriptor-based shortening is implemented on the IDE plugin side. // Descriptor-based shortening is implemented on the IDE plugin side.
val ktFilePointer = SmartPointerManager.createPointer(file)
return object : ShortenCommand { return object : ShortenCommand {
override val isEmpty: Boolean override val targetFile: SmartPsiElementPointer<KtFile> get() = ktFilePointer
get() = true override val importsToAdd: List<FqName> get() = emptyList()
override val starImportsToAdd: List<FqName> get() = emptyList()
override val typesToShorten: List<SmartPsiElementPointer<KtUserType>> get() = emptyList()
override val qualifiersToShorten: List<SmartPsiElementPointer<KtDotQualifiedExpression>> get() = emptyList()
override fun getTypesToShorten(): List<SmartPsiElementPointer<KtUserType>> = emptyList() override val isEmpty: Boolean get() = true
override fun getQualifiersToShorten(): List<SmartPsiElementPointer<KtDotQualifiedExpression>> = emptyList()
override fun invokeShortening(): List<KtElement> = emptyList() override fun invokeShortening(): List<KtElement> = emptyList()
} }
@@ -1060,11 +1060,11 @@ private class ElementsToShortenCollector(
} }
private class ShortenCommandImpl( private class ShortenCommandImpl(
private val targetFile: SmartPsiElementPointer<KtFile>, override val targetFile: SmartPsiElementPointer<KtFile>,
private val importsToAdd: List<FqName>, override val importsToAdd: List<FqName>,
private val starImportsToAdd: List<FqName>, override val starImportsToAdd: List<FqName>,
private val typesToShorten: List<SmartPsiElementPointer<KtUserType>>, override val typesToShorten: List<SmartPsiElementPointer<KtUserType>>,
private val qualifiersToShorten: List<SmartPsiElementPointer<KtDotQualifiedExpression>>, override val qualifiersToShorten: List<SmartPsiElementPointer<KtDotQualifiedExpression>>,
) : ShortenCommand { ) : ShortenCommand {
override fun invokeShortening(): List<KtElement> { override fun invokeShortening(): List<KtElement> {
@@ -1095,12 +1095,6 @@ private class ShortenCommandImpl(
// } // }
return shorteningResults return shorteningResults
} }
override val isEmpty: Boolean get() = typesToShorten.isEmpty() && qualifiersToShorten.isEmpty()
override fun getTypesToShorten(): List<SmartPsiElementPointer<KtUserType>> = typesToShorten
override fun getQualifiersToShorten(): List<SmartPsiElementPointer<KtDotQualifiedExpression>> = qualifiersToShorten
} }
private fun KtUserType.hasFakeRootPrefix(): Boolean = private fun KtUserType.hasFakeRootPrefix(): Boolean =
@@ -14,12 +14,12 @@ internal object ShorteningResultsRenderer {
return return
} }
shortening.getTypesToShorten().forEach { userType -> shortening.typesToShorten.forEach { userType ->
userType.element?.text?.let { userType.element?.text?.let {
appendLine("[type] $it") appendLine("[type] $it")
} }
} }
shortening.getQualifiersToShorten().forEach { qualifier -> shortening.qualifiersToShorten.forEach { qualifier ->
qualifier.element?.text?.let { qualifier.element?.text?.let {
appendLine("[qualifier] $it") appendLine("[qualifier] $it")
} }
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtClassLikeSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KtEnumEntrySymbol 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.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
@@ -118,12 +119,14 @@ public interface KtReferenceShortenerMixIn : KtAnalysisSessionMixIn {
} }
public interface ShortenCommand { public interface ShortenCommand {
/** public val targetFile: SmartPsiElementPointer<KtFile>
* Shortens target elements, adds necessary import, and returns a list of the shortened result elements. public val importsToAdd: List<FqName>
*/ public val starImportsToAdd: List<FqName>
public fun invokeShortening(): List<KtElement> public val typesToShorten: List<SmartPsiElementPointer<KtUserType>>
public val qualifiersToShorten: List<SmartPsiElementPointer<KtDotQualifiedExpression>>
public val isEmpty: Boolean public val isEmpty: Boolean
public fun getTypesToShorten(): List<SmartPsiElementPointer<KtUserType>> get() = typesToShorten.isEmpty() && qualifiersToShorten.isEmpty()
public fun getQualifiersToShorten(): List<SmartPsiElementPointer<KtDotQualifiedExpression>>
public fun invokeShortening(): List<KtElement>
} }