KT-61889 [AA] Migrate KtFirReferenceShortener to ContextCollector

This should make reference shortener considerably faster, since it won't
need to perform redundant extra resolve of the file.

`ContextCollector` more accurately collects the scopes for the scripts,
so some script tests are also fixed.

It should fix the following bugs:

^KTIJ-26714 Fixed
^KTIJ-26727 Fixed

This is also an important part of fixing the following bugs:
- KTIJ-26715
- KTIJ-26734

But those bugs also rely on KT-61890, because completion uses scopes
and snows incorrect elements from them
This commit is contained in:
Roman Golyshev
2023-09-11 12:57:08 +02:00
committed by Space Team
parent ce900063c0
commit 7b50506aea
10 changed files with 167 additions and 10 deletions
@@ -39,7 +39,7 @@ import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.utils.yieldIfNotNull
internal object ContextCollector {
object ContextCollector {
enum class ContextKind {
/** Represents the context of the declaration itself. */
SELF,
@@ -79,7 +79,7 @@ internal object ContextCollector {
val isBodyContextCollected = bodyElement != null
val acceptedElements = targetElement.parentsWithSelf.toSet()
val contextProvider = process(file, computeDesignation(file, targetElement), holder, isBodyContextCollected) { candidate ->
val contextProvider = process(file, holder, computeDesignation(file, targetElement), isBodyContextCollected) { candidate ->
when (candidate) {
targetElement -> FilterResponse.STOP
in acceptedElements -> FilterResponse.CONTINUE
@@ -104,7 +104,7 @@ internal object ContextCollector {
return null
}
private fun computeDesignation(file: FirFile, targetElement: PsiElement): FirDesignation? {
fun computeDesignation(file: FirFile, targetElement: PsiElement): FirDesignation? {
val contextKtDeclaration = targetElement.getNonLocalContainingOrThisDeclaration()
if (contextKtDeclaration != null) {
val designationPath = FirElementFinder.collectDesignationPath(file, contextKtDeclaration)
@@ -126,15 +126,15 @@ internal object ContextCollector {
* Processes the [FirFile], collecting contexts for elements matching the [filter].
*
* @param file The file to process.
* @param designation The declaration to process. If `null`, all declarations in the [file] are processed.
* @param holder The [SessionHolder] for the session that owns a [file].
* @param designation The declaration to process. If `null`, all declarations in the [file] are processed.
* @param shouldCollectBodyContext If `true`, [ContextKind.BODY] is collected where available.
* @param filter The filter predicate. Context is collected only for [PsiElement]s for which the [filter] returns `true`.
*/
fun process(
file: FirFile,
designation: FirDesignation?,
holder: SessionHolder,
designation: FirDesignation?,
shouldCollectBodyContext: Boolean,
filter: (PsiElement) -> FilterResponse
): ContextProvider {