[AA] Shortener: allow import if requested import kind has high priority

This commit is contained in:
aleksandrina-streltsova
2023-06-12 17:58:04 +03:00
committed by teamcity
parent a96e2f37e7
commit 57cbab7d8e
10 changed files with 94 additions and 7 deletions
@@ -133,12 +133,12 @@ private enum class ImportKind {
/** Explicitly imported by user. */
EXPLICIT,
/** Explicitly imported by Kotlin default. For example, `kotlin.String`. */
DEFAULT_EXPLICIT,
/** Implicitly imported from package. */
PACKAGE,
/** Explicitly imported by Kotlin default. For example, `kotlin.String`. */
DEFAULT_EXPLICIT,
/** Star imported (star import) by user. */
STAR,
@@ -158,6 +158,12 @@ private enum class ImportKind {
else -> LOCAL
}
}
fun fromShortenOption(option: ShortenOption): ImportKind? = when (option) {
ShortenOption.SHORTEN_AND_IMPORT -> EXPLICIT
ShortenOption.SHORTEN_AND_STAR_IMPORT -> STAR
else -> null
}
}
}
@@ -974,10 +980,12 @@ private class ElementsToShortenCollector(
val nameToImport = shorteningContext.convertToImportableName(calledSymbol)
val (matchedCallables, otherCallables) = availableCallables.partition { it.symbol.callableId == calledSymbol.callableId }
val importKindFromOption = ImportKind.fromShortenOption(option)
val importKind = matchedCallables.minOfOrNull { it.importKind } ?: importKindFromOption ?: return
val callToShorten = when {
// TODO: instead of allowing import only if the other callables are all with kind `DEFAULT_STAR`, we should allow import if
// the requested import kind has higher priority than the available symbols.
otherCallables.all { it.importKind == ImportKind.DEFAULT_STAR } -> {
otherCallables.all { importKind.hasHigherPriorityThan(it.importKind) } -> {
when {
matchedCallables.isEmpty() -> {
if (nameToImport == null || option == ShortenOption.SHORTEN_IF_ALREADY_IMPORTED) return
@@ -52,6 +52,24 @@ public class FirIdeNormalAnalysisSourceModuleReferenceShortenerTestGenerated ext
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/annotaiton.kt");
}
@Test
@TestMetadata("callableFromDefaultImport.kt")
public void testCallableFromDefaultImport() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/callableFromDefaultImport.kt");
}
@Test
@TestMetadata("callableFromDefaultImport2.kt")
public void testCallableFromDefaultImport2() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/callableFromDefaultImport2.kt");
}
@Test
@TestMetadata("callableFromExplicitImport.kt")
public void testCallableFromExplicitImport() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/callableFromExplicitImport.kt");
}
@Test
@TestMetadata("classScopes.kt")
public void testClassScopes() throws Exception {
@@ -52,6 +52,24 @@ public class FirStandaloneNormalAnalysisSourceModuleReferenceShortenerTestGenera
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/annotaiton.kt");
}
@Test
@TestMetadata("callableFromDefaultImport.kt")
public void testCallableFromDefaultImport() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/callableFromDefaultImport.kt");
}
@Test
@TestMetadata("callableFromDefaultImport2.kt")
public void testCallableFromDefaultImport2() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/callableFromDefaultImport2.kt");
}
@Test
@TestMetadata("callableFromExplicitImport.kt")
public void testCallableFromExplicitImport() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/callableFromExplicitImport.kt");
}
@Test
@TestMetadata("classScopes.kt")
public void testClassScopes() throws Exception {
@@ -0,0 +1,4 @@
// WITH_STDLIB
// FILE: main.kt
fun test() = <expr>kotlin.text.charset("")</expr>
@@ -0,0 +1,8 @@
Before shortening: kotlin.text.charset("")
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[qualifier] kotlin.text.charset("")
with SHORTEN_AND_IMPORT:
[qualifier] kotlin.text.charset("")
with SHORTEN_AND_STAR_IMPORT:
[qualifier] kotlin.text.charset("")
@@ -0,0 +1,7 @@
// WITH_STDLIB
// FILE: main.kt
import java.nio.charset.Charset
fun charset(charsetName: String): Charset = Charset.forName(charsetName)
fun test() = <expr>kotlin.text.charset("")</expr>
@@ -0,0 +1,5 @@
Before shortening: kotlin.text.charset("")
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
with SHORTEN_AND_STAR_IMPORT:
@@ -0,0 +1,13 @@
// FILE: main.kt
package a.b.c
fun foo() {}
fun test() {
<expr>dependency.foo()</expr>
}
// FILE: dependency.kt
package dependency
fun foo() {}
@@ -0,0 +1,6 @@
Before shortening: dependency.foo()
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
[qualifier] dependency.foo()
with SHORTEN_AND_STAR_IMPORT:
@@ -3,6 +3,6 @@ with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
[qualifier] a.b.c.MyClass
with SHORTEN_AND_IMPORT:
[qualifier] a.b.c.MyClass.Companion
[qualifier] a.b.c.MyClass.Companion.foo()
with SHORTEN_AND_STAR_IMPORT:
[qualifier] a.b.c.MyClass.Companion