[Analysis API] Unwrap anonymous function expressions in reference shortener

Anonymous functions are additionally wrapped into an expression

^KTIJ-26629 Fixed
^KTIJ-26597 Fixed
This commit is contained in:
Roman Golyshev
2023-08-14 12:55:40 +02:00
committed by teamcity
parent bddc18e463
commit 48433bf9fd
9 changed files with 104 additions and 5 deletions
@@ -86,7 +86,7 @@ internal class KtFirReferenceShortener(
val declarationToVisit = file.findSmallestElementOfTypeContainingSelection<KtDeclaration>(selection)
?: file
val firDeclaration = declarationToVisit.getOrBuildFir(firResolveSession) as? FirDeclaration ?: return ShortenCommandImpl(
val firDeclaration = declarationToVisit.getCorrespondingFirDeclaration() ?: return ShortenCommandImpl(
file.createSmartPointer(),
importsToAdd = emptySet(),
starImportsToAdd = emptySet(),
@@ -141,6 +141,18 @@ internal class KtFirReferenceShortener(
)
}
private fun KtElement.getCorrespondingFirDeclaration(): FirDeclaration? {
require(this is KtFile || this is KtDeclaration)
val firElement = getOrBuildFir(firResolveSession)
return when (firElement) {
is FirDeclaration -> firElement
is FirAnonymousFunctionExpression -> firElement.anonymousFunction
else -> null
}
}
private fun buildSymbol(firSymbol: FirBasedSymbol<*>): KtSymbol = analysisSession.firSymbolBuilder.buildSymbol(firSymbol)
}
@@ -153,10 +165,13 @@ private fun FqName.dropFakeRootPrefixIfPresent(): FqName {
private data class AdditionalImports(val simpleImports: Set<FqName>, val starImports: Set<FqName>)
private inline fun <reified T : KtElement> KtFile.findSmallestElementOfTypeContainingSelection(selection: TextRange): T? =
findElementAt(selection.startOffset)
?.parentsOfType<T>(withSelf = true)
?.firstOrNull { selection in it.textRange }
private inline fun <reified T : KtElement> KtFile.findSmallestElementOfTypeContainingSelection(selection: TextRange): T? {
val parents = findElementAt(selection.startOffset)
?.parentsWithSelf
?.toList()
return parents?.filterIsInstance<T>()?.firstOrNull { selection in it.textRange }
}
/**
* How a symbol is imported. The order of the enum entry represents the priority of imports. If a symbol is available from multiple kinds of
@@ -52,6 +52,24 @@ public class FirIdeNormalAnalysisSourceModuleReferenceShortenerTestGenerated ext
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/annotaiton.kt");
}
@Test
@TestMetadata("anonymousFunction_annotation.kt")
public void testAnonymousFunction_annotation() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/anonymousFunction_annotation.kt");
}
@Test
@TestMetadata("anonymousFunction_receiverType.kt")
public void testAnonymousFunction_receiverType() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/anonymousFunction_receiverType.kt");
}
@Test
@TestMetadata("anonymousFunction_returnType.kt")
public void testAnonymousFunction_returnType() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/anonymousFunction_returnType.kt");
}
@Test
@TestMetadata("callInsideScriptExpression.kts")
public void testCallInsideScriptExpression() throws Exception {
@@ -52,6 +52,24 @@ public class FirStandaloneNormalAnalysisSourceModuleReferenceShortenerTestGenera
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/annotaiton.kt");
}
@Test
@TestMetadata("anonymousFunction_annotation.kt")
public void testAnonymousFunction_annotation() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/anonymousFunction_annotation.kt");
}
@Test
@TestMetadata("anonymousFunction_receiverType.kt")
public void testAnonymousFunction_receiverType() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/anonymousFunction_receiverType.kt");
}
@Test
@TestMetadata("anonymousFunction_returnType.kt")
public void testAnonymousFunction_returnType() throws Exception {
runTest("analysis/analysis-api/testData/components/referenceShortener/referenceShortener/anonymousFunction_returnType.kt");
}
@Test
@TestMetadata("callInsideScriptExpression.kts")
public void testCallInsideScriptExpression() throws Exception {
@@ -0,0 +1,9 @@
// FILE: main.kt
package test
val anonymous = <expr>@dependency.Ann</expr> fun() {}
// FILE: dependency.kt
package dependency
annotation class Ann
@@ -0,0 +1,7 @@
Before shortening: @dependency.Ann
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
[type] dependency.Ann
with SHORTEN_AND_STAR_IMPORT:
[type] dependency.Ann
@@ -0,0 +1,9 @@
// FILE: main.kt
package test
val anonymous = fun <expr>dependency.Type</expr>.() {}
// FILE: dependency.kt
package dependency
class Type
@@ -0,0 +1,7 @@
Before shortening: dependency.Type
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
[type] dependency.Type
with SHORTEN_AND_STAR_IMPORT:
[type] dependency.Type
@@ -0,0 +1,9 @@
// FILE: main.kt
package test
val anonymous = fun(): <expr>dependency.Type</expr> = dependency.Type()
// FILE: dependency.kt
package dependency
class Type
@@ -0,0 +1,7 @@
Before shortening: dependency.Type
with DO_NOT_SHORTEN:
with SHORTEN_IF_ALREADY_IMPORTED:
with SHORTEN_AND_IMPORT:
[type] dependency.Type
with SHORTEN_AND_STAR_IMPORT:
[type] dependency.Type