You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@toree.apache.org by ch...@apache.org on 2017/02/21 22:02:14 UTC

incubator-toree git commit: TOREE-378: Fix is_complete_handler responses.

Repository: incubator-toree
Updated Branches:
  refs/heads/master 5b96c323a -> e048b58a7


TOREE-378: Fix is_complete_handler responses.

This commit includes a bug fix to send indentation instead of the
response twice.

This also updates the is-complete check to require a blank line to end a
multi-line block, which is the way ipython behaves. This avoids running
code when a user intends to continue a block.

Last, this updates the indentation heuristic. First, indentation is
taken from the previous line's starting indentation (before, the first
white space was used). Then, if toree detects => or {, it adds two
spaces.


Project: http://git-wip-us.apache.org/repos/asf/incubator-toree/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-toree/commit/e048b58a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-toree/tree/e048b58a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-toree/diff/e048b58a

Branch: refs/heads/master
Commit: e048b58a7472371d677044deba56da8835859436
Parents: 5b96c32
Author: Ryan Blue <bl...@apache.org>
Authored: Fri Feb 3 14:44:38 2017 -0800
Committer: Ryan Blue <bl...@apache.org>
Committed: Tue Feb 21 13:58:54 2017 -0800

----------------------------------------------------------------------
 .../protocol/v5/handler/IsCompleteHandler.scala |  2 +-
 .../scala/ScalaInterpreterSpecific.scala        | 26 +++++++++++++++++---
 2 files changed, 23 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/e048b58a/kernel/src/main/scala/org/apache/toree/kernel/protocol/v5/handler/IsCompleteHandler.scala
----------------------------------------------------------------------
diff --git a/kernel/src/main/scala/org/apache/toree/kernel/protocol/v5/handler/IsCompleteHandler.scala b/kernel/src/main/scala/org/apache/toree/kernel/protocol/v5/handler/IsCompleteHandler.scala
index 7cab339..15a42d5 100644
--- a/kernel/src/main/scala/org/apache/toree/kernel/protocol/v5/handler/IsCompleteHandler.scala
+++ b/kernel/src/main/scala/org/apache/toree/kernel/protocol/v5/handler/IsCompleteHandler.scala
@@ -48,7 +48,7 @@ class IsCompleteHandler(actorLoader: ActorLoader)
     val codeCompleteFuture = ask(interpreterActor, cr).mapTo[(String, String)]
     codeCompleteFuture.onComplete {
       case Success(tuple) =>
-        val reply = IsCompleteReply(tuple._1, tuple._1)
+        val reply = IsCompleteReply(tuple._1, tuple._2)
         val isCompleteReplyType = MessageType.Outgoing.IsCompleteReply.toString
         logKernelMessageAction("Sending is complete reply for", km)
         actorLoader.load(SystemActorType.KernelMessageRelay) !

http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/e048b58a/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala
----------------------------------------------------------------------
diff --git a/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala b/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala
index bd654e8..98f7599 100644
--- a/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala
+++ b/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala
@@ -329,18 +329,36 @@ trait ScalaInterpreterSpecific extends SettingsProducerLike { this: ScalaInterpr
       val parse = iMain.parse
       parse(code) match {
         case t: parse.Error => ("invalid", "")
-        case t: parse.Success => ("complete", "")
+        case t: parse.Success =>
+          val lines = code.split("\n", -1)
+          val numLines = lines.length
+          // for multiline code blocks, require an empty line before executing
+          // to mimic the behavior of ipython
+          if (numLines > 1 && lines.last.matches("\\s*\\S.*")) {
+            ("incomplete", startingWhiteSpace(lines.last))
+          } else {
+            ("complete", "")
+          }
         case t: parse.Incomplete =>
-          val lastLine = code.split("\n").last
+          val lines = code.split("\n", -1)
           // For now lets just grab the indent of the current line, if none default to 2 spaces.
-          val indent = "\\s+".r.findFirstIn(lastLine).getOrElse("  ")
-          ("incomplete", indent)
+          ("incomplete", startingWhiteSpace(lines.last))
       }
     }
     lastResultOut.reset()
     result
   }
 
+  private def startingWhiteSpace(line: String): String = {
+    val indent = "^\\s+".r.findFirstIn(line).getOrElse("")
+    // increase the indent if the line ends with => or {
+    if (line.matches(".*(?:(?:\\{)|(?:=>))\\s*")) {
+      indent + "  "
+    } else {
+      indent
+    }
+  }
+
   override def newSettings(args: List[String]): Settings = {
     val s = new Settings()