Skip to content

Commit

Permalink
save history even for parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Li Haoyi committed Aug 5, 2015
1 parent d809509 commit 6f7aa5b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
6 changes: 6 additions & 0 deletions readme/Index.scalatex
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,12 @@
@a("Gitter Channel", href:="https://gitter.im/lihaoyi/Ammonite")

@sect{Changelog}
@sect{0.4.2}
@ul
@li
Fix @a("#139: Can't fix typos?", href := "https://github.com/lihaoyi/Ammonite/issues/139")
@li
Fix bad wrapping of long lines in ammonite-repl
@sect{0.4.1}
@ul
@li
Expand Down
10 changes: 9 additions & 1 deletion repl/src/main/scala/ammonite/repl/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Repl(input: InputStream,
val frontEnd = Ref[FrontEnd](FrontEnd.Ammonite)

val printer = new PrintStream(output, true)
var history = new History(Vector())

Timer("Repl init printer")
val interp: Interpreter = new Interpreter(
prompt,
Expand All @@ -32,8 +34,10 @@ class Repl(input: InputStream,
colors,
printer.print,
storage,
history,
predef
)

Timer("Repl init interpreter")
val reader = new InputStreamReader(input)
def action() = for{
Expand All @@ -44,7 +48,11 @@ class Repl(input: InputStream,
colors().prompt() + prompt() + colors().reset(),
colors(),
interp.pressy.complete(_, interp.eval.previousImportBlock, _),
storage().fullHistory()
storage().fullHistory(),
addHistory = (code) => if (code != "") {
storage().fullHistory() = storage().fullHistory() :+ code
history = history :+ code
}
)
_ <- Signaller("INT") { interp.mainThread.stop() }
out <- interp.processLine(code, stmts, _.foreach(printer.print))
Expand Down
20 changes: 13 additions & 7 deletions repl/src/main/scala/ammonite/repl/frontend/FrontEnd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ trait FrontEnd{
prompt: String,
colors: Colors,
compilerComplete: (Int, String) => (Int, Seq[String], Seq[String]),
history: Seq[String]): Res[(String, Seq[String])]
history: Seq[String],
addHistory: String => Unit): Res[(String, Seq[String])]
}

object FrontEnd{
Expand Down Expand Up @@ -56,11 +57,13 @@ object FrontEnd{
prompt: String,
colors: Colors,
compilerComplete: (Int, String) => (Int, Seq[String], Seq[String]),
history: Seq[String]) = {
history: Seq[String],
addHistory: String => Unit) = {
Timer("FrontEnd.Ammonite.action start")
val res = readLine(reader, output, prompt, colors, compilerComplete, history) match{
case None => Res.Exit
case Some(code) =>
addHistory(code)
Parsers.Splitter.parse(code) match{
case Result.Success(value, idx) => Res.Success((code, value))
case f: Result.Failure => Res.Failure(
Expand Down Expand Up @@ -196,7 +199,8 @@ object FrontEnd{
prompt: String,
colors: Colors,
compilerComplete: (Int, String) => (Int, Seq[String], Seq[String]),
history: Seq[String]) = {
history: Seq[String],
addHistory: String => Unit) = {

val term = makeTerm()
term.init()
Expand Down Expand Up @@ -250,10 +254,12 @@ object FrontEnd{
case Some(newCode) =>
val code = buffered + newCode
Parsers.split(code) match{
case Some(Result.Success(value, idx)) => Res.Success(code -> value)
case Some(f: Result.Failure) => Res.Failure(
fastparse.core.SyntaxError.msg(f.input, f.traced.expected, f.index)
)
case Some(Result.Success(value, idx)) =>
addHistory(code)
Res.Success(code -> value)
case Some(f: Result.Failure) =>
addHistory(code)
Res.Failure(fastparse.core.SyntaxError.msg(f.input, f.traced.expected, f.index))
case None => readCode(code + "\n")
}
}
Expand Down
6 changes: 1 addition & 5 deletions repl/src/main/scala/ammonite/repl/interp/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Interpreter(prompt0: Ref[String],
colors0: Ref[Colors],
stdout: String => Unit,
storage: Ref[Storage],
history: => History,
predef: String){ interp =>

val hardcodedPredef =
Expand All @@ -38,14 +39,9 @@ class Interpreter(prompt0: Ref[String],
val dynamicClasspath = new VirtualDirectory("(memory)", None)
var extraJars = Seq[java.io.File]()

var history = new History(Vector())
def processLine(code: String,
stmts: Seq[String],
printer: Iterator[String] => Unit) = {
if (code != "") {
storage().fullHistory() = storage().fullHistory() :+ code
history = history :+ code
}
for{
_ <- Catching { case ex =>
Res.Exception(ex, "Something unexpected went wrong =(")
Expand Down
1 change: 1 addition & 0 deletions repl/src/test/scala/ammonite/repl/BuiltinTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ object BuiltinTests extends TestSuite{
@ repl.colors().error() = Console.YELLOW
""")
}

'workingDir{
check.session("""
@ val originalWd = wd
Expand Down
1 change: 1 addition & 0 deletions repl/src/test/scala/ammonite/repl/Checker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Checker {
Ref(Colors.BlackWhite),
stdout = allOutput += _,
storage = Ref(Storage(tempDir)),
new History(Vector()),
predef = predef
)

Expand Down
1 change: 1 addition & 0 deletions repl/src/test/scala/ammonite/repl/ScriptTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ object ScriptTests extends TestSuite{
Ref(Colors.BlackWhite),
stdout = _ => (),
storage = Ref(storage),
new History(Vector()),
predef = ""
)

Expand Down

0 comments on commit 6f7aa5b

Please sign in to comment.