From ee0874a26d48a22df1b7b09429ac346ebbdf0055 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 31 Jan 2017 15:27:39 +0300 Subject: [PATCH] Drop JsVirtualFileFinder and its factory, refactor nearby The only remaining usage was in KotlinRuntimeLibraryUtil.kt where we only needed to check whether there's at least one file in a given package indexed by KotlinJavaScriptMetaFileIndex. Move that logic to a public extension, drop everything else --- .../idea/vfilefinder/JsVirtualFileFinder.kt | 32 ----------- .../vfilefinder/JsVirtualFileFinderFactory.kt | 31 ----------- .../kotlin/idea/vfilefinder/factories.kt | 4 -- .../kotlin/idea/vfilefinder/fileFinders.kt | 54 ++++++------------- .../kotlin/idea/vfilefinder/fileIndexes.kt | 4 ++ idea/src/META-INF/plugin.xml | 3 -- .../idea/versions/KotlinRuntimeLibraryUtil.kt | 10 ++-- 7 files changed, 26 insertions(+), 112 deletions(-) delete mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/JsVirtualFileFinder.kt delete mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/JsVirtualFileFinderFactory.kt diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/JsVirtualFileFinder.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/JsVirtualFileFinder.kt deleted file mode 100644 index ea66bd19acb..00000000000 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/JsVirtualFileFinder.kt +++ /dev/null @@ -1,32 +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.vfilefinder - -import com.intellij.openapi.components.ServiceManager -import com.intellij.openapi.project.Project -import com.intellij.psi.search.GlobalSearchScope -import org.jetbrains.kotlin.load.kotlin.VirtualFileFinder -import org.jetbrains.kotlin.name.FqName - -interface JsVirtualFileFinder : VirtualFileFinder { - fun hasPackage(fqName: FqName): Boolean - - object SERVICE { - @JvmStatic fun getInstance(project: Project): JsVirtualFileFinder = - ServiceManager.getService(project, JsVirtualFileFinderFactory::class.java).create(GlobalSearchScope.allScope(project)) - } -} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/JsVirtualFileFinderFactory.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/JsVirtualFileFinderFactory.kt deleted file mode 100644 index a11a5c969a6..00000000000 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/JsVirtualFileFinderFactory.kt +++ /dev/null @@ -1,31 +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.vfilefinder - -import com.intellij.openapi.components.ServiceManager -import com.intellij.openapi.project.Project -import com.intellij.psi.search.GlobalSearchScope -import org.jetbrains.kotlin.load.kotlin.VirtualFileFinderFactory - -interface JsVirtualFileFinderFactory : VirtualFileFinderFactory { - override fun create(scope: GlobalSearchScope): JsVirtualFileFinder - - object SERVICE { - @JvmStatic fun getInstance(project: Project): JsVirtualFileFinderFactory = - ServiceManager.getService(project, JsVirtualFileFinderFactory::class.java) - } -} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/factories.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/factories.kt index 3ad162c4a3f..979c1dc2ba8 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/factories.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/factories.kt @@ -23,7 +23,3 @@ import org.jetbrains.kotlin.load.kotlin.JvmVirtualFileFinderFactory class JvmIDEVirtualFileFinderFactory : JvmVirtualFileFinderFactory { override fun create(scope: GlobalSearchScope): JvmVirtualFileFinder = JvmIDEVirtualFileFinder(scope) } - -class JsIDEVirtualFileFinderFactory : JsVirtualFileFinderFactory { - override fun create(scope: GlobalSearchScope): JsVirtualFileFinder = JsIDEVirtualFileFinder(scope) -} diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileFinders.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileFinders.kt index 6475393d4ea..008e539c431 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileFinders.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileFinders.kt @@ -27,56 +27,34 @@ import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import java.io.InputStream -private fun checkScopeForFinder(scope: GlobalSearchScope, logger: Logger) { - if (scope != GlobalSearchScope.EMPTY_SCOPE && scope.project == null) { - logger.warn("Scope with null project " + scope) - } -} - -private fun findVirtualFileWithHeader(classId: ClassId, key: ID, scope: GlobalSearchScope, logger: Logger): VirtualFile? { - val files = FileBasedIndex.getInstance().getContainingFiles(key, classId.asSingleFqName(), scope) - if (files.isEmpty()) return null - - if (files.size > 1) { - logger.warn("There are " + files.size + " classes with same fqName: " + classId + " found.") - } - return files.iterator().next() -} - -// TODO: drop this implementation, replace with a much simpler service -class JsIDEVirtualFileFinder(private val scope: GlobalSearchScope) : JsVirtualFileFinder { - - init { checkScopeForFinder(scope, LOG) } - - override fun findVirtualFileWithHeader(classId: ClassId): VirtualFile? = - throw UnsupportedOperationException("This method should not be called. (classId = $classId)") - - override fun hasPackage(fqName: FqName): Boolean = KotlinJavaScriptMetaFileIndex.hasPackage(fqName, scope) - - companion object { - private val LOG = Logger.getInstance(JsIDEVirtualFileFinder::class.java) - } -} - class JvmIDEVirtualFileFinder(private val scope: GlobalSearchScope) : VirtualFileKotlinClassFinder(), JvmVirtualFileFinder { override fun findMetadata(classId: ClassId): InputStream? { - return findVirtualFileWithHeader(classId, KotlinMetadataFileIndex.KEY, scope, LOG)?.inputStream + return findVirtualFileWithHeader(classId, KotlinMetadataFileIndex.KEY)?.inputStream } - override fun hasMetadataPackage(fqName: FqName): Boolean = KotlinMetadataFilePackageIndex.hasPackage(fqName, scope) + override fun hasMetadataPackage(fqName: FqName): Boolean = KotlinMetadataFilePackageIndex.hasSomethingInPackage(fqName, scope) // TODO: load built-ins metadata from scope override fun findBuiltInsData(packageFqName: FqName): InputStream? = null - init { checkScopeForFinder(scope, LOG) } + init { + if (scope != GlobalSearchScope.EMPTY_SCOPE && scope.project == null) { + LOG.warn("Scope with null project $scope") + } + } override fun findVirtualFileWithHeader(classId: ClassId): VirtualFile? = - findVirtualFileWithHeader(classId, KotlinClassFileIndex.KEY, scope, LOG) + findVirtualFileWithHeader(classId, KotlinClassFileIndex.KEY) + + private fun findVirtualFileWithHeader(classId: ClassId, key: ID): VirtualFile? { + val files = FileBasedIndex.getInstance().getContainingFiles(key, classId.asSingleFqName(), scope) + if (files.size > 1) { + LOG.warn("There are ${files.size} classes with same fqName: $classId found.") + } + return files.firstOrNull() + } companion object { private val LOG = Logger.getInstance(JvmIDEVirtualFileFinder::class.java) } } - -private fun KotlinFileIndexBase.hasPackage(fqName: FqName, scope: GlobalSearchScope): Boolean = - !FileBasedIndex.getInstance().processValues(name, fqName, null, { _, _ -> false }, scope) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileIndexes.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileIndexes.kt index 52142db1b76..164a1db781b 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileIndexes.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/vfilefinder/fileIndexes.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.vfilefinder import com.intellij.ide.highlighter.JavaClassFileType import com.intellij.openapi.diagnostic.Logger +import com.intellij.psi.search.GlobalSearchScope import com.intellij.util.indexing.* import com.intellij.util.io.IOUtil import com.intellij.util.io.KeyDescriptor @@ -76,6 +77,9 @@ abstract class KotlinFileIndexBase(classOfIndex: Class) : ScalarIndexExten } } +fun KotlinFileIndexBase.hasSomethingInPackage(fqName: FqName, scope: GlobalSearchScope): Boolean = + !FileBasedIndex.getInstance().processValues(name, fqName, null, { _, _ -> false }, scope) + object KotlinClassFileIndex : KotlinFileIndexBase(KotlinClassFileIndex::class.java) { override fun getIndexer() = INDEXER diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 83dd45dfe2c..17d81142d46 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -238,9 +238,6 @@ - - diff --git a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinRuntimeLibraryUtil.kt b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinRuntimeLibraryUtil.kt index b58e9dd8366..6ba4307c835 100644 --- a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinRuntimeLibraryUtil.kt +++ b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinRuntimeLibraryUtil.kt @@ -51,13 +51,13 @@ import org.jetbrains.kotlin.idea.framework.* import org.jetbrains.kotlin.idea.util.application.runReadAction import org.jetbrains.kotlin.idea.util.projectStructure.allModules import org.jetbrains.kotlin.idea.util.runWithAlternativeResolveEnabled -import org.jetbrains.kotlin.idea.vfilefinder.JsVirtualFileFinderFactory +import org.jetbrains.kotlin.idea.vfilefinder.KotlinJavaScriptMetaFileIndex +import org.jetbrains.kotlin.idea.vfilefinder.hasSomethingInPackage import org.jetbrains.kotlin.load.kotlin.JvmMetadataVersion import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.serialization.deserialization.BinaryVersion import org.jetbrains.kotlin.utils.JsMetadataVersion import org.jetbrains.kotlin.utils.PathUtil -import org.jetbrains.kotlin.utils.addToStdlib.constant import java.io.File import java.io.IOException @@ -167,7 +167,7 @@ fun findAllUsedLibraries(project: Project): MultiMap { return libraries } -private enum class LibraryJarDescriptor private constructor(val jarName: String, val shouldExist: Boolean) { +private enum class LibraryJarDescriptor(val jarName: String, val shouldExist: Boolean) { RUNTIME_JAR(PathUtil.KOTLIN_JAVA_RUNTIME_JAR, true), REFLECT_JAR(PathUtil.KOTLIN_JAVA_REFLECT_JAR, false), SCRIPT_RUNTIME_JAR(PathUtil.KOTLIN_JAVA_SCRIPT_RUNTIME_JAR, true), @@ -288,10 +288,12 @@ fun getKotlinJvmRuntimeMarkerClass(project: Project, scope: GlobalSearchScope): } } +private val KOTLIN_JS_FQ_NAME = FqName("kotlin.js") + fun hasKotlinJsKjsmFile(project: Project, scope: GlobalSearchScope): Boolean { return runReadAction { project.runWithAlternativeResolveEnabled { - JsVirtualFileFinderFactory.SERVICE.getInstance(project).create(scope).hasPackage(constant { FqName("kotlin.js") }) + KotlinJavaScriptMetaFileIndex.hasSomethingInPackage(KOTLIN_JS_FQ_NAME, scope) } } }