KT-62676 [AA] Introduce ShortenOptions class

This class should contain 'global' settings for reference shortening;
ATM it only contains the `removeThis` flag

In the future this setting might be merged together with
`ShortenStrategy` to be computed on per-element basis
This commit is contained in:
Roman Golyshev
2023-10-26 20:27:12 +02:00
committed by teamcity
parent adf7ee4535
commit 4ca1f9d492
5 changed files with 36 additions and 2 deletions
@@ -10,6 +10,7 @@ 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
import org.jetbrains.kotlin.analysis.api.components.ShortenOptions
import org.jetbrains.kotlin.analysis.api.components.ShortenStrategy import org.jetbrains.kotlin.analysis.api.components.ShortenStrategy
import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession import org.jetbrains.kotlin.analysis.api.descriptors.KtFe10AnalysisSession
import org.jetbrains.kotlin.analysis.api.descriptors.components.base.Fe10KtAnalysisSessionComponent import org.jetbrains.kotlin.analysis.api.descriptors.components.base.Fe10KtAnalysisSessionComponent
@@ -31,6 +32,7 @@ internal class KtFe10ReferenceShortener(
override fun collectShortenings( override fun collectShortenings(
file: KtFile, file: KtFile,
selection: TextRange, selection: TextRange,
shortenOptions: ShortenOptions,
classShortenStrategy: (KtClassLikeSymbol) -> ShortenStrategy, classShortenStrategy: (KtClassLikeSymbol) -> ShortenStrategy,
callableShortenStrategy: (KtCallableSymbol) -> ShortenStrategy, callableShortenStrategy: (KtCallableSymbol) -> ShortenStrategy,
): ShortenCommand { ): ShortenCommand {
@@ -12,6 +12,7 @@ import com.intellij.psi.SmartPsiElementPointer
import org.jetbrains.kotlin.KtFakeSourceElementKind import org.jetbrains.kotlin.KtFakeSourceElementKind
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
import org.jetbrains.kotlin.analysis.api.components.ShortenOptions
import org.jetbrains.kotlin.analysis.api.components.ShortenStrategy import org.jetbrains.kotlin.analysis.api.components.ShortenStrategy
import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession import org.jetbrains.kotlin.analysis.api.fir.KtFirAnalysisSession
import org.jetbrains.kotlin.analysis.api.fir.components.ElementsToShortenCollector.PartialOrderOfScope.Companion.toPartialOrder import org.jetbrains.kotlin.analysis.api.fir.components.ElementsToShortenCollector.PartialOrderOfScope.Companion.toPartialOrder
@@ -78,6 +79,7 @@ internal class KtFirReferenceShortener(
override fun collectShortenings( override fun collectShortenings(
file: KtFile, file: KtFile,
selection: TextRange, selection: TextRange,
shortenOptions: ShortenOptions,
classShortenStrategy: (KtClassLikeSymbol) -> ShortenStrategy, classShortenStrategy: (KtClassLikeSymbol) -> ShortenStrategy,
callableShortenStrategy: (KtCallableSymbol) -> ShortenStrategy callableShortenStrategy: (KtCallableSymbol) -> ShortenStrategy
): ShortenCommand { ): ShortenCommand {
@@ -21,7 +21,12 @@ abstract class AbstractReferenceShortenerForWholeFileTest : AbstractAnalysisApiB
val shortenings = executeOnPooledThreadInReadAction { val shortenings = executeOnPooledThreadInReadAction {
analyseForTest(file) { analyseForTest(file) {
ShortenStrategy.values().map { option -> ShortenStrategy.values().map { option ->
val shorteningsForOption = collectPossibleReferenceShortenings(file, file.textRange, { option }, { option }) val shorteningsForOption = collectPossibleReferenceShortenings(
file,
file.textRange,
classShortenStrategy = { option },
callableShortenStrategy = { option }
)
Pair(option.name, shorteningsForOption) Pair(option.name, shorteningsForOption)
} }
@@ -28,7 +28,13 @@ abstract class AbstractReferenceShortenerTest : AbstractAnalysisApiBasedSingleMo
val shortenings = executeOnPooledThreadInReadAction { val shortenings = executeOnPooledThreadInReadAction {
analyseForTest(element) { analyseForTest(element) {
ShortenStrategy.values().map { option -> ShortenStrategy.values().map { option ->
Pair(option.name, collectPossibleReferenceShorteningsInElement(element, { option }, { option })) val shorteningsForOption = collectPossibleReferenceShorteningsInElement(
element,
classShortenStrategy = { option },
callableShortenStrategy = { option }
)
Pair(option.name, shorteningsForOption)
} }
} }
} }
@@ -20,6 +20,20 @@ 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
/**
* @property removeThis If set to `true`, reference shortener will detect redundant `this` qualifiers
* and will collect them to [ShortenCommand.qualifiersToShorten].
*/
public data class ShortenOptions(
public val removeThis: Boolean = false,
) {
public companion object {
public val DEFAULT: ShortenOptions = ShortenOptions()
public val ALL_ENABLED: ShortenOptions = ShortenOptions(removeThis = true)
}
}
public enum class ShortenStrategy { public enum class ShortenStrategy {
/** Skip shortening references to this symbol. */ /** Skip shortening references to this symbol. */
DO_NOT_SHORTEN, DO_NOT_SHORTEN,
@@ -78,6 +92,7 @@ public abstract class KtReferenceShortener : KtAnalysisSessionComponent() {
public abstract fun collectShortenings( public abstract fun collectShortenings(
file: KtFile, file: KtFile,
selection: TextRange, selection: TextRange,
shortenOptions: ShortenOptions,
classShortenStrategy: (KtClassLikeSymbol) -> ShortenStrategy, classShortenStrategy: (KtClassLikeSymbol) -> ShortenStrategy,
callableShortenStrategy: (KtCallableSymbol) -> ShortenStrategy callableShortenStrategy: (KtCallableSymbol) -> ShortenStrategy
): ShortenCommand ): ShortenCommand
@@ -97,6 +112,7 @@ public interface KtReferenceShortenerMixIn : KtAnalysisSessionMixIn {
public fun collectPossibleReferenceShortenings( public fun collectPossibleReferenceShortenings(
file: KtFile, file: KtFile,
selection: TextRange = file.textRange, selection: TextRange = file.textRange,
shortenOptions: ShortenOptions = ShortenOptions.DEFAULT,
classShortenStrategy: (KtClassLikeSymbol) -> ShortenStrategy = defaultClassShortenStrategy, classShortenStrategy: (KtClassLikeSymbol) -> ShortenStrategy = defaultClassShortenStrategy,
callableShortenStrategy: (KtCallableSymbol) -> ShortenStrategy = defaultCallableShortenStrategy callableShortenStrategy: (KtCallableSymbol) -> ShortenStrategy = defaultCallableShortenStrategy
): ShortenCommand = ): ShortenCommand =
@@ -104,6 +120,7 @@ public interface KtReferenceShortenerMixIn : KtAnalysisSessionMixIn {
analysisSession.referenceShortener.collectShortenings( analysisSession.referenceShortener.collectShortenings(
file, file,
selection, selection,
shortenOptions,
classShortenStrategy, classShortenStrategy,
callableShortenStrategy callableShortenStrategy
) )
@@ -120,6 +137,7 @@ public interface KtReferenceShortenerMixIn : KtAnalysisSessionMixIn {
*/ */
public fun collectPossibleReferenceShorteningsInElement( public fun collectPossibleReferenceShorteningsInElement(
element: KtElement, element: KtElement,
shortenOptions: ShortenOptions = ShortenOptions.DEFAULT,
classShortenStrategy: (KtClassLikeSymbol) -> ShortenStrategy = defaultClassShortenStrategy, classShortenStrategy: (KtClassLikeSymbol) -> ShortenStrategy = defaultClassShortenStrategy,
callableShortenStrategy: (KtCallableSymbol) -> ShortenStrategy = defaultCallableShortenStrategy callableShortenStrategy: (KtCallableSymbol) -> ShortenStrategy = defaultCallableShortenStrategy
): ShortenCommand = ): ShortenCommand =
@@ -127,6 +145,7 @@ public interface KtReferenceShortenerMixIn : KtAnalysisSessionMixIn {
analysisSession.referenceShortener.collectShortenings( analysisSession.referenceShortener.collectShortenings(
element.containingKtFile, element.containingKtFile,
element.textRange, element.textRange,
shortenOptions,
classShortenStrategy, classShortenStrategy,
callableShortenStrategy callableShortenStrategy
) )