Change AnalyzerFacade#createSetup to accept a list of synthetic files and a scope for physical source files

Make DeclarationProviderFactory a project service
This commit is contained in:
Pavel V. Talanov
2014-06-24 20:05:45 +04:00
parent f7351a72de
commit 0a43c38bae
14 changed files with 182 additions and 145 deletions
@@ -152,8 +152,6 @@ public class JetCoreEnvironment {
applicationEnvironment.getApplication().registerService(OperationModeProvider.class, new CompilerModeProvider());
applicationEnvironment.getApplication().registerService(KotlinBinaryClassCache.class, new KotlinBinaryClassCache());
applicationEnvironment.getApplication().registerService(DeclarationProviderFactoryService.class,
new CliDeclarationProviderFactoryService());
return applicationEnvironment;
}
@@ -219,6 +217,7 @@ public class JetCoreEnvironment {
configuration.getList(CommonConfigurationKeys.SCRIPT_DEFINITIONS_KEY));
project.registerService(VirtualFileFinder.class, new CliVirtualFileFinder(classPath));
project.registerService(DeclarationProviderFactoryService.class, new CliDeclarationProviderFactoryService(sourceFiles));
}
public CompilerConfiguration getConfiguration() {
@@ -19,7 +19,11 @@ package org.jetbrains.jet.lang.resolve.java;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;
import kotlin.Function1;
import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.analyzer.AnalyzerFacade;
@@ -47,6 +51,7 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public enum AnalyzerFacadeForJVM implements AnalyzerFacade {
@@ -80,8 +85,12 @@ public enum AnalyzerFacadeForJVM implements AnalyzerFacade {
@NotNull
@Override
public JvmSetup createSetup(@NotNull Project fileProject, @NotNull Collection<JetFile> files) {
return createSetup(fileProject, files, new BindingTraceContext(), true);
public JvmSetup createSetup(
@NotNull Project fileProject,
@NotNull Collection<JetFile> syntheticFiles,
@NotNull GlobalSearchScope filesScope
) {
return createSetup(fileProject, syntheticFiles, filesScope, new BindingTraceContext(), true);
}
@NotNull
@@ -91,20 +100,27 @@ public enum AnalyzerFacadeForJVM implements AnalyzerFacade {
@NotNull BindingTrace trace,
boolean addBuiltIns
) {
return createSetup(project, files, trace, addBuiltIns).getLazyResolveSession();
List<VirtualFile> virtualFiles = KotlinPackage.map(files, new Function1<JetFile, VirtualFile>() {
@Override
public VirtualFile invoke(JetFile file) {
return file.getVirtualFile();
}
});
return createSetup(project, Collections.<JetFile>emptyList(),
GlobalSearchScope.filesScope(project, virtualFiles), trace, addBuiltIns).getLazyResolveSession();
}
private static JvmSetup createSetup(
Project project,
Collection<JetFile> files,
BindingTrace trace,
public static JvmSetup createSetup(
@NotNull Project project,
@NotNull Collection<JetFile> syntheticFiles,
@NotNull GlobalSearchScope filesScope,
@NotNull BindingTrace trace,
boolean addBuiltIns
) {
GlobalContextImpl globalContext = ContextPackage.GlobalContext();
DeclarationProviderFactory declarationProviderFactory =
DeclarationProviderFactoryService.createDeclarationProviderFactory(project, globalContext.getStorageManager(), files);
DeclarationProviderFactory declarationProviderFactory = DeclarationProviderFactoryService.object$
.createDeclarationProviderFactory(project, globalContext.getStorageManager(), syntheticFiles, filesScope);
InjectorForLazyResolveWithJava resolveWithJava = new InjectorForLazyResolveWithJava(
project,
@@ -17,6 +17,7 @@
package org.jetbrains.jet.analyzer;
import com.intellij.openapi.project.Project;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
@@ -48,6 +49,7 @@ public interface AnalyzerFacade {
@NotNull
Setup createSetup(
@NotNull Project project,
@NotNull Collection<JetFile> files
@NotNull Collection<JetFile> syntheticFiles,
@NotNull GlobalSearchScope filesScope
);
}
@@ -1,34 +0,0 @@
/*
* 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.lazy.declarations;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.storage.StorageManager;
import java.util.Collection;
public final class CliDeclarationProviderFactoryService extends DeclarationProviderFactoryService {
@NotNull
@Override
public DeclarationProviderFactory create(
@NotNull Project project, @NotNull StorageManager storageManager, @NotNull Collection<JetFile> files
) {
return new FileBasedDeclarationProviderFactory(storageManager, files);
}
}
@@ -0,0 +1,42 @@
/*
* 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.lazy.declarations
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.storage.StorageManager
import org.jetbrains.kotlin.util.sure
import java.util.ArrayList
public class CliDeclarationProviderFactoryService(private val sourceFiles: Collection<JetFile>) : DeclarationProviderFactoryService() {
override fun create(
project: Project,
storageManager: StorageManager,
syntheticFiles: Collection<JetFile>,
filesScope: GlobalSearchScope
): DeclarationProviderFactory {
val allFiles = ArrayList<JetFile>()
sourceFiles.filterTo(allFiles) {
val vFile = it.getVirtualFile().sure("Source files should be physical files")
filesScope.contains(vFile)
}
allFiles addAll syntheticFiles
return FileBasedDeclarationProviderFactory(storageManager, allFiles)
}
}
@@ -1,43 +0,0 @@
/*
* 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.lazy.declarations;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.storage.StorageManager;
import java.util.Collection;
public abstract class DeclarationProviderFactoryService {
@NotNull
public abstract DeclarationProviderFactory create(
@NotNull Project project,
@NotNull StorageManager storageManager,
@NotNull Collection<JetFile> files
);
@NotNull
public static DeclarationProviderFactory createDeclarationProviderFactory(
@NotNull Project project,
@NotNull StorageManager storageManager,
@NotNull Collection<JetFile> files
) {
return ServiceManager.getService(DeclarationProviderFactoryService.class).create(project, storageManager, files);
}
}
@@ -0,0 +1,64 @@
/*
* 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.lazy.declarations
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import com.intellij.psi.search.DelegatingGlobalSearchScope
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.storage.StorageManager
import org.jetbrains.kotlin.util.sure
import com.intellij.openapi.vfs.VirtualFile
import java.util.HashSet
public abstract class DeclarationProviderFactoryService {
public abstract fun create(
project: Project,
storageManager: StorageManager,
syntheticFiles: Collection<JetFile>,
filesScope: GlobalSearchScope
): DeclarationProviderFactory
class object {
public fun createDeclarationProviderFactory(
project: Project,
storageManager: StorageManager,
syntheticFiles: Collection<JetFile>,
filesScope: GlobalSearchScope
): DeclarationProviderFactory {
return ServiceManager.getService(project, javaClass<DeclarationProviderFactoryService>())!!
.create(project, storageManager, syntheticFiles, SyntheticFilesFilteringScope(syntheticFiles, filesScope))
}
}
private class SyntheticFilesFilteringScope(syntheticFiles: Collection<JetFile>, baseScope: GlobalSearchScope) :
DelegatingGlobalSearchScope(baseScope) {
val originals = syntheticFiles.map {
it.getOriginalFile().getVirtualFile()
}.filterNotNullTo(HashSet<VirtualFile>())
override fun contains(file: VirtualFile): Boolean {
if (file in originals) return false
return super.contains(file)
}
}
}
@@ -20,6 +20,7 @@ import com.google.common.base.Predicates;
import com.google.common.collect.Sets;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.cli.jvm.compiler.CliLightClassGenerationSupport;
@@ -68,7 +69,8 @@ public class LazyResolveTestUtil {
CliLightClassGenerationSupport support = CliLightClassGenerationSupport.getInstanceForCli(project);
BindingTrace sharedTrace = support.getTrace();
ResolveSession lazyResolveSession = AnalyzerFacadeForJVM.createLazyResolveSession(project, files, sharedTrace, addBuiltIns);
ResolveSession lazyResolveSession = AnalyzerFacadeForJVM.createSetup(project, files, GlobalSearchScope.EMPTY_SCOPE,
sharedTrace, addBuiltIns).getLazyResolveSession();
support.setModule((ModuleDescriptorImpl)lazyResolveSession.getModuleDescriptor());
return lazyResolveSession;