FIR IDE: move helper to shorten refs to IDE

This commit is contained in:
Tianyu Geng
2021-10-14 11:06:29 -07:00
committed by TeamCityServer
parent 3877dc8cc3
commit e3b92fe5f3
2 changed files with 24 additions and 55 deletions
@@ -5,14 +5,12 @@
package org.jetbrains.kotlin.analysis.api.components
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.analysis.api.analyse
import org.jetbrains.kotlin.analysis.api.components.ShortenOption.Companion.defaultCallableShortenOption
import org.jetbrains.kotlin.analysis.api.components.ShortenOption.Companion.defaultClassShortenOption
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.analysis.api.tokens.HackToForceAllowRunningAnalyzeOnEDT
import org.jetbrains.kotlin.analysis.api.tokens.hackyAllowRunningOnEdt
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
@@ -27,7 +25,22 @@ public enum class ShortenOption {
SHORTEN_AND_IMPORT,
/** Shorten references to this symbol and import this symbol and all its sibling symbols with star import on the parent. */
SHORTEN_AND_STAR_IMPORT
SHORTEN_AND_STAR_IMPORT;
public companion object {
public val defaultClassShortenOption: (KtClassLikeSymbol) -> ShortenOption = {
if (it.classIdIfNonLocal?.isNestedClass == true) {
SHORTEN_IF_ALREADY_IMPORTED
} else {
SHORTEN_AND_IMPORT
}
}
public val defaultCallableShortenOption: (KtCallableSymbol) -> ShortenOption = { symbol ->
if (symbol is KtEnumEntrySymbol) DO_NOT_SHORTEN
else SHORTEN_AND_IMPORT
}
}
}
public abstract class KtReferenceShortener : KtAnalysisSessionComponent() {
@@ -40,56 +53,6 @@ public abstract class KtReferenceShortener : KtAnalysisSessionComponent() {
}
public interface KtReferenceShortenerMixIn : KtAnalysisSessionMixIn {
public companion object {
private val defaultClassShortenOption: (KtClassLikeSymbol) -> ShortenOption = {
if (it.classIdIfNonLocal?.isNestedClass == true) {
ShortenOption.SHORTEN_IF_ALREADY_IMPORTED
} else {
ShortenOption.SHORTEN_AND_IMPORT
}
}
private val defaultCallableShortenOption: (KtCallableSymbol) -> ShortenOption = { symbol ->
if (symbol is KtEnumEntrySymbol) ShortenOption.DO_NOT_SHORTEN
else ShortenOption.SHORTEN_AND_IMPORT
}
/**
* Shorten references in the given [element]. See [shortenReferencesInRange] for more details.
*/
public fun shortenReferences(
element: KtElement,
classShortenOption: (KtClassLikeSymbol) -> ShortenOption = defaultClassShortenOption,
callableShortenOption: (KtCallableSymbol) -> ShortenOption = defaultCallableShortenOption
): Unit = shortenReferencesInRange(
element.containingKtFile,
element.textRange,
classShortenOption,
callableShortenOption
)
/**
* Shorten references in the given [file] and [range]. The function must be invoked on EDT thread because it modifies the underlying
* PSI. This method analyse Kotlin code and hence could block the EDT thread for longer period of time. Hence, this method should be
* called only to shorten references in *newly generated code* by IDE actions. In other cases, please consider using
* [org.jetbrains.kotlin.analysis.api.components.KtReferenceShortenerMixIn] in a background thread to perform the analysis and then
* modify PSI on the EDt thread by invoking [org.jetbrains.kotlin.analysis.api.components.ShortenCommand.invokeShortening]. */
@OptIn(HackToForceAllowRunningAnalyzeOnEDT::class)
public fun shortenReferencesInRange(
file: KtFile,
range: TextRange = file.textRange,
classShortenOption: (KtClassLikeSymbol) -> ShortenOption = defaultClassShortenOption,
callableShortenOption: (KtCallableSymbol) -> ShortenOption = defaultCallableShortenOption
) {
ApplicationManager.getApplication().assertIsDispatchThread()
val shortenCommand = hackyAllowRunningOnEdt {
analyse(file) {
collectPossibleReferenceShortenings(file, range, classShortenOption, callableShortenOption)
}
}
shortenCommand.invokeShortening()
}
}
/**
* Collects possible references to shorten. By default, it shortens a fully-qualified members to the outermost class and does not