Project structure fixed
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module" module-name="frontend" />
|
||||
<orderEntry type="module" module-name="js.libraries" />
|
||||
<orderEntry type="library" name="js-libs" level="project" />
|
||||
<orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
@@ -17,11 +17,14 @@
|
||||
package org.jetbrains.k2js.analyze;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Predicates;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.Configuration;
|
||||
import org.jetbrains.jet.lang.JetSemanticServices;
|
||||
import org.jetbrains.jet.lang.StandardConfiguration;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
@@ -31,6 +34,7 @@ import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
|
||||
import org.jetbrains.k2js.config.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -89,6 +93,21 @@ public final class Analyzer {
|
||||
};
|
||||
}
|
||||
|
||||
public static BindingContext analyzeNamespace(@NotNull JetFile file) {
|
||||
BindingTraceContext bindingTraceContext = new BindingTraceContext();
|
||||
Project project = file.getProject();
|
||||
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(project);
|
||||
return AnalyzingUtils.analyzeFilesWithGivenTrace(
|
||||
project,
|
||||
JsConfiguration.jsLibConfiguration(project),
|
||||
Collections.singletonList(file),
|
||||
Predicates.<PsiFile>alwaysTrue(),
|
||||
JetControlFlowDataTraceFactory.EMPTY,
|
||||
bindingTraceContext,
|
||||
semanticServices);
|
||||
// return AnalyzerFacade.analyzeOneFileWithJavaIntegration(file, flowDataTraceFactory);
|
||||
}
|
||||
|
||||
private static final class JsConfiguration implements Configuration {
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -18,10 +18,10 @@ package org.jetbrains.k2js.config;
|
||||
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import core.Dummy;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.compiler.JetCoreEnvironment;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.k2js.utils.JetFileUtils;
|
||||
|
||||
@@ -56,32 +56,20 @@ public final class TestConfig extends Config {
|
||||
"/html5/image.kt"
|
||||
);
|
||||
|
||||
@NotNull
|
||||
private static JetCoreEnvironment getTestEnvironment() {
|
||||
if (testOnlyEnvironment == null) {
|
||||
testOnlyEnvironment = new JetCoreEnvironment(new Disposable() {
|
||||
@Override
|
||||
public void dispose() {
|
||||
}
|
||||
});
|
||||
}
|
||||
return testOnlyEnvironment;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static /*var*/ JetCoreEnvironment testOnlyEnvironment = null;
|
||||
|
||||
|
||||
@Nullable
|
||||
private /*var*/ List<JetFile> jsLibFiles = null;
|
||||
|
||||
public TestConfig() {
|
||||
@NotNull
|
||||
private final Project project;
|
||||
|
||||
public TestConfig(@NotNull Project project) {
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Project getProject() {
|
||||
return getTestEnvironment().getProject();
|
||||
return project;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -92,7 +80,7 @@ public final class TestConfig extends Config {
|
||||
//TODO: close stream?
|
||||
InputStream stream = Dummy.class.getResourceAsStream(libFileName);
|
||||
try {
|
||||
String text = readString(stream);
|
||||
String text = FileUtil.loadTextAndClose(stream);
|
||||
file = JetFileUtils.createPsiFile(libFileName, text, project);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -110,17 +98,17 @@ public final class TestConfig extends Config {
|
||||
return jsLibFiles;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String readString(@NotNull InputStream is) throws IOException {
|
||||
char[] buf = new char[2048];
|
||||
Reader r = new InputStreamReader(is, "UTF-8");
|
||||
StringBuilder s = new StringBuilder();
|
||||
while (true) {
|
||||
int n = r.read(buf);
|
||||
if (n < 0)
|
||||
break;
|
||||
s.append(buf, 0, n);
|
||||
}
|
||||
return s.toString();
|
||||
}
|
||||
// @NotNull
|
||||
// private static String readString(@NotNull InputStream is) throws IOException {
|
||||
// char[] buf = new char[2048];
|
||||
// Reader r = new InputStreamReader(is, "UTF-8");
|
||||
// StringBuilder s = new StringBuilder();
|
||||
// while (true) {
|
||||
// int n = r.read(buf);
|
||||
// if (n < 0)
|
||||
// break;
|
||||
// s.append(buf, 0, n);
|
||||
// }
|
||||
// return s.toString();
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -64,20 +64,7 @@ public final class K2JSTranslator {
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public static void testTranslateFile(@NotNull String inputFile,
|
||||
@NotNull String outputFile) throws Exception {
|
||||
testTranslateFiles(Collections.singletonList(inputFile), outputFile);
|
||||
}
|
||||
|
||||
public static void testTranslateFiles(@NotNull List<String> inputFiles,
|
||||
@NotNull String outputFile) throws Exception {
|
||||
K2JSTranslator translator = new K2JSTranslator(new TestConfig());
|
||||
List<JetFile> psiFiles = createPsiFileList(inputFiles, translator.getProject());
|
||||
JsProgram program = translator.generateProgram(psiFiles);
|
||||
saveProgramToFile(outputFile, program);
|
||||
}
|
||||
|
||||
private static void saveProgramToFile(@NotNull String outputFile, @NotNull JsProgram program) throws IOException {
|
||||
public static void saveProgramToFile(@NotNull String outputFile, @NotNull JsProgram program) throws IOException {
|
||||
CodeGenerator generator = new CodeGenerator();
|
||||
generator.generateToFile(program, new File(outputFile));
|
||||
}
|
||||
@@ -123,7 +110,7 @@ public final class K2JSTranslator {
|
||||
|
||||
|
||||
@NotNull
|
||||
private JsProgram generateProgram(@NotNull List<JetFile> filesToTranslate) {
|
||||
public JsProgram generateProgram(@NotNull List<JetFile> filesToTranslate) {
|
||||
JetStandardLibrary.initialize(config.getProject());
|
||||
BindingContext bindingContext = Analyzer.analyzeFilesAndCheckErrors(filesToTranslate, config);
|
||||
return Translation.generateAst(bindingContext, Analyzer.withJsLibAdded(filesToTranslate, config));
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.k2js.facade;
|
||||
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
@@ -33,9 +34,9 @@ public class K2JSTranslatorUtils {
|
||||
private static String EXCEPTION = "exception=";
|
||||
|
||||
@Nullable
|
||||
public String translateToJS(@NotNull String code, @NotNull String arguments) {
|
||||
public String translateToJS(@NotNull Project project, @NotNull String code, @NotNull String arguments) {
|
||||
try {
|
||||
return generateJSCode(code, arguments);
|
||||
return generateJSCode(project, code, arguments);
|
||||
} catch (AssertionError e) {
|
||||
reportException(e);
|
||||
return EXCEPTION + "Translation error.";
|
||||
@@ -49,9 +50,9 @@ public class K2JSTranslatorUtils {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BindingContext getBindingContext(@NotNull String programText) {
|
||||
public BindingContext getBindingContext(@NotNull Project project, @NotNull String programText) {
|
||||
try {
|
||||
K2JSTranslator k2JSTranslator = new K2JSTranslator(new TestConfig());
|
||||
K2JSTranslator k2JSTranslator = new K2JSTranslator(new TestConfig(project));
|
||||
return k2JSTranslator.analyzeProgramCode(programText);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
@@ -61,8 +62,8 @@ public class K2JSTranslatorUtils {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String generateJSCode(@NotNull String code, @NotNull String arguments) {
|
||||
String generatedCode = (new K2JSTranslator(new TestConfig())).translateStringWithCallToMain(code, arguments);
|
||||
private String generateJSCode(@NotNull Project project, @NotNull String code, @NotNull String arguments) {
|
||||
String generatedCode = (new K2JSTranslator(new TestConfig(project))).translateStringWithCallToMain(code, arguments);
|
||||
return generatedCode;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user