diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/codeInsight/DescriptorToSourceUtilsIde.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/codeInsight/DescriptorToSourceUtilsIde.kt index 96c49efbc79..e11a608a9f4 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/codeInsight/DescriptorToSourceUtilsIde.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/codeInsight/DescriptorToSourceUtilsIde.kt @@ -20,7 +20,6 @@ import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.idea.decompiler.navigation.findDecompiledDeclaration -import org.jetbrains.kotlin.idea.references.BuiltInsReferenceResolver import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.utils.addToStdlib.sequenceOfLazyValues @@ -46,7 +45,7 @@ public object DescriptorToSourceUtilsIde { // therefore we put both source declaration and decompiled declaration to stream, and afterwards we filter it in getAllDeclarations sequenceOfLazyValues( { DescriptorToSourceUtils.getSourceFromDescriptor(effectiveReferenced) }, - { BuiltInsReferenceResolver.resolveBuiltInSymbol(project, effectiveReferenced) ?: findDecompiledDeclaration(project, effectiveReferenced) } + { findDecompiledDeclaration(project, effectiveReferenced) } ) }.filterNotNull() } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/references/BuiltInsReferenceResolver.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/references/BuiltInsReferenceResolver.kt deleted file mode 100644 index 97473009858..00000000000 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/references/BuiltInsReferenceResolver.kt +++ /dev/null @@ -1,247 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.idea.references - -import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.components.ServiceManager -import com.intellij.openapi.progress.NonCancelableSection -import com.intellij.openapi.progress.impl.CoreProgressManager -import com.intellij.openapi.project.Project -import com.intellij.openapi.startup.StartupManager -import com.intellij.openapi.vfs.VfsUtilCore -import com.intellij.openapi.vfs.VirtualFileManager -import com.intellij.psi.PsiElement -import com.intellij.psi.PsiManager -import com.intellij.util.PathUtil -import org.jetbrains.kotlin.asJava.LightClassUtil -import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.context.MutableModuleContextImpl -import org.jetbrains.kotlin.context.ProjectContextImpl -import org.jetbrains.kotlin.context.SimpleGlobalContext -import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.frontend.di.createLazyResolveSession -import org.jetbrains.kotlin.idea.util.application.runReadAction -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.psi.KtFile -import org.jetbrains.kotlin.renderer.DescriptorRenderer -import org.jetbrains.kotlin.resolve.BindingTraceContext -import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils -import org.jetbrains.kotlin.resolve.TargetPlatform -import org.jetbrains.kotlin.resolve.createModule -import org.jetbrains.kotlin.resolve.descriptorUtil.classId -import org.jetbrains.kotlin.resolve.descriptorUtil.module -import org.jetbrains.kotlin.resolve.lazy.declarations.FileBasedDeclarationProviderFactory -import org.jetbrains.kotlin.resolve.scopes.MemberScope -import org.jetbrains.kotlin.serialization.deserialization.findClassAcrossModuleDependencies -import org.jetbrains.kotlin.storage.ExceptionTracker -import org.jetbrains.kotlin.storage.LockBasedStorageManager -import org.jetbrains.kotlin.storage.ObservableStorageManager -import org.jetbrains.kotlin.storage.StorageManager -import java.io.File -import java.net.URL - -public class BuiltInsReferenceResolver(val project: Project, val startupManager: StartupManager) { - - @Volatile private var moduleDescriptor: ModuleDescriptor? = null - - @Volatile public var builtInsSources: Set? = null - private set - - @Volatile private var builtinsPackageFragment: PackageFragmentDescriptor? = null - - init { - startupManager.runWhenProjectIsInitialized { initialize() } - } - - private fun initialize() { - assert(moduleDescriptor == null) { "Attempt to initialize twice" } - - val jetBuiltInsFiles = getBuiltInsKtFiles() - - runReadAction { - val tracker = object : ExceptionTracker() { - override fun handleException(throwable: Throwable): RuntimeException { - throw RuntimeException("No exceptions expected in ${BuiltInsReferenceResolver::class.simpleName}", throwable) - } - } - - val storageManager = NonCancelableStorageManager(LockBasedStorageManager.createWithExceptionHandling(tracker)) - val globalContext = SimpleGlobalContext(storageManager, tracker) - - val projectContext = ProjectContextImpl(project, globalContext) - val module = TargetPlatform.Default.createModule(Name.special(""), projectContext.storageManager) - val newModuleContext = MutableModuleContextImpl(module, projectContext) - - newModuleContext.setDependencies(newModuleContext.module) - - val declarationFactory = FileBasedDeclarationProviderFactory(newModuleContext.storageManager, jetBuiltInsFiles) - - val resolveSession = createLazyResolveSession( - newModuleContext, - declarationFactory, BindingTraceContext(), - TargetPlatform.Default) - - newModuleContext.initializeModuleContents(resolveSession.getPackageFragmentProvider()) - - val packageView = newModuleContext.module.getPackage(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME) - val fragments = packageView.fragments - - this@BuiltInsReferenceResolver.moduleDescriptor = newModuleContext.module - builtinsPackageFragment = fragments.single() - builtInsSources = jetBuiltInsFiles - } - } - - private fun getBuiltInsKtFiles(): Set { - return getBuiltInsDirUrls().flatMapTo(hashSetOf()) { getBuiltInSourceFiles(it) } - } - - private fun getBuiltInSourceFiles(url: URL): Set { - val fromUrl = VfsUtilCore.convertFromUrl(url) - val vf = VirtualFileManager.getInstance().findFileByUrl(fromUrl) - assert(vf != null) { "Virtual file not found by URL: $url" } - - val psiDirectory = PsiManager.getInstance(project).findDirectory(vf!!) - assert(psiDirectory != null) { "No PsiDirectory for $vf" } - return psiDirectory!!.getFiles().filterIsInstance().toHashSet() - } - - private fun findCurrentDescriptorForMember(originalDescriptor: MemberDescriptor): DeclarationDescriptor? { - val containingDeclaration = findCurrentDescriptor(originalDescriptor.getContainingDeclaration()) - val memberScope = getMemberScope(containingDeclaration) ?: return null - - val renderedOriginal = DescriptorRenderer.FQ_NAMES_IN_TYPES.render(originalDescriptor) - val descriptors = if (originalDescriptor is ConstructorDescriptor && containingDeclaration is ClassDescriptor) { - containingDeclaration.getConstructors() - } - else { - memberScope.getContributedDescriptors() - } - - return descriptors.firstOrNull { renderedOriginal == DescriptorRenderer.FQ_NAMES_IN_TYPES.render(it) } - } - - private fun findCurrentDescriptor(originalDescriptor: DeclarationDescriptor): DeclarationDescriptor? = when(originalDescriptor) { - is ClassDescriptor -> moduleDescriptor!!.findClassAcrossModuleDependencies(originalDescriptor.classId) - - is PackageFragmentDescriptor -> { - if (KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAME == originalDescriptor.fqName) - builtinsPackageFragment - else - null - } - - is MemberDescriptor -> findCurrentDescriptorForMember(originalDescriptor) - - else -> null - } - - companion object { - private val BUILT_INS_COMPILABLE_SRC_DIR = File("core/builtins/src", KotlinBuiltIns.BUILT_INS_PACKAGE_NAME.asString()) - - private val builtInDirUrls = getBuiltInsDirUrls().map { VfsUtilCore.convertFromUrl(it) } - - public fun getInstance(project: Project): BuiltInsReferenceResolver = - ServiceManager.getService(project, BuiltInsReferenceResolver::class.java) - - private fun isFromBuiltinModule(originalDescriptor: DeclarationDescriptor): Boolean { - // TODO This is optimization only - // It should be rewritten by checking declarationDescriptor.getSource(), when the latter returns something non-trivial for builtins. - val containingModule = originalDescriptor.module - return containingModule.builtIns.builtInsModule == containingModule - } - - public fun resolveBuiltInSymbol(project: Project, declarationDescriptor: DeclarationDescriptor): PsiElement? { - if (!isFromBuiltinModule(declarationDescriptor)) { - return null - } - - val resolver = getInstance(project) - if (resolver.moduleDescriptor == null) { - return null - } - - val descriptor = resolver.findCurrentDescriptor(declarationDescriptor) - if (descriptor != null) { - return DescriptorToSourceUtils.getSourceFromDescriptor(descriptor) - } - return null - } - - public fun isFromBuiltIns(element: PsiElement): Boolean { - val url = element.getContainingFile()?.getVirtualFile()?.getUrl() - return url != null && VfsUtilCore.isUnder(url, builtInDirUrls) - } - - private fun getMemberScope(parent: DeclarationDescriptor?): MemberScope? = when(parent) { - is ClassDescriptor -> parent.getDefaultType().getMemberScope() - is PackageFragmentDescriptor -> parent.getMemberScope() - else -> null - } - - public fun getBuiltInsDirUrls(): Set { - val defaultBuiltIns = LightClassUtil.builtInsDirUrl - // In production, the above URL is enough as it contains sources for both native and compilable built-ins - // (it's simply the "kotlin" directory in kotlin-plugin.jar) - // But in tests, sources of built-ins are not added to the classpath automatically, so we manually specify URLs for both: - // LightClassUtil.getBuiltInsDirUrl() does so for native built-ins and the code below for compilable built-ins - - if (ApplicationManager.getApplication().isUnitTestMode) { - return setOf(defaultBuiltIns, BUILT_INS_COMPILABLE_SRC_DIR.toURI().toURL()) - } - return setOf(defaultBuiltIns) - } - - public fun refreshBuiltIns() { - getBuiltInsDirUrls().forEach { url -> - val fromUrl = VfsUtilCore.convertFromUrl(url) - val vf = VirtualFileManager.getInstance().findFileByUrl(fromUrl) - assert(vf != null) { "Virtual file not found by URL: $url" } - - // Refreshing VFS: in case the plugin jar was updated, the caches may hold the old value - vf!!.getChildren() - vf.refresh(true, true) - PathUtil.getLocalFile(vf).refresh(true, true) - } - } - } -} - -class NonCancelableStorageManager(val delegate: StorageManager) : ObservableStorageManager(delegate) { - override val (() -> V).observable: () -> V get() = { - runNonCancelable { this@observable() } - } - - override val ((K) -> V).observable: (K) -> V get() = { p -> - runNonCancelable { this@observable(p) } - } - - private inline fun runNonCancelable(crossinline call: () -> T) : T { - if (CoreProgressManager.getInstance().progressIndicator is NonCancelableSection) { - return call() - } - - var result: Any? = null - CoreProgressManager.getInstance().executeNonCancelableSection { - result = call() - } - - @Suppress("UNCHECKED_CAST") - return result as T - } -} - diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/quickfix/QuickFixUtil.java b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/quickfix/QuickFixUtil.java index 7de65d0533d..0a415272fdf 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/quickfix/QuickFixUtil.java +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/quickfix/QuickFixUtil.java @@ -27,7 +27,6 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.diagnostics.Diagnostic; import org.jetbrains.kotlin.idea.caches.resolve.ResolutionUtils; -import org.jetbrains.kotlin.idea.references.BuiltInsReferenceResolver; import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.psi.*; @@ -89,7 +88,7 @@ public class QuickFixUtil { } public static boolean canModifyElement(@NotNull PsiElement element) { - return element.isWritable() && !BuiltInsReferenceResolver.Companion.isFromBuiltIns(element); + return element.isWritable(); } @Nullable diff --git a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/testUtils.kt b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/testUtils.kt index cce5cd0f074..3f5e0704cac 100644 --- a/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/testUtils.kt +++ b/idea/idea-test-framework/src/org/jetbrains/kotlin/idea/test/testUtils.kt @@ -34,7 +34,6 @@ import org.jetbrains.kotlin.idea.caches.resolve.LibraryModificationTracker import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult import org.jetbrains.kotlin.idea.decompiler.KotlinDecompiledFileViewProvider import org.jetbrains.kotlin.idea.decompiler.KtDecompiledFile -import org.jetbrains.kotlin.idea.references.BuiltInsReferenceResolver import org.jetbrains.kotlin.psi.KtFile import java.util.* @@ -85,8 +84,6 @@ public fun closeAndDeleteProject(): Unit = ApplicationManager.getApplication().runWriteAction() { LightPlatformTestCase.closeAndDeleteProject() } public fun unInvalidateBuiltinsAndStdLib(project: Project, runnable: RunnableWithException) { - val builtInsSources = BuiltInsReferenceResolver.getInstance(project).builtInsSources!! - val stdLibViewProviders = HashSet() val vFileToViewProviderMap = ((PsiManager.getInstance(project) as PsiManagerEx).fileManager as FileManagerImpl).vFileToViewProviderMap for ((file, viewProvider) in vFileToViewProviderMap) { @@ -104,7 +101,6 @@ public fun unInvalidateBuiltinsAndStdLib(project: Project, runnable: RunnableWit field.set(file, false) } - builtInsSources.forEach { unInvalidateFile(it) } stdLibViewProviders.forEach { it.allFiles.forEach { unInvalidateFile(it as KtDecompiledFile) } vFileToViewProviderMap.set(it.virtualFile, it) diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 8df3e7acd13..47aed21305a 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -270,9 +270,6 @@ - - @@ -488,7 +485,6 @@ -