Skip to content

Commit

Permalink
SI-9206 Local refactor to save eyesight
Browse files Browse the repository at this point in the history
We talk about bit rot but not about how dust accumulates on
code that hasn't been swept since the last time the furniture
was moved around.
  • Loading branch information
som-snytt committed Jun 21, 2015
1 parent 3bfafbc commit 7968421
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
79 changes: 38 additions & 41 deletions src/repl/scala/tools/nsc/interpreter/ILoop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,16 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
echo("\n// Detected repl transcript paste: ctrl-D to finish.\n")
apply(Iterator(start) ++ readWhile(!isPromptOnly(_)))
}

def unapply(line: String): Boolean = isPrompted(line)
}

private object invocation {
def unapply(line: String): Boolean = Completion.looksLikeInvocation(line)
}

private val lineComment = """\s*//.*""".r // all comment

/** Interpret expressions starting with the first line.
* Read lines until a complete compilation unit is available
* or until a syntax error has been seen. If a full unit is
Expand All @@ -795,53 +803,42 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
// signal completion non-completion input has been received
in.completion.resetVerbosity()

def reallyInterpret = {
val reallyResult = intp.interpret(code)
(reallyResult, reallyResult match {
case IR.Error => None
case IR.Success => Some(code)
case IR.Incomplete =>
if (in.interactive && code.endsWith("\n\n")) {
echo("You typed two blank lines. Starting a new command.")
def reallyInterpret = intp.interpret(code) match {
case IR.Error => None
case IR.Success => Some(code)
case IR.Incomplete if in.interactive && code.endsWith("\n\n") =>
echo("You typed two blank lines. Starting a new command.")
None
case IR.Incomplete =>
in.readLine(paste.ContinueString) match {
case null =>
// we know compilation is going to fail since we're at EOF and the
// parser thinks the input is still incomplete, but since this is
// a file being read non-interactively we want to fail. So we send
// it straight to the compiler for the nice error message.
intp.compileString(code)
None
}
else in.readLine(paste.ContinueString) match {
case null =>
// we know compilation is going to fail since we're at EOF and the
// parser thinks the input is still incomplete, but since this is
// a file being read non-interactively we want to fail. So we send
// it straight to the compiler for the nice error message.
intp.compileString(code)
None

case line => interpretStartingWith(code + "\n" + line)
}
})

case line => interpretStartingWith(code + "\n" + line)
}
}

/** Here we place ourselves between the user and the interpreter and examine
* the input they are ostensibly submitting. We intervene in several cases:
/* Here we place ourselves between the user and the interpreter and examine
* the input they are ostensibly submitting. We intervene in several cases:
*
* 1) If the line starts with "scala> " it is assumed to be an interpreter paste.
* 2) If the line starts with "." (but not ".." or "./") it is treated as an invocation
* on the previous result.
* 3) If the Completion object's execute returns Some(_), we inject that value
* and avoid the interpreter, as it's likely not valid scala code.
* 1) If the line starts with "scala> " it is assumed to be an interpreter paste.
* 2) If the line starts with "." (but not ".." or "./") it is treated as an invocation
* on the previous result.
* 3) If the Completion object's execute returns Some(_), we inject that value
* and avoid the interpreter, as it's likely not valid scala code.
*/
if (code == "") None
else if (!paste.running && paste.isPrompted(code)) {
paste.transcript(code)
None
}
else if (Completion.looksLikeInvocation(code) && intp.mostRecentVar != "") {
interpretStartingWith(intp.mostRecentVar + code)
code match {
case "" => None
case lineComment() => None // line comment, do nothing
case paste() if !paste.running => paste.transcript(code) ; None
case invocation() if intp.mostRecentVar != "" => interpretStartingWith(intp.mostRecentVar + code)
case _ => reallyInterpret
}
else if (code.trim startsWith "//") {
// line comment, do nothing
None
}
else
reallyInterpret._2
}

// runs :load `file` on any files passed via -i
Expand Down
2 changes: 1 addition & 1 deletion src/repl/scala/tools/nsc/interpreter/Pasted.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ abstract class Pasted {
case _ => code
}

def run() {
def run(): Unit = {
println("// Replaying %d commands from transcript.\n" format cmds.size)
cmds foreach { cmd =>
print(ActualPromptString)
Expand Down

0 comments on commit 7968421

Please sign in to comment.