Introduce components for library-to-source resolution in IDE
#KT-24309 In progress
This commit is contained in:
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||
import org.jetbrains.kotlin.resolve.AnchorProvider
|
||||
|
||||
interface ModuleDescriptor : DeclarationDescriptor {
|
||||
override fun getContainingDeclaration(): DeclarationDescriptor? = null
|
||||
@@ -61,4 +62,6 @@ interface ModuleDescriptor : DeclarationDescriptor {
|
||||
val isValid: Boolean
|
||||
|
||||
fun assertValid()
|
||||
|
||||
val anchorProvider: AnchorProvider
|
||||
}
|
||||
|
||||
@@ -19,21 +19,28 @@ package org.jetbrains.kotlin.descriptors
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
|
||||
fun ModuleDescriptor.findClassifierAcrossModuleDependencies(classId: ClassId): ClassifierDescriptor? {
|
||||
fun ModuleDescriptor.findClassifierAcrossModuleDependencies(classId: ClassId): ClassifierDescriptor? = withAnchorFallback {
|
||||
val packageViewDescriptor = getPackage(classId.packageFqName)
|
||||
val segments = classId.relativeClassName.pathSegments()
|
||||
val topLevelClass = packageViewDescriptor.memberScope.getContributedClassifier(
|
||||
segments.first(),
|
||||
NoLookupLocation.FROM_DESERIALIZATION
|
||||
) ?: return null
|
||||
) ?: return@withAnchorFallback null
|
||||
var result = topLevelClass
|
||||
for (name in segments.subList(1, segments.size)) {
|
||||
if (result !is ClassDescriptor) return null
|
||||
if (result !is ClassDescriptor) return@withAnchorFallback null
|
||||
result = result.unsubstitutedInnerClassesScope
|
||||
.getContributedClassifier(name, NoLookupLocation.FROM_DESERIALIZATION) as? ClassDescriptor
|
||||
?: return null
|
||||
?: return@withAnchorFallback null
|
||||
}
|
||||
return result
|
||||
return@withAnchorFallback result
|
||||
}
|
||||
|
||||
private inline fun ModuleDescriptor.withAnchorFallback(
|
||||
crossinline doSearch: ModuleDescriptor.() -> ClassifierDescriptor?
|
||||
): ClassifierDescriptor? {
|
||||
val anchor = anchorProvider.getAnchor(this)
|
||||
return if (anchor == null) doSearch() else doSearch() ?: anchor.doSearch()
|
||||
}
|
||||
|
||||
fun ModuleDescriptor.findClassAcrossModuleDependencies(classId: ClassId): ClassDescriptor? =
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.platform.TargetPlatform
|
||||
import org.jetbrains.kotlin.resolve.AnchorProvider
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.types.checker.REFINER_CAPABILITY
|
||||
import org.jetbrains.kotlin.types.checker.Ref
|
||||
@@ -38,7 +39,8 @@ class ModuleDescriptorImpl @JvmOverloads constructor(
|
||||
// May be null in compiler context, should be not-null in IDE context
|
||||
override val platform: TargetPlatform? = null,
|
||||
capabilities: Map<ModuleDescriptor.Capability<*>, Any?> = emptyMap(),
|
||||
override val stableName: Name? = null
|
||||
override val stableName: Name? = null,
|
||||
override val anchorProvider: AnchorProvider = AnchorProvider.Default,
|
||||
) : DeclarationDescriptorImpl(Annotations.EMPTY, moduleName), ModuleDescriptor {
|
||||
private val capabilities: Map<ModuleDescriptor.Capability<*>, Any?>
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* 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.resolve
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
|
||||
abstract class AnchorProvider {
|
||||
open fun getAnchor(moduleDescriptor: ModuleDescriptor): ModuleDescriptor? = null
|
||||
|
||||
companion object Default : AnchorProvider()
|
||||
}
|
||||
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.incremental.components.LookupLocation;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.platform.TargetPlatform;
|
||||
import org.jetbrains.kotlin.resolve.AnchorProvider;
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
|
||||
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter;
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope;
|
||||
@@ -53,6 +54,12 @@ public class ErrorUtils {
|
||||
|
||||
static {
|
||||
ERROR_MODULE = new ModuleDescriptor() {
|
||||
@NotNull
|
||||
@Override
|
||||
public AnchorProvider getAnchorProvider() {
|
||||
return AnchorProvider.Default;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <T> T getCapability(@NotNull Capability<T> capability) {
|
||||
|
||||
Reference in New Issue
Block a user