提交 f34e4920 编写于 作者: A Andrey Breslav

JetCoreEnvironment encapsulates a project (in the IDEA sense), so the...

JetCoreEnvironment encapsulates a project (in the IDEA sense), so the classpath and sources lie in its area of responsibility
上级 afd1c79d
......@@ -33,7 +33,7 @@ public class CliJetFilesProvider extends JetFilesProvider {
private Function<JetFile,Collection<JetFile>> all_files = new Function<JetFile, Collection<JetFile>>() {
@Override
public Collection<JetFile> fun(JetFile file) {
return environment.getSession().getSourceFiles();
return environment.getSourceFiles();
}
};
......@@ -50,12 +50,9 @@ public class CliJetFilesProvider extends JetFilesProvider {
@Override
public List<JetFile> allInScope(GlobalSearchScope scope) {
List<JetFile> answer = new ArrayList<JetFile>();
CompileSession session = environment.getSession();
if(session != null) {
for (JetFile file : session.getSourceFiles()) {
if (scope.contains(file.getVirtualFile())) {
answer.add(file);
}
for (JetFile file : environment.getSourceFiles()) {
if (scope.contains(file.getVirtualFile())) {
answer.add(file);
}
}
return answer;
......
......@@ -254,7 +254,7 @@ public class CompileEnvironment {
public List<Module> loadModuleScript(String moduleFile) {
CompileSession scriptCompileSession = newCompileSession();
scriptCompileSession.addSources(moduleFile);
environment.addSources(moduleFile);
ensureRuntime();
if (compilerDependencies.getRuntimeJar() != null) {
......@@ -322,7 +322,7 @@ public class CompileEnvironment {
throw new CompileEnvironmentException("'" + source + "' does not exist");
}
moduleCompileSession.addSources(source.getPath());
environment.addSources(source.getPath());
}
for (String classpathRoot : moduleBuilder.getClasspathRoots()) {
environment.addToClasspath(new File(classpathRoot));
......@@ -415,7 +415,7 @@ public class CompileEnvironment {
public ClassLoader compileText(String code) {
CompileSession session = newCompileSession();
session.addSources(new LightVirtualFile("script" + LocalTimeCounter.currentTime() + ".kt", JetLanguage.INSTANCE, code));
environment.addSources(new LightVirtualFile("script" + LocalTimeCounter.currentTime() + ".kt", JetLanguage.INSTANCE, code));
if (!session.analyze() && !ignoreErrors) {
return null;
......@@ -429,7 +429,7 @@ public class CompileEnvironment {
CompileSession session = newCompileSession();
session.setStubs(compilerDependencies.getCompilerSpecialMode().isStubs());
session.addSources(sourceFileOrDir);
environment.addSources(sourceFileOrDir);
return compileBunchOfSources(jar, outputDir, includeRuntime, session);
}
......@@ -439,7 +439,7 @@ public class CompileEnvironment {
session.setStubs(compilerDependencies.getCompilerSpecialMode().isStubs());
for (String source : sources) {
session.addSources(source);
environment.addSources(source);
}
return compileBunchOfSources(jar, outputDir, includeRuntime, session);
......@@ -447,7 +447,7 @@ public class CompileEnvironment {
private boolean compileBunchOfSources(String jar, String outputDir, boolean includeRuntime, CompileSession session) {
FqName mainClass = null;
for (JetFile file : session.getSourceFiles()) {
for (JetFile file : environment.getSourceFiles()) {
if (JetMainDetector.hasMain(file.getDeclarations())) {
FqName fqName = JetPsiUtil.getFQName(file);
mainClass = fqName.child(JvmAbi.PACKAGE_CLASS);
......@@ -480,7 +480,6 @@ public class CompileEnvironment {
private CompileSession newCompileSession() {
CompileSession answer = new CompileSession(environment, messageRenderer, errorStream, verbose, compilerDependencies);
environment.setSession(answer);
return answer;
}
......
......@@ -23,7 +23,6 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
......@@ -45,12 +44,9 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.lang.resolve.java.CompilerDependencies;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.plugin.JetFileType;
import org.jetbrains.jet.utils.Progress;
import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
......@@ -62,12 +58,10 @@ import java.util.List;
public class CompileSession {
private final JetCoreEnvironment environment;
private final MessageCollector messageCollector;
private final List<JetFile> sourceFiles = new ArrayList<JetFile>();
private List<String> errors = new ArrayList<String>();
private boolean stubs = false;
private final MessageRenderer messageRenderer;
private final PrintStream errorStream;
private final boolean isVerbose;
private final boolean verbose;
private final CompilerDependencies compilerDependencies;
private AnalyzeExhaust bindingContext;
......@@ -76,9 +70,9 @@ public class CompileSession {
this.environment = environment;
this.messageRenderer = messageRenderer;
this.errorStream = errorStream;
isVerbose = verbose;
this.verbose = verbose;
this.compilerDependencies = compilerDependencies;
messageCollector = new MessageCollector(this.messageRenderer);
this.messageCollector = new MessageCollector(this.messageRenderer);
}
@NotNull
......@@ -90,68 +84,7 @@ public class CompileSession {
this.stubs = stubs;
}
public void addSources(String path) {
if(path == null)
return;
VirtualFile vFile = environment.getLocalFileSystem().findFileByPath(path);
if (vFile == null) {
errors.add("File/directory not found: " + path);
return;
}
if (!vFile.isDirectory() && vFile.getFileType() != JetFileType.INSTANCE) {
errors.add("Not a Kotlin file: " + path);
return;
}
addSources(new File(path));
}
private void addSources(File file) {
if(file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File child : files) {
addSources(child);
}
}
}
else {
VirtualFile fileByPath = environment.getLocalFileSystem().findFileByPath(file.getAbsolutePath());
if (fileByPath != null) {
PsiFile psiFile = PsiManager.getInstance(environment.getProject()).findFile(fileByPath);
if(psiFile instanceof JetFile) {
sourceFiles.add((JetFile)psiFile);
}
}
}
}
public void addSources(VirtualFile vFile) {
if (vFile.isDirectory()) {
for (VirtualFile virtualFile : vFile.getChildren()) {
addSources(virtualFile);
}
}
else {
if (vFile.getFileType() == JetFileType.INSTANCE) {
PsiFile psiFile = PsiManager.getInstance(environment.getProject()).findFile(vFile);
if (psiFile instanceof JetFile) {
sourceFiles.add((JetFile)psiFile);
}
}
}
}
public List<JetFile> getSourceFiles() {
return sourceFiles;
}
public boolean analyze() {
for (String error : errors) {
messageCollector.report(Severity.ERROR, error, null, -1, -1);
}
reportSyntaxErrors();
analyzeAndReportSemanticErrors();
......@@ -178,7 +111,7 @@ public class CompileSession {
Predicate<PsiFile> filesToAnalyzeCompletely =
stubs ? Predicates.<PsiFile>alwaysFalse() : Predicates.<PsiFile>alwaysTrue();
bindingContext = AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
environment.getProject(), sourceFiles, filesToAnalyzeCompletely, JetControlFlowDataTraceFactory.EMPTY,
environment.getProject(), environment.getSourceFiles(), filesToAnalyzeCompletely, JetControlFlowDataTraceFactory.EMPTY,
compilerDependencies);
for (Diagnostic diagnostic : bindingContext.getBindingContext().getDiagnostics()) {
......@@ -200,7 +133,7 @@ public class CompileSession {
}
private void reportSyntaxErrors() {
for (JetFile file : sourceFiles) {
for (JetFile file : environment.getSourceFiles()) {
file.accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitErrorElement(PsiErrorElement element) {
......@@ -224,13 +157,13 @@ public class CompileSession {
public GenerationState generate(boolean module) {
Project project = environment.getProject();
GenerationState generationState = new GenerationState(project, ClassBuilderFactories.binaries(stubs),
isVerbose ? new BackendProgress() : Progress.DEAF, bindingContext, sourceFiles, compilerDependencies.getCompilerSpecialMode());
verbose ? new BackendProgress() : Progress.DEAF, bindingContext, environment.getSourceFiles(), compilerDependencies.getCompilerSpecialMode());
generationState.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION);
List<CompilerPlugin> plugins = environment.getCompilerPlugins();
if (!module) {
if (plugins != null) {
CompilerPluginContext context = new CompilerPluginContext(project, bindingContext.getBindingContext(), getSourceFiles());
CompilerPluginContext context = new CompilerPluginContext(project, bindingContext.getBindingContext(), environment.getSourceFiles());
for (CompilerPlugin plugin : plugins) {
plugin.processFiles(context);
}
......
......@@ -23,15 +23,17 @@ import com.intellij.openapi.Disposable;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElementFinder;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.asJava.JavaElementFinder;
import org.jetbrains.jet.lang.parsing.JetParserDefinition;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.java.CompilerDependencies;
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
import org.jetbrains.jet.lang.resolve.java.JetFilesProvider;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import org.jetbrains.jet.plugin.JetFileType;
import org.jetbrains.jet.utils.PathUtil;
import java.io.File;
import java.net.URL;
......@@ -43,8 +45,8 @@ import java.util.List;
* @author yole
*/
public class JetCoreEnvironment extends JavaCoreEnvironment {
private final List<JetFile> sourceFiles = new ArrayList<JetFile>();
private List<CompilerPlugin> compilerPlugins = new ArrayList<CompilerPlugin>();
private CompileSession session;
public JetCoreEnvironment(Disposable parentDisposable, @NotNull CompilerDependencies compilerDependencies) {
super(parentDisposable);
......@@ -81,6 +83,61 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
return myApplication;
}
private void addSources(File file) {
if(file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File child : files) {
addSources(child);
}
}
}
else {
VirtualFile fileByPath = getLocalFileSystem().findFileByPath(file.getAbsolutePath());
if (fileByPath != null) {
PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(fileByPath);
if(psiFile instanceof JetFile) {
sourceFiles.add((JetFile)psiFile);
}
}
}
}
public void addSources(VirtualFile vFile) {
if (vFile.isDirectory()) {
for (VirtualFile virtualFile : vFile.getChildren()) {
addSources(virtualFile);
}
}
else {
if (vFile.getFileType() == JetFileType.INSTANCE) {
PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(vFile);
if (psiFile instanceof JetFile) {
sourceFiles.add((JetFile)psiFile);
}
}
}
}
public void addSources(String path) {
if(path == null)
return;
VirtualFile vFile = getLocalFileSystem().findFileByPath(path);
if (vFile == null) {
throw new CompileEnvironmentException("File/directory not found: " + path);
}
if (!vFile.isDirectory() && vFile.getFileType() != JetFileType.INSTANCE) {
throw new CompileEnvironmentException("Not a Kotlin file: " + path);
}
addSources(new File(path));
}
public List<JetFile> getSourceFiles() {
return sourceFiles;
}
public List<CompilerPlugin> getCompilerPlugins() {
return compilerPlugins;
}
......@@ -93,7 +150,7 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
ClassLoader parent = loader.getParent();
if(parent != null)
addToClasspathFromClassLoader(parent);
if(loader instanceof URLClassLoader) {
for (URL url : ((URLClassLoader) loader).getURLs()) {
File file = new File(url.getPath());
......@@ -102,12 +159,4 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
}
}
}
public void setSession(CompileSession session) {
this.session = session;
}
public CompileSession getSession() {
return session;
}
}
......@@ -89,8 +89,8 @@ public class TestlibTest extends CodegenTestCase {
myEnvironment.addToClasspath(compilerDependencies.getRuntimeJar());
CoreLocalFileSystem localFileSystem = myEnvironment.getLocalFileSystem();
session.addSources(localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../libraries/stdlib/test"));
session.addSources(localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../libraries/kunit/src"));
myEnvironment.addSources(localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../libraries/stdlib/test"));
myEnvironment.addSources(localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../libraries/kunit/src"));
if (!session.analyze()) {
throw new RuntimeException("There were compilation errors");
......@@ -107,7 +107,7 @@ public class TestlibTest extends CodegenTestCase {
JetTypeMapper typeMapper = state.getInjector().getJetTypeMapper();
TestSuite suite = new TestSuite("stdlib_test");
try {
for(JetFile jetFile : session.getSourceFiles()) {
for(JetFile jetFile : myEnvironment.getSourceFiles()) {
for(JetDeclaration decl : jetFile.getDeclarations()) {
if(decl instanceof JetClass) {
JetClass jetClass = (JetClass) decl;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册