JetCoreEnvironment encapsulates a project (in the IDEA sense), so the classpath and sources lie in its area of responsibility

This commit is contained in:
Andrey Breslav
2012-04-11 15:38:22 +04:00
parent afd1c79dfd
commit f34e492022
5 changed files with 80 additions and 102 deletions
@@ -33,7 +33,7 @@ public class CliJetFilesProvider extends JetFilesProvider {
private Function<JetFile,Collection<JetFile>> all_files = new Function<JetFile, Collection<JetFile>>() { private Function<JetFile,Collection<JetFile>> all_files = new Function<JetFile, Collection<JetFile>>() {
@Override @Override
public Collection<JetFile> fun(JetFile file) { public Collection<JetFile> fun(JetFile file) {
return environment.getSession().getSourceFiles(); return environment.getSourceFiles();
} }
}; };
@@ -50,12 +50,9 @@ public class CliJetFilesProvider extends JetFilesProvider {
@Override @Override
public List<JetFile> allInScope(GlobalSearchScope scope) { public List<JetFile> allInScope(GlobalSearchScope scope) {
List<JetFile> answer = new ArrayList<JetFile>(); List<JetFile> answer = new ArrayList<JetFile>();
CompileSession session = environment.getSession(); for (JetFile file : environment.getSourceFiles()) {
if(session != null) { if (scope.contains(file.getVirtualFile())) {
for (JetFile file : session.getSourceFiles()) { answer.add(file);
if (scope.contains(file.getVirtualFile())) {
answer.add(file);
}
} }
} }
return answer; return answer;
@@ -254,7 +254,7 @@ public class CompileEnvironment {
public List<Module> loadModuleScript(String moduleFile) { public List<Module> loadModuleScript(String moduleFile) {
CompileSession scriptCompileSession = newCompileSession(); CompileSession scriptCompileSession = newCompileSession();
scriptCompileSession.addSources(moduleFile); environment.addSources(moduleFile);
ensureRuntime(); ensureRuntime();
if (compilerDependencies.getRuntimeJar() != null) { if (compilerDependencies.getRuntimeJar() != null) {
@@ -322,7 +322,7 @@ public class CompileEnvironment {
throw new CompileEnvironmentException("'" + source + "' does not exist"); throw new CompileEnvironmentException("'" + source + "' does not exist");
} }
moduleCompileSession.addSources(source.getPath()); environment.addSources(source.getPath());
} }
for (String classpathRoot : moduleBuilder.getClasspathRoots()) { for (String classpathRoot : moduleBuilder.getClasspathRoots()) {
environment.addToClasspath(new File(classpathRoot)); environment.addToClasspath(new File(classpathRoot));
@@ -415,7 +415,7 @@ public class CompileEnvironment {
public ClassLoader compileText(String code) { public ClassLoader compileText(String code) {
CompileSession session = newCompileSession(); 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) { if (!session.analyze() && !ignoreErrors) {
return null; return null;
@@ -429,7 +429,7 @@ public class CompileEnvironment {
CompileSession session = newCompileSession(); CompileSession session = newCompileSession();
session.setStubs(compilerDependencies.getCompilerSpecialMode().isStubs()); session.setStubs(compilerDependencies.getCompilerSpecialMode().isStubs());
session.addSources(sourceFileOrDir); environment.addSources(sourceFileOrDir);
return compileBunchOfSources(jar, outputDir, includeRuntime, session); return compileBunchOfSources(jar, outputDir, includeRuntime, session);
} }
@@ -439,7 +439,7 @@ public class CompileEnvironment {
session.setStubs(compilerDependencies.getCompilerSpecialMode().isStubs()); session.setStubs(compilerDependencies.getCompilerSpecialMode().isStubs());
for (String source : sources) { for (String source : sources) {
session.addSources(source); environment.addSources(source);
} }
return compileBunchOfSources(jar, outputDir, includeRuntime, session); return compileBunchOfSources(jar, outputDir, includeRuntime, session);
@@ -447,7 +447,7 @@ public class CompileEnvironment {
private boolean compileBunchOfSources(String jar, String outputDir, boolean includeRuntime, CompileSession session) { private boolean compileBunchOfSources(String jar, String outputDir, boolean includeRuntime, CompileSession session) {
FqName mainClass = null; FqName mainClass = null;
for (JetFile file : session.getSourceFiles()) { for (JetFile file : environment.getSourceFiles()) {
if (JetMainDetector.hasMain(file.getDeclarations())) { if (JetMainDetector.hasMain(file.getDeclarations())) {
FqName fqName = JetPsiUtil.getFQName(file); FqName fqName = JetPsiUtil.getFQName(file);
mainClass = fqName.child(JvmAbi.PACKAGE_CLASS); mainClass = fqName.child(JvmAbi.PACKAGE_CLASS);
@@ -480,7 +480,6 @@ public class CompileEnvironment {
private CompileSession newCompileSession() { private CompileSession newCompileSession() {
CompileSession answer = new CompileSession(environment, messageRenderer, errorStream, verbose, compilerDependencies); CompileSession answer = new CompileSession(environment, messageRenderer, errorStream, verbose, compilerDependencies);
environment.setSession(answer);
return answer; return answer;
} }
@@ -23,7 +23,6 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiErrorElement; import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiRecursiveElementWalkingVisitor; import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.analyzer.AnalyzeExhaust; 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.AnalyzerFacadeForJVM;
import org.jetbrains.jet.lang.resolve.java.CompilerDependencies; import org.jetbrains.jet.lang.resolve.java.CompilerDependencies;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver; import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.plugin.JetFileType;
import org.jetbrains.jet.utils.Progress; import org.jetbrains.jet.utils.Progress;
import java.io.File;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@@ -62,12 +58,10 @@ import java.util.List;
public class CompileSession { public class CompileSession {
private final JetCoreEnvironment environment; private final JetCoreEnvironment environment;
private final MessageCollector messageCollector; private final MessageCollector messageCollector;
private final List<JetFile> sourceFiles = new ArrayList<JetFile>();
private List<String> errors = new ArrayList<String>();
private boolean stubs = false; private boolean stubs = false;
private final MessageRenderer messageRenderer; private final MessageRenderer messageRenderer;
private final PrintStream errorStream; private final PrintStream errorStream;
private final boolean isVerbose; private final boolean verbose;
private final CompilerDependencies compilerDependencies; private final CompilerDependencies compilerDependencies;
private AnalyzeExhaust bindingContext; private AnalyzeExhaust bindingContext;
@@ -76,9 +70,9 @@ public class CompileSession {
this.environment = environment; this.environment = environment;
this.messageRenderer = messageRenderer; this.messageRenderer = messageRenderer;
this.errorStream = errorStream; this.errorStream = errorStream;
isVerbose = verbose; this.verbose = verbose;
this.compilerDependencies = compilerDependencies; this.compilerDependencies = compilerDependencies;
messageCollector = new MessageCollector(this.messageRenderer); this.messageCollector = new MessageCollector(this.messageRenderer);
} }
@NotNull @NotNull
@@ -90,68 +84,7 @@ public class CompileSession {
this.stubs = stubs; 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() { public boolean analyze() {
for (String error : errors) {
messageCollector.report(Severity.ERROR, error, null, -1, -1);
}
reportSyntaxErrors(); reportSyntaxErrors();
analyzeAndReportSemanticErrors(); analyzeAndReportSemanticErrors();
@@ -178,7 +111,7 @@ public class CompileSession {
Predicate<PsiFile> filesToAnalyzeCompletely = Predicate<PsiFile> filesToAnalyzeCompletely =
stubs ? Predicates.<PsiFile>alwaysFalse() : Predicates.<PsiFile>alwaysTrue(); stubs ? Predicates.<PsiFile>alwaysFalse() : Predicates.<PsiFile>alwaysTrue();
bindingContext = AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration( bindingContext = AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
environment.getProject(), sourceFiles, filesToAnalyzeCompletely, JetControlFlowDataTraceFactory.EMPTY, environment.getProject(), environment.getSourceFiles(), filesToAnalyzeCompletely, JetControlFlowDataTraceFactory.EMPTY,
compilerDependencies); compilerDependencies);
for (Diagnostic diagnostic : bindingContext.getBindingContext().getDiagnostics()) { for (Diagnostic diagnostic : bindingContext.getBindingContext().getDiagnostics()) {
@@ -200,7 +133,7 @@ public class CompileSession {
} }
private void reportSyntaxErrors() { private void reportSyntaxErrors() {
for (JetFile file : sourceFiles) { for (JetFile file : environment.getSourceFiles()) {
file.accept(new PsiRecursiveElementWalkingVisitor() { file.accept(new PsiRecursiveElementWalkingVisitor() {
@Override @Override
public void visitErrorElement(PsiErrorElement element) { public void visitErrorElement(PsiErrorElement element) {
@@ -224,13 +157,13 @@ public class CompileSession {
public GenerationState generate(boolean module) { public GenerationState generate(boolean module) {
Project project = environment.getProject(); Project project = environment.getProject();
GenerationState generationState = new GenerationState(project, ClassBuilderFactories.binaries(stubs), 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); generationState.compileCorrectFiles(CompilationErrorHandler.THROW_EXCEPTION);
List<CompilerPlugin> plugins = environment.getCompilerPlugins(); List<CompilerPlugin> plugins = environment.getCompilerPlugins();
if (!module) { if (!module) {
if (plugins != null) { if (plugins != null) {
CompilerPluginContext context = new CompilerPluginContext(project, bindingContext.getBindingContext(), getSourceFiles()); CompilerPluginContext context = new CompilerPluginContext(project, bindingContext.getBindingContext(), environment.getSourceFiles());
for (CompilerPlugin plugin : plugins) { for (CompilerPlugin plugin : plugins) {
plugin.processFiles(context); plugin.processFiles(context);
} }
@@ -23,15 +23,17 @@ import com.intellij.openapi.Disposable;
import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElementFinder; import com.intellij.psi.PsiElementFinder;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.asJava.JavaElementFinder; import org.jetbrains.jet.asJava.JavaElementFinder;
import org.jetbrains.jet.lang.parsing.JetParserDefinition; 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.CompilerDependencies;
import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode; import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
import org.jetbrains.jet.lang.resolve.java.JetFilesProvider; import org.jetbrains.jet.lang.resolve.java.JetFilesProvider;
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
import org.jetbrains.jet.plugin.JetFileType; import org.jetbrains.jet.plugin.JetFileType;
import org.jetbrains.jet.utils.PathUtil;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
@@ -43,8 +45,8 @@ import java.util.List;
* @author yole * @author yole
*/ */
public class JetCoreEnvironment extends JavaCoreEnvironment { public class JetCoreEnvironment extends JavaCoreEnvironment {
private final List<JetFile> sourceFiles = new ArrayList<JetFile>();
private List<CompilerPlugin> compilerPlugins = new ArrayList<CompilerPlugin>(); private List<CompilerPlugin> compilerPlugins = new ArrayList<CompilerPlugin>();
private CompileSession session;
public JetCoreEnvironment(Disposable parentDisposable, @NotNull CompilerDependencies compilerDependencies) { public JetCoreEnvironment(Disposable parentDisposable, @NotNull CompilerDependencies compilerDependencies) {
super(parentDisposable); super(parentDisposable);
@@ -81,6 +83,61 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
return myApplication; 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() { public List<CompilerPlugin> getCompilerPlugins() {
return compilerPlugins; return compilerPlugins;
} }
@@ -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()); myEnvironment.addToClasspath(compilerDependencies.getRuntimeJar());
CoreLocalFileSystem localFileSystem = myEnvironment.getLocalFileSystem(); CoreLocalFileSystem localFileSystem = myEnvironment.getLocalFileSystem();
session.addSources(localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../libraries/stdlib/test")); myEnvironment.addSources(localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../libraries/stdlib/test"));
session.addSources(localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../libraries/kunit/src")); myEnvironment.addSources(localFileSystem.findFileByPath(JetParsingTest.getTestDataDir() + "/../../libraries/kunit/src"));
if (!session.analyze()) { if (!session.analyze()) {
throw new RuntimeException("There were compilation errors"); throw new RuntimeException("There were compilation errors");
@@ -107,7 +107,7 @@ public class TestlibTest extends CodegenTestCase {
JetTypeMapper typeMapper = state.getInjector().getJetTypeMapper(); JetTypeMapper typeMapper = state.getInjector().getJetTypeMapper();
TestSuite suite = new TestSuite("stdlib_test"); TestSuite suite = new TestSuite("stdlib_test");
try { try {
for(JetFile jetFile : session.getSourceFiles()) { for(JetFile jetFile : myEnvironment.getSourceFiles()) {
for(JetDeclaration decl : jetFile.getDeclarations()) { for(JetDeclaration decl : jetFile.getDeclarations()) {
if(decl instanceof JetClass) { if(decl instanceof JetClass) {
JetClass jetClass = (JetClass) decl; JetClass jetClass = (JetClass) decl;