VirtualFileFinderFactory: introduce JvmVirtualFileFinderFactory, JsVirtualFileFinderFactory

This commit is contained in:
Michael Nedzelsky
2015-05-20 20:47:43 +03:00
parent de35434800
commit 618a8622a1
18 changed files with 255 additions and 88 deletions
@@ -0,0 +1,31 @@
/*
* 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 kotlin.platform.platformStatic
public interface JsVirtualFileFinder : VirtualFileFinder {
public object SERVICE {
platformStatic
public fun getInstance(project: Project): JsVirtualFileFinder =
ServiceManager.getService(project, javaClass<JsVirtualFileFinderFactory>()).create(GlobalSearchScope.allScope(project))
}
}
@@ -14,18 +14,20 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.vfilefinder;
package org.jetbrains.kotlin.idea.vfilefinder
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.load.kotlin.VirtualFileFinder;
import org.jetbrains.kotlin.load.kotlin.VirtualFileFinderFactory;
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
import kotlin.platform.platformStatic
public class IDEVirtualFileFinderFactory implements VirtualFileFinderFactory {
public interface JsVirtualFileFinderFactory : VirtualFileFinderFactory {
override fun create(scope: GlobalSearchScope): JsVirtualFileFinder
@NotNull
@Override
public VirtualFileFinder create(@NotNull GlobalSearchScope scope) {
return new IDEVirtualFileFinder(scope);
public object SERVICE {
platformStatic
public fun getInstance(project: Project): JsVirtualFileFinderFactory =
ServiceManager.getService(project, javaClass<JsVirtualFileFinderFactory>())
}
}
@@ -0,0 +1,29 @@
/*
* 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.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.load.kotlin.JvmVirtualFileFinder
import org.jetbrains.kotlin.load.kotlin.JvmVirtualFileFinderFactory
public class JvmIDEVirtualFileFinderFactory : JvmVirtualFileFinderFactory {
override fun create(scope: GlobalSearchScope): JvmVirtualFileFinder = JvmIDEVirtualFileFinder(scope)
}
public class JsIDEVirtualFileFinderFactory : JsVirtualFileFinderFactory {
override fun create(scope: GlobalSearchScope): JsVirtualFileFinder = JsIDEVirtualFileFinder(scope)
}
@@ -0,0 +1,69 @@
/*
* 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.diagnostic.Logger
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.util.indexing.FileBasedIndex
import com.intellij.util.indexing.ID
import org.jetbrains.kotlin.load.kotlin.JvmVirtualFileFinder
import org.jetbrains.kotlin.load.kotlin.VirtualFileKotlinClassFinder
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
private fun checkScopeForFinder(scope: GlobalSearchScope, logger: Logger) {
if (scope != GlobalSearchScope.EMPTY_SCOPE && scope.getProject() == null) {
logger.warn("Scope with null project " + scope)
}
}
private fun findVirtualFileWithHeader(classId: ClassId, key: ID<FqName, Void>, scope: GlobalSearchScope, logger: Logger): VirtualFile? {
val files = FileBasedIndex.getInstance().getContainingFiles<FqName, Void>(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()
}
public class JsIDEVirtualFileFinder(private val scope: GlobalSearchScope) : JsVirtualFileFinder {
init { checkScopeForFinder(scope, LOG) }
override fun findVirtualFileWithHeader(classId: ClassId): VirtualFile? =
findVirtualFileWithHeader(classId, KotlinJavaScriptMetaFileIndex.KEY, scope, LOG)
companion object {
private val LOG = Logger.getInstance(javaClass<JsIDEVirtualFileFinder>())
}
}
public class JvmIDEVirtualFileFinder(private val scope: GlobalSearchScope) : VirtualFileKotlinClassFinder(), JvmVirtualFileFinder {
init { checkScopeForFinder(scope, LOG) }
override fun findVirtualFileWithHeader(classId: ClassId): VirtualFile? =
findVirtualFileWithHeader(classId, KotlinClassFileIndex.KEY, scope, LOG)
companion object {
private val LOG = Logger.getInstance(javaClass<JvmIDEVirtualFileFinder>())
}
}