From e3b92fe5f38a7dcb8a4ecaadac8ca85b57ec7c69 Mon Sep 17 00:00:00 2001 From: Tianyu Geng Date: Thu, 14 Oct 2021 11:06:29 -0700 Subject: [PATCH] FIR IDE: move helper to shorten refs to IDE --- .../kotlin_test_wasm_js_1_6_255_SNAPSHOT.xml | 6 ++ .../api/components/KtReferenceShortener.kt | 73 +++++-------------- 2 files changed, 24 insertions(+), 55 deletions(-) create mode 100644 .idea/artifacts/kotlin_test_wasm_js_1_6_255_SNAPSHOT.xml diff --git a/.idea/artifacts/kotlin_test_wasm_js_1_6_255_SNAPSHOT.xml b/.idea/artifacts/kotlin_test_wasm_js_1_6_255_SNAPSHOT.xml new file mode 100644 index 00000000000..6ec66790f30 --- /dev/null +++ b/.idea/artifacts/kotlin_test_wasm_js_1_6_255_SNAPSHOT.xml @@ -0,0 +1,6 @@ + + + $PROJECT_DIR$/libraries/kotlin.test/wasm/build/libs + + + \ No newline at end of file diff --git a/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtReferenceShortener.kt b/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtReferenceShortener.kt index 603b9db8673..88f549a6560 100644 --- a/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtReferenceShortener.kt +++ b/analysis/analysis-api/src/org/jetbrains/kotlin/analysis/api/components/KtReferenceShortener.kt @@ -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