From babf079c3198eab770f12962a6528c23b49d3ccb Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Fri, 9 Apr 2021 14:34:30 +0200 Subject: [PATCH] FIR IDE: fix resolving of inner type aliases --- .../idea/stubindex/IdeStubIndexService.java | 12 +++++-- .../KotlinInnerTypeAliasClassIdIndex.kt | 34 +++++++++++++++++++ .../idea/fir/low/level/api/IndexHelper.kt | 11 ++++-- idea/resources/META-INF/indexes.xml | 1 + 4 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/KotlinInnerTypeAliasClassIdIndex.kt diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/IdeStubIndexService.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/IdeStubIndexService.java index 4ddf811d240..3bfe2e5834b 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/IdeStubIndexService.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/IdeStubIndexService.java @@ -27,6 +27,7 @@ import org.jetbrains.kotlin.fileClasses.JvmFileClassInfo; import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil; import org.jetbrains.kotlin.lexer.KtTokens; import org.jetbrains.kotlin.load.java.JvmAbi; +import org.jetbrains.kotlin.name.ClassId; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.psi.KtClassOrObject; @@ -174,13 +175,18 @@ public class IdeStubIndexService extends StubIndexService { IndexUtilsKt.indexTypeAliasExpansion(stub, sink); - if (stub.isTopLevel()) { - FqName fqName = stub.getFqName(); - if (fqName != null) { + FqName fqName = stub.getFqName(); + if (fqName != null) { + if (stub.isTopLevel()) { sink.occurrence(KotlinTopLevelTypeAliasFqNameIndex.getInstance().getKey(), fqName.asString()); sink.occurrence(KotlinTopLevelTypeAliasByPackageIndex.getInstance().getKey(), fqName.parent().asString()); } } + + ClassId classId = stub.getClassId(); + if (classId != null && !stub.isTopLevel()) { + sink.occurrence(KotlinInnerTypeAliasClassIdIndex.getInstance().getKey(), classId.asString()); + } } @Override diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/KotlinInnerTypeAliasClassIdIndex.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/KotlinInnerTypeAliasClassIdIndex.kt new file mode 100644 index 00000000000..45ac57049d8 --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/stubindex/KotlinInnerTypeAliasClassIdIndex.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2010-2019 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.stubindex + +import com.intellij.openapi.project.Project +import com.intellij.psi.search.GlobalSearchScope +import com.intellij.psi.stubs.StringStubIndexExtension +import com.intellij.psi.stubs.StubIndex +import com.intellij.psi.stubs.StubIndexKey +import org.jetbrains.kotlin.psi.KtTypeAlias + +/** + * Index of non-top-level type aliases (members of classes) + * The key is stringified [org.jetbrains.kotlin.name.ClassId] by the rules described in [org.jetbrains.kotlin.name.ClassId.asString]: + * packages are delimited by '/' and classes by '.', e.g. "kotlin/Map.Entry" + */ +class KotlinInnerTypeAliasClassIdIndex : StringStubIndexExtension() { + override fun getKey(): StubIndexKey = KEY + + override fun get(s: String, project: Project, scope: GlobalSearchScope): Collection = + StubIndex.getElements(KEY, s, project, scope, KtTypeAlias::class.java) + + companion object { + val KEY = KotlinIndexUtil.createIndexKey(KotlinInnerTypeAliasClassIdIndex::class.java) + val INSTANCE = KotlinInnerTypeAliasClassIdIndex() + + @JvmStatic + fun getInstance() = INSTANCE + } +} + diff --git a/idea/idea-frontend-fir/idea-fir-low-level-api/src/org/jetbrains/kotlin/idea/fir/low/level/api/IndexHelper.kt b/idea/idea-frontend-fir/idea-fir-low-level-api/src/org/jetbrains/kotlin/idea/fir/low/level/api/IndexHelper.kt index 022453d3bf5..82e21c3b46d 100644 --- a/idea/idea-frontend-fir/idea-fir-low-level-api/src/org/jetbrains/kotlin/idea/fir/low/level/api/IndexHelper.kt +++ b/idea/idea-frontend-fir/idea-fir-low-level-api/src/org/jetbrains/kotlin/idea/fir/low/level/api/IndexHelper.kt @@ -18,6 +18,7 @@ import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult /* * Move to another module @@ -28,7 +29,7 @@ public class IndexHelper(val project: Project, private val scope: GlobalSearchSc private inline fun firstMatchingOrNull( stubKey: StubIndexKey, key: INDEX_KEY, - crossinline filter: (PSI) -> Boolean + crossinline filter: (PSI) -> Boolean = { true } ): PSI? { var result: PSI? = null stubIndex.processElements( @@ -48,10 +49,14 @@ public class IndexHelper(val project: Project, private val scope: GlobalSearchSc classId.asStringForIndexes(), ) { candidate -> candidate.containingKtFile.packageFqName == classId.packageFqName } - fun typeAliasFromIndexByClassId(classId: ClassId) = firstMatchingOrNull( + fun typeAliasFromIndexByClassId(classId: ClassId): KtTypeAlias? = firstMatchingOrNull( KotlinTopLevelTypeAliasFqNameIndex.KEY, - classId.asStringForIndexes(), + key = classId.asStringForIndexes(), ) { candidate -> candidate.containingKtFile.packageFqName == classId.packageFqName } + ?: firstMatchingOrNull( + KotlinInnerTypeAliasClassIdIndex.KEY, + key = classId.asString(), + ) fun getTopLevelProperties(callableId: CallableId): Collection = diff --git a/idea/resources/META-INF/indexes.xml b/idea/resources/META-INF/indexes.xml index 9241a167702..9b7a895f941 100644 --- a/idea/resources/META-INF/indexes.xml +++ b/idea/resources/META-INF/indexes.xml @@ -15,6 +15,7 @@ +