jline for repl

This commit is contained in:
Stepan Koltsov
2012-06-09 00:28:17 +04:00
parent 48b1ab417b
commit 8c7a879e65
6 changed files with 59 additions and 15 deletions
+11
View File
@@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="jline">
<CLASSES>
<root url="jar://$PROJECT_DIR$/dependencies/jline-2.6.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/dependencies/jline-2.6-sources.jar!/" />
</SOURCES>
</library>
</component>
+1
View File
@@ -21,6 +21,7 @@ classpath="$root/out/production/cli"
classpath="$classpath:$root/out/production/backend:$root/out/production/frontend:$root/out/production/frontend.java:$root/out/production/jet.as.java.psi:$root/out/production/util" classpath="$classpath:$root/out/production/backend:$root/out/production/frontend:$root/out/production/frontend.java:$root/out/production/jet.as.java.psi:$root/out/production/util"
classpath="$classpath:$root/out/production/stdlib:$root/out/production/runtime" classpath="$classpath:$root/out/production/stdlib:$root/out/production/runtime"
classpath="$classpath:$root/lib/*:$ideaRoot/lib/*:$ideaRoot/lib/rt/*" classpath="$classpath:$root/lib/*:$ideaRoot/lib/*:$ideaRoot/lib/rt/*"
classpath="$classpath:$root/dependencies/jline-2.6.jar"
exec java $JAVA_OPTS \ exec java $JAVA_OPTS \
-ea \ -ea \
+2
View File
@@ -21,6 +21,7 @@
<pathelement path="${idea.sdk}/lib/resources_en.jar"/> <pathelement path="${idea.sdk}/lib/resources_en.jar"/>
<fileset dir="${basedir}/lib" includes="**/*.jar"/> <fileset dir="${basedir}/lib" includes="**/*.jar"/>
<fileset dir="${basedir}/dependencies" includes="jline-*.jar"/>
<fileset dir="${basedir}/js/js.translator/lib" includes="*.jar"/> <fileset dir="${basedir}/js/js.translator/lib" includes="*.jar"/>
</path> </path>
@@ -165,6 +166,7 @@
<zipgroupfileset dir="${basedir}/lib" includes="*.jar"/> <zipgroupfileset dir="${basedir}/lib" includes="*.jar"/>
<zipgroupfileset dir="${basedir}/ideaSDK/core" includes="*.jar"/> <zipgroupfileset dir="${basedir}/ideaSDK/core" includes="*.jar"/>
<zipgroupfileset dir="${basedir}/js/js.translator/lib" includes="*.jar"/> <zipgroupfileset dir="${basedir}/js/js.translator/lib" includes="*.jar"/>
<zipgroupfileset dir="${basedir}/dependencies" includes="jline-*.jar"/>
<zap pattern="com.thoughtworks.xstream.converters.extended.ISO8601*"/> <zap pattern="com.thoughtworks.xstream.converters.extended.ISO8601*"/>
<zap pattern="com.thoughtworks.xstream.converters.reflection.CGLIBEnhancedConverter*"/> <zap pattern="com.thoughtworks.xstream.converters.reflection.CGLIBEnhancedConverter*"/>
+1
View File
@@ -15,6 +15,7 @@
<orderEntry type="library" name="intellij-core" level="project" /> <orderEntry type="library" name="intellij-core" level="project" />
<orderEntry type="library" name="asm" level="project" /> <orderEntry type="library" name="asm" level="project" />
<orderEntry type="module" module-name="js.translator" /> <orderEntry type="module" module-name="js.translator" />
<orderEntry type="library" name="jline" level="project" />
</component> </component>
</module> </module>
@@ -17,14 +17,14 @@
package org.jetbrains.jet.cli.jvm.repl; package org.jetbrains.jet.cli.jvm.repl;
import com.intellij.openapi.Disposable; import com.intellij.openapi.Disposable;
import jline.console.ConsoleReader;
import jline.console.history.FileHistory;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
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.utils.ExceptionUtils; import org.jetbrains.jet.utils.ExceptionUtils;
import javax.sound.midi.SysexMessage;
import java.io.Console;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -37,6 +37,8 @@ public class ReplFromTerminal {
private Throwable replInitializationFailed; private Throwable replInitializationFailed;
private final Object waitRepl = new Object(); private final Object waitRepl = new Object();
private final ConsoleReader consoleReader;
public ReplFromTerminal(@NotNull final Disposable disposable, @NotNull final CompilerDependencies compilerDependencies) { public ReplFromTerminal(@NotNull final Disposable disposable, @NotNull final CompilerDependencies compilerDependencies) {
final List<File> extraClasspath = Collections.<File>emptyList(); final List<File> extraClasspath = Collections.<File>emptyList();
new Thread("initialize-repl") { new Thread("initialize-repl") {
@@ -52,6 +54,14 @@ public class ReplFromTerminal {
} }
} }
}.start(); }.start();
try {
consoleReader = new ConsoleReader("kotlin", System.in, System.out, null);
consoleReader.setHistoryEnabled(true);
consoleReader.setHistory(new FileHistory(new File(new File(System.getProperty("user.home")), ".kotlin_history")));
} catch (Exception e) {
throw ExceptionUtils.rethrow(e);
}
} }
private ReplInterpreter getReplInterpreter() { private ReplInterpreter getReplInterpreter() {
@@ -74,6 +84,7 @@ public class ReplFromTerminal {
} }
private void doRun() { private void doRun() {
try {
System.out.println("Kotlin"); System.out.println("Kotlin");
while (true) { while (true) {
boolean next = one(); boolean next = one();
@@ -81,11 +92,20 @@ public class ReplFromTerminal {
break; break;
} }
} }
} catch (Exception e) {
throw ExceptionUtils.rethrow(e);
} finally {
try {
((FileHistory) consoleReader.getHistory()).flush();
} catch (Exception e) {
System.err.println("failed to flush history: " + e);
}
}
} }
private boolean one() { private boolean one() {
System.out.print(">>> "); try {
String line = System.console().readLine(); String line = consoleReader.readLine(">>> ");
if (line == null) { if (line == null) {
return false; return false;
} }
@@ -93,6 +113,10 @@ public class ReplFromTerminal {
System.out.println(value); System.out.println(value);
return true; return true;
} }
catch (Exception e) {
throw ExceptionUtils.rethrow(e);
}
}
public static void run(@NotNull Disposable disposable, @NotNull CompilerDependencies compilerDependencies) { public static void run(@NotNull Disposable disposable, @NotNull CompilerDependencies compilerDependencies) {
new ReplFromTerminal(disposable, compilerDependencies).doRun(); new ReplFromTerminal(disposable, compilerDependencies).doRun();
+5
View File
@@ -63,6 +63,11 @@
<!-- dx.jar --> <!-- dx.jar -->
<get src="http://search.maven.org/remotecontent?filepath=com/google/android/tools/dx/1.7/dx-1.7.jar" dest="dependencies/dx.jar"/> <get src="http://search.maven.org/remotecontent?filepath=com/google/android/tools/dx/1.7/dx-1.7.jar" dest="dependencies/dx.jar"/>
<get src="http://search.maven.org/remotecontent?filepath=com/google/android/tools/dx/1.7/dx-1.7-sources.jar" dest="dependencies/dx-sources.jar"/> <get src="http://search.maven.org/remotecontent?filepath=com/google/android/tools/dx/1.7/dx-1.7-sources.jar" dest="dependencies/dx-sources.jar"/>
<get src="http://repo1.maven.org/maven2/jline/jline/2.6/jline-2.6.jar"
dest="dependencies/jline-2.6.jar"/>
<get src="http://repo1.maven.org/maven2/jline/jline/2.6/jline-2.6-sources.jar"
dest="dependencies/jline-2.6-sources.jar"/>
</target> </target>
<macrodef name="execute_update"> <macrodef name="execute_update">