Introduce VirtualFileFinderFactory

Allow creation of VirtualFileFinder with restricted scope
This commit is contained in:
Pavel V. Talanov
2014-06-06 17:23:08 +04:00
parent 331f24d01a
commit 09d3ddfcae
7 changed files with 113 additions and 11 deletions
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2014 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.jet.cli.jvm.compiler;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinderFactory;
public final class CliVirtualFileFinderFactory implements VirtualFileFinderFactory {
@NotNull
private final ClassPath classPath;
public CliVirtualFileFinderFactory(@NotNull ClassPath path) {
classPath = path;
}
@NotNull
@Override
public VirtualFileFinder create(@NotNull GlobalSearchScope scope) {
return new CliVirtualFileFinder(classPath);
}
}
@@ -56,7 +56,7 @@ import org.jetbrains.jet.lang.parsing.JetParserDefinition;
import org.jetbrains.jet.lang.parsing.JetScriptDefinitionProvider;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.kotlin.KotlinBinaryClassCache;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinderFactory;
import org.jetbrains.jet.lang.resolve.lazy.declarations.CliDeclarationProviderFactoryService;
import org.jetbrains.jet.lang.resolve.lazy.declarations.DeclarationProviderFactoryService;
import org.jetbrains.jet.plugin.JetFileType;
@@ -215,7 +215,7 @@ public class JetCoreEnvironment {
JetScriptDefinitionProvider.getInstance(project).addScriptDefinitions(
configuration.getList(CommonConfigurationKeys.SCRIPT_DEFINITIONS_KEY));
project.registerService(VirtualFileFinder.class, new CliVirtualFileFinder(classPath));
project.registerService(VirtualFileFinderFactory.class, new CliVirtualFileFinderFactory(classPath));
}
// made public for Upsource
@@ -19,6 +19,7 @@ package org.jetbrains.jet.lang.resolve.kotlin;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.name.FqName;
@@ -27,11 +28,10 @@ public interface VirtualFileFinder extends KotlinClassFinder {
class SERVICE {
@NotNull
public static VirtualFileFinder getInstance(@NotNull Project project) {
return ServiceManager.getService(project, VirtualFileFinder.class);
return ServiceManager.getService(project, VirtualFileFinderFactory.class).create(GlobalSearchScope.allScope(project));
}
}
// TODO: support scope
@Nullable
VirtualFile findVirtualFileWithHeader(@NotNull FqName className);
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2014 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.jet.lang.resolve.kotlin;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
public interface VirtualFileFinderFactory {
@NotNull
VirtualFileFinder create(@NotNull GlobalSearchScope scope);
}
@@ -37,16 +37,17 @@ public final class IDEVirtualFileFinder extends VirtualFileKotlinClassFinder imp
private static final Logger LOG = Logger.getInstance(IDEVirtualFileFinder.class);
@NotNull private final Project project;
@NotNull private final GlobalSearchScope scope;
public IDEVirtualFileFinder(@NotNull Project project) {
public IDEVirtualFileFinder(@NotNull Project project, @NotNull GlobalSearchScope scope) {
this.project = project;
this.scope = scope;
}
@Nullable
@Override
public VirtualFile findVirtualFileWithHeader(@NotNull FqName className) {
Collection<VirtualFile> files =
FileBasedIndex.getInstance().getContainingFiles(KotlinClassFileIndex.KEY, className, GlobalSearchScope.allScope(project));
Collection<VirtualFile> files = FileBasedIndex.getInstance().getContainingFiles(KotlinClassFileIndex.KEY, className, scope);
if (files.isEmpty()) {
return null;
}
@@ -61,12 +62,12 @@ public final class IDEVirtualFileFinder extends VirtualFileKotlinClassFinder imp
JavaFileManager fileFinder = ServiceManager.getService(project, JavaFileManager.class);
String qName = internalName.replace('/', '.');
PsiClass psiClass = fileFinder.findClass(qName, GlobalSearchScope.allScope(project));
PsiClass psiClass = fileFinder.findClass(qName, scope);
if (psiClass == null) {
int dollarIndex = qName.indexOf('$');
assert dollarIndex > 0 : "Only inner classes could be found with this patch: " + internalName;
String newName = qName.substring(0, dollarIndex);
psiClass = fileFinder.findClass(newName, GlobalSearchScope.allScope(project));
psiClass = fileFinder.findClass(newName, scope);
if (psiClass != null) {
int i = qName.lastIndexOf('.');
return psiClass.getContainingFile().getVirtualFile().getParent().findChild(qName.substring(i + 1) + ".class");
+2 -2
View File
@@ -130,8 +130,8 @@
<projectService serviceInterface="org.jetbrains.jet.plugin.caches.resolve.KotlinCacheService"
serviceImplementation="org.jetbrains.jet.plugin.caches.resolve.KotlinCacheService"/>
<projectService serviceInterface="org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder"
serviceImplementation="org.jetbrains.jet.plugin.vfilefinder.IDEVirtualFileFinder"/>
<projectService serviceInterface="org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinderFactory"
serviceImplementation="org.jetbrains.jet.plugin.vfilefinder.IDEVirtualFileFinderFactory"/>
<projectService serviceInterface="org.jetbrains.jet.asJava.LightClassGenerationSupport"
serviceImplementation="org.jetbrains.jet.plugin.caches.resolve.IDELightClassGenerationSupport"/>
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2014 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.jet.plugin.vfilefinder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinderFactory;
public class IDEVirtualFileFinderFactory implements VirtualFileFinderFactory {
@NotNull
private final Project project;
public IDEVirtualFileFinderFactory(@NotNull Project project) {
this.project = project;
}
@NotNull
@Override
public VirtualFileFinder create(@NotNull GlobalSearchScope scope) {
return new IDEVirtualFileFinder(project, scope);
}
}