-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIR IDE: introduce KtFirCollectionLiteralReference
- Loading branch information
1 parent
bac5ebc
commit 7b1eef1
Showing
5 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...-frontend-fir/src/org/jetbrains/kotlin/idea/references/KtFirCollectionLiteralReference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.idea.references | ||
|
||
import org.jetbrains.kotlin.fir.declarations.FirFunction | ||
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction | ||
import org.jetbrains.kotlin.fir.expressions.FirArrayOfCall | ||
import org.jetbrains.kotlin.fir.resolve.firSymbolProvider | ||
import org.jetbrains.kotlin.fir.symbols.CallableId | ||
import org.jetbrains.kotlin.fir.symbols.StandardClassIds | ||
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol | ||
import org.jetbrains.kotlin.fir.types.ConeClassLikeType | ||
import org.jetbrains.kotlin.fir.types.coneType | ||
import org.jetbrains.kotlin.fir.types.coneTypeSafe | ||
import org.jetbrains.kotlin.idea.fir.low.level.api.api.getOrBuildFirSafe | ||
import org.jetbrains.kotlin.idea.frontend.api.KtAnalysisSession | ||
import org.jetbrains.kotlin.idea.frontend.api.fir.KtFirAnalysisSession | ||
import org.jetbrains.kotlin.idea.frontend.api.symbols.KtSymbol | ||
import org.jetbrains.kotlin.name.ClassId | ||
import org.jetbrains.kotlin.name.FqName | ||
import org.jetbrains.kotlin.name.Name | ||
import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression | ||
|
||
class KtFirCollectionLiteralReference( | ||
expression: KtCollectionLiteralExpression | ||
) : KtCollectionLiteralReference(expression), KtFirReference { | ||
override fun KtAnalysisSession.resolveToSymbols(): Collection<KtSymbol> { | ||
check(this is KtFirAnalysisSession) | ||
val fir = element.getOrBuildFirSafe<FirArrayOfCall>(firResolveState) ?: return emptyList() | ||
val type = fir.typeRef.coneTypeSafe<ConeClassLikeType>() ?: return listOfNotNull(arrayOfSymbol(arrayOf)) | ||
val call = arrayTypeToArrayOfCall[type.lookupTag.classId] ?: arrayOf | ||
return listOfNotNull(arrayOfSymbol(call)) | ||
} | ||
|
||
private fun KtFirAnalysisSession.arrayOfSymbol(identifier: Name): KtSymbol? { | ||
val fir = firResolveState.rootModuleSession.firSymbolProvider.getTopLevelCallableSymbols(kotlinPackage, identifier).firstOrNull { | ||
/* choose (for byte array) | ||
* public fun byteArrayOf(vararg elements: kotlin.Byte): kotlin.ByteArray | ||
*/ | ||
(it as? FirFunctionSymbol<*>)?.fir?.valueParameters?.singleOrNull()?.isVararg == true | ||
}?.fir as? FirSimpleFunction ?: return null | ||
return firSymbolBuilder.buildFunctionSymbol(fir) | ||
} | ||
|
||
companion object { | ||
private val kotlinPackage = FqName("kotlin") | ||
private val arrayOf = Name.identifier("arrayOf") | ||
private val arrayTypeToArrayOfCall = run { | ||
StandardClassIds.primitiveArrayTypeByElementType.values + StandardClassIds.unsignedArrayTypeByElementType.values | ||
}.associateWith { it.correspondingArrayOfCallFqName() } | ||
|
||
private fun ClassId.correspondingArrayOfCallFqName(): Name = | ||
Name.identifier("${shortClassName.identifier.decapitalize()}Of") | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// IGNORE_FIR | ||
|
||
val abc = <caret>[1, 2, 3] | ||
|
||
// REF: (kotlin).arrayOf(vararg T) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// IGNORE_FIR | ||
|
||
val abc: IntArray = [1, 2, 3<caret>] | ||
|
||
// REF: (kotlin).intArrayOf(vararg kotlin.Int) |